27👍
Try using pad_inches=0
, i.e.
plt.savefig( IMG_DIR + 'match.png',bbox_inches='tight', transparent="True", pad_inches=0)
From the documentation:
pad_inches: Amount of padding around the figure when bbox_inches is
‘tight’.
I think the default is pad_inches=0.1
8👍
Just add plt.tight_layout()
before plt.savefig()
!!
plt.figure(figsize=(16, 10))
# ... Doing Something ...
plt.tight_layout()
plt.savefig('wethers.png')
plt.show()
- Django __call__() missing 1 required keyword-only argument: 'manager'
- Managing multiple settings.py files
- Paypal monthly subscription plan settings for first day of the month and making monthly recurring payment – django python
- In django, is there a way to directly annotate a query with a related object in single query?
- How to customize django rest auth password reset email content/template
5👍
This worked for me. After plotting, get the Axes object from plt using ax = plt.gca(). Then set the xlim, and ylim of ax object to match image width and image height. Matplotlib seems to automatically increase xlim and ylim of viewing area when you plot. Note that while setting y_lim you have to invert the order of coordinates.
for i in range(len(r)):
plt.plot(r[i][0],r[i][1],c=(rgb_number(speeds[i]),0,1-rgb_number(speeds[i])),linewidth=1)
plt.axis('off')
ax = plt.gca();
ax.set_xlim(0.0, width_of_im);
ax.set_ylim(height_of_im, 0.0);
plt.savefig( IMG_DIR + 'match.png',bbox_inches='tight', transparent="True")
- How do I simulate connection errors and request timeouts in python unit tests
- Django: WSGIRequest' object has no attribute 'user' on some pages?
- Django's get_current_language always returns "en"
- Appropriate choice of authentication class for python REST API used by web app
3👍
All previous approaches didn’t quite work for me, they all left some padding around the figure.
The following lines successfully removed the white or transparent padding that was left:
plt.axis('off')
ax = plt.gca()
ax.xaxis.set_major_locator(matplotlib.ticker.NullLocator())
ax.yaxis.set_major_locator(matplotlib.ticker.NullLocator())
plt.savefig(IMG_DIR + 'match.png', pad_inches=0, bbox_inches='tight', transparent=True)
3👍
Use plt.gca().set_position((0, 0, 1, 1))
to let the axes span the whole figure, see reference.
If plt.imshow
is used, this requires that the figure has the correct aspect ratio.
import matplotlib as mpl
import matplotlib.pyplot as plt
# set the correct aspect ratio
dpi = mpl.rcParams["figure.dpi"]
plt.figure(figsize=(560/dpi, 820/dpi))
plt.axis('off')
plt.gca().set_position((0, 0, 1, 1))
im = plt.imread('field.jpg')
plt.imshow(im)
plt.savefig("test.png")
plt.close()