Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

padding figures in pyplot

import matplotlib
matplotlib.rc('text',usetex=True)
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
import numpy as np

text = 'egin{tabular}{|c|c|}hline1&2\hline3&4\hlineend{tabular}'

fig, ax = plt.subplots(1)

img = ax.imshow(np.zeros((10,10)), cmap=plt.cm.gray)
txt = ax.text( 4.5,
          4.5,
          text,
          fontsize=24,
          ha='center',
          va='center',
          bbox=dict(alpha=0))

fig.canvas.draw()
bbox = txt.get_bbox_patch()
xmin = bbox.get_window_extent().xmin
xmax = bbox.get_window_extent().xmax
ymin = bbox.get_window_extent().ymin
ymax = bbox.get_window_extent().ymax

xmin, ymin = fig.transFigure.inverted().transform((xmin, ymin))
xmax, ymax = fig.transFigure.inverted().transform((xmax, ymax))

dx = xmax-xmin
dy = ymax-ymin

# The bounding box vals can be tweaked manually here.
rect = Rectangle((xmin-0.02,ymin-0.01), dx+0.04, dy+0.05, fc='w', transform=fig.transFigure)

ax.add_patch(rect)
fig.canvas.draw()
ax.axis('off')
plt.savefig('ok.png',bbox_inches='tight')




Comment

PREVIOUS NEXT
Code Example
Python :: how to iterate over rows in pandas 
Python :: ros python service client 
Python :: python oneline if 
Python :: return key from value dictionary python 
Python :: Python If ... Else 
Python :: pyplot.plot 
Python :: TypeError: expected str, bytes or os.PathLike object, not list 
Python :: django class based views listview 
Python :: float and int difference 
Python :: set password django 
Python :: def is_leap(year): leap = False 
Python :: add new column to pandas dataframe 
Python :: get chrome version with python 
Python :: python buffer 
Python :: shallow copy deep copy python 
Python :: how to duplicate a list in python 
Python :: hash in python 
Python :: pip for python 
Python :: pygame draw square 
Python :: runtime errors in python 
Python :: python elif syntax 
Python :: python newton raphson 
Python :: List Nested Lists 
Python :: python3 delete file 
Python :: show chrome devtools in selenium 
Python :: python os check if file with extension exists 
Python :: how to move an item from one list to another python 
Python :: tkinter pack align left 
Python :: qtablewidget add row python 
Python :: number pattern program in python using for loop 
ADD CONTENT
Topic
Content
Source link
Name
2+4 =