DaveL17
DaveL17

Reputation: 2015

Patches that Account for Margins

I'm trying to add a patch to a plot that takes into account axis margins. The purpose of the patch is to allow for transparent plots with a filled plot area (such that the patch fills the entire plot area.)

the margins for ax are: (0.04, 0.05)

The code I use to add the patch:

ax.add_patch(patches.Rectangle((xlim[0], ylim[0]), xlim[1] - xlim[0], ylim[1] - ylim[0], facecolor=plot_area_color, zorder=1))

I can't find a way to account for the margins. Or, is there an alternate way to set a savefig kwarg transparent=True and have the plot area be filled?

Upvotes: 1

Views: 56

Answers (1)

DaveL17
DaveL17

Reputation: 2015

I stumbled upon an answer that seems to be working well for me. The key was to change the patch from plotting based on the X and Y limits and rather based on the coordinate system. The addition of an attribute transform=ax.transAxes to my patch code has solved my problem.

ax.add_patch(patches.Rectangle((0, 0), 1, 1, transform=ax.transAxes, facecolor=plot_area_color, zorder=1))

For my purpose (transparent chart with filled plot area), the coordinate system is particularly well suited.

Upvotes: 1

Related Questions