Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

matplotlib measure the width of text

import matplotlib.pyplot as plt

# some example plot
plt.plot([1,2,3], [2,3,4])

t = plt.text(1.1, 3.1, "my text", fontsize=18)

# to get the text bounding box 
# we need to draw the plot
plt.gcf().canvas.draw()


# get bounding box of the text 
# in the units of the data
bbox = t.get_window_extent()
    .inverse_transformed(plt.gca().transData)


print(bbox)
# prints: Bbox(x0=1.1, y0=3.0702380952380954, x1=1.5296875, y1=3.2130952380952382)


# plot the bounding box around the text
plt.plot([bbox.x0, bbox.x0, bbox.x1, bbox.x1, bbox.x0],
         [bbox.y0, bbox.y1, bbox.y1, bbox.y0, bbox.y0])

plt.show()
Comment

matplotlib measure the width of text

from matplotlib import pyplot as plt

f = plt.figure()
r = f.canvas.get_renderer()
t = plt.text(0.5, 0.5, 'test')

bb = t.get_window_extent(renderer=r)
width = bb.width
height = bb.height
Comment

PREVIOUS NEXT
Code Example
Python :: how to run cmd line commands in python 
Python :: how to url encode using python django 
Python :: run linux command using python 
Python :: pyhon random number 
Python :: fstring 
Python :: how to do a foreach loop in python 
Python :: pd.read_excel column data type 
Python :: python dict key delete 
Python :: remove duplicate columns python dataframe 
Python :: how to check an element in a list in python 
Python :: how to do disconnect command on member in discord python 
Python :: defualt image django 
Python :: python plotting moving average 
Python :: custom validation in django models 
Python :: pythonwrite to file 
Python :: select a random element from a list python 
Python :: python pdf fpdf example 
Python :: python pip jupyter notebook install 
Python :: measure time per line python 
Python :: complex arrays python 
Python :: tower of hanoi python 
Python :: how to find the position in a list python 
Python :: the list of prime number in a given range python 
Python :: django id 
Python :: python input timeout 
Python :: instance variable in python 
Python :: python generate public private key pair 
Python :: correlation between images python 
Python :: pandas pass two columns to function 
Python :: python remove space from end of string 
ADD CONTENT
Topic
Content
Source link
Name
5+1 =