Issue
I am creating a figure in matplotlib with three subplot aligned horizontally. The three subplots have different background color. What I would like is to have the space between the subplot to have the same color of the subplot that is at its the right hand side. I was trying to insert a patch in the figure and pin the coordinate of this patch in the subplot coordinate but did not succeed.
I tried the following code
import matplotlib.pyplot as mplt
import matplotlib.patches as patches
fig1, (ax1, ax2) = mplt.subplots(1,2)
# set graph dimension
ax1.set_xlim([1150,1200])
ax1.set_ylim([-210, 10])
ax2.set_xlim([2350,2400])
ax2.set_ylim([-210, 10])
# Set the axis labels position and content
x_yLabel, y_yLabel= 0.01, 0.95
x_xLabel, y_xLabel = 0.99, 0.05
# Define the shaded region along the x-axis
x_start1 = 1150
x_end1 = 1200
x_start2 = 2300
x_end2 = 2400
#Define colors for CO and Ar
shade_colorAr = 'mediumseagreen'
shade_colorCO = 'indianred'
# Draw the plain background
# For the first Ar flush
ax1.axvspan(x_start1, x_end1, facecolor=shade_colorAr, alpha=0.8)
# For the CO flush
ax2.axvspan(x_start2, x_end2, facecolor=shade_colorCO, alpha=0.8)
x_fig1, y_fig1 = ax1.transFigure.transform((1200, -210))
x_fig2, y_fig2 = ax2.transFigure.transform((2350, 10))
# Define the coordinates for the rectangle
rect = patches.Rectangle((x_fig1, y_fig1), x_fig2-x_fig1, y_fig2-y_fig1, linewidth=1, edgecolor='none', facecolor='lightblue',zorder=1)
# Add the rectangle to the figure
fig1.patches.append(rect)
I have to admit it is a suggestion from ChatGPT, but it doesn't work due to the following error:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[4], line 30
26 # For the CO flush
27 ax2.axvspan(x_start2, x_end2, facecolor=shade_colorCO, alpha=0.8)
---> 30 x_fig1, y_fig1 = ax1.transFigure.transform((1200, -210))
31 x_fig2, y_fig2 = ax2.transFigure.transform((2350, 10))
33 # Define the coordinates for the rectangle
AttributeError: 'Axes' object has no attribute 'transFigure'
Solution
One option would be to set the transform of the Rectangle
to fig.transFigure
, and define everything in figure coordinates.
As we add the patch to ax1
, but it needs to extend outside of that Axes, we also need to set clip_on=False
so it is not clipped.
Note: In my opinion, this is the better option than adding the patch to the figure instance, since then it would be in front of the Axes by default, or if you set the zorder below the Axes, you would no longer see the patch inside the Axes limits.
For example:
import matplotlib.pyplot as plt
import matplotlib.patches as patches
fig, (ax1, ax2) = plt.subplots(ncols=2)
# ax.get_position() returns a BBox in figure coordinates
x0 = ax1.get_position().x0 # left of ax1
x1 = ax2.get_position().x0 # left of ax2
y0 = ax1.get_position().y0 # bottom of ax1
y1 = ax1.get_position().y1 # top of ax1
w = x1 - x0 # width of rectangle is distance from left of ax1 to left of ax2
h = y1 - y0 # height of rectangle is height of Axes
# Create rectangle. set transform to figure transform, and set
# clip_on=False so it can extend outside of ax1 limits
rect = patches.Rectangle((x0, y0), w, h, facecolor='r',
clip_on=False, transform=fig.transFigure)
# add rectangle to ax1
ax1.add_patch(rect)
plt.show()
Answered By - tmdavison
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.