tomicapretto / flexitext

Flexitext: Draw styled text in Matplotlib
https://tomicapretto.github.io/flexitext/
MIT License
115 stars 8 forks source link

plt.savefig() when using flexitext #12

Closed due23 closed 1 year ago

due23 commented 1 year ago

Hi! I'm using flexitext to style my plot titles as well as add in captions underneath my plot with the following code:

ax = states.boundary.plot(edgecolor="#5A5A5A", linewidth=0.5, facecolor="white", zorder=-1)

alpha_shape_gdf.plot(ax = ax, cmap="plasma", column = "decade", legend=True, categorical=True)
lgd = ax.get_legend()
lgd.draw_frame(False)
lgd.set_bbox_to_anchor((1.2, 0.85))

title_text = "<style:italic>Opuntia</> sp. observations per decade"
flexitext(0.5, 1, title_text, va="bottom", ha="center");

caption_text = "<color:#5A5A5A, style:italic, size:7>Distributions calculated with alpha hulls of each decade's Opuntia sp. observations</>"
flexitext(0.05, 0, caption_text, va="top");

plt.xlim([110, 161])
plt.ylim([-45, -8])
plt.axis("off")

plt.savefig("invasives/new filters/opuntia sp final.png", dpi=1200, bbox_extra_artists=(lgd,), bbox_inches='tight')
plt.show()

This code produces the following image but when the plot is saved using plt.savefig it cuts off the title and caption as can be seen below. image image

Usually when a plot element is cut off I add it into the bbox_extra_artists list in plt.savefig() so I tried this but it did not work. Any suggestions on how to save the figure without the flexitext being cut off would be greatly appreciated!

tomicapretto commented 1 year ago

Thanks for opening the issue!

What happens if you remove bbox_inches='tight' from plt.savefig()? Also, it may be worth trying xycoords = "figure fraction" when you call flexitext().

Another thing worth trying is doing plt.show() before plt.savefig()

due23 commented 1 year ago

Thanks for the suggestions! I changed my code to include xycoords = "figure fraction", however it didn't really make a difference in terms of the cutting off of the titles. Removing bbox_inches='tight' from plt.savefig() did mean that the title and caption were not cut off but instead the legend was cut off 😞

image

Alas! I found a solution which doesn't seem to cut any of the plot components off and involved removing bbox_inches='tight' from plt.savefig() and adding in this line of code: plt.tight_layout(). Below is the updated code I used plus the associated figure. Also I'm pretty sure you have to do plt.savefig() before plt.show() otherwise it clears the figure for saving, this is what happened to me at least and I ended up with a blank saved figure.

ax = states.boundary.plot(edgecolor="#5A5A5A", linewidth=0.5, facecolor="white", zorder=-1)

alpha_shape_gdf.plot(ax = ax, cmap="plasma", column = "decade", legend=True, categorical=True)
lgd = ax.get_legend()
lgd.draw_frame(False)
lgd.set_bbox_to_anchor((1.2, 0.85))

title_text = "<style:italic>Opuntia</> sp. observations per decade"
flexitext(0.5, 0.9, title_text, va="bottom", ha="center", xycoords = "figure fraction");

caption_text = "<color:#5A5A5A, style:italic, size:7>Distributions calculated with alpha hulls of each decade's Opuntia sp. observations</>"
flexitext(0.125, 0.1, caption_text, va="top", xycoords = "figure fraction");

plt.xlim([110, 161])
plt.ylim([-45, -8])
plt.tight_layout()
plt.axis("off")

plt.savefig("invasives/new filters/opuntia sp final.png", dpi=1200, bbox_extra_artists=(lgd,))
plt.show()

opuntia sp final

tomicapretto commented 1 year ago

Happy you could fix it! :D