I want to have a master plot, and various subplots that will be plotted at specific locations on the master plot coordinate system. The y coordinates should be matching and the x of the subplots is local to each.
So far I have the following code, but the subplot y coordinates do not match the y coordinate of the master plot.
<code>import matplotlib.pyplot as plt
fig = plt.figure(figsize=(10, 5),dpi = 300)
# Master Axes - Setting limits to master plot dimensions
ax_master = fig.add_subplot(111)
ax_master.set_xlim(master_xlims)
ax_master.set_ylim(master_ylims)
ax_master.plot([0, 2500], [0, 0], color='blue') # Example line
ax_master.fill_between([0, 2500], [-25, -25], [0, 0], color='gray', alpha=0.5) # Example shaded area
master_trans = ax_master.transAxes.inverted()
x,y = master_trans.transform((2500,-40))
# Subplot information: (x, y, width, height) in master coordinates
# Create each subplot within the master plot
for x, y, width, height in subplots_info:
# Normalize x position and width relative to the master plot's width
# x,y = master_trans.transform(ax_master.transData.transform((x,y)))
norm_x = x / master_xlims[1]
norm_width = width / master_xlims[1]
# Normalize y position and height relative to the figure's dimensions
# Calculate the bottom position in normalized coordinates considering the figure's height
fig_bottom = (y - master_ylims[0]) / (master_ylims[1] - master_ylims[0])
fig_height = height / (master_ylims[1] - master_ylims[0])
ax_sub = fig.add_axes([norm_x, fig_bottom, norm_width, fig_height])
ax_sub.set_ylim([y, y + height]) # Matching the master plot's y-coordinates exactly
ax_sub.plot() # You may add actual plot commands here
ax_sub.set_title(f"Subplot from y={y} to y={y+height}")
# pos = ax_sub.get_position()
# ax_sub.set_position([pos.x0, fig_bottom, pos.width, fig_height])
<code>import matplotlib.pyplot as plt
# Master plot dimensions
master_xlims = (0, 2500)
master_ylims = (-40, 40)
# Create a master figure
fig = plt.figure(figsize=(10, 5),dpi = 300)
# fig.tight_layout()
# Master Axes - Setting limits to master plot dimensions
ax_master = fig.add_subplot(111)
ax_master.set_xlim(master_xlims)
ax_master.set_ylim(master_ylims)
ax_master.plot([0, 2500], [0, 0], color='blue') # Example line
ax_master.fill_between([0, 2500], [-25, -25], [0, 0], color='gray', alpha=0.5) # Example shaded area
master_trans = ax_master.transAxes.inverted()
x,y = master_trans.transform((2500,-40))
print(x,y)
# Subplot information: (x, y, width, height) in master coordinates
subplots_info = [
(100, 0, 300, 10),
(1500, -20, 300, 15),
(500, 0, 300, 20)
]
# Create each subplot within the master plot
for x, y, width, height in subplots_info:
# Normalize x position and width relative to the master plot's width
# x,y = master_trans.transform(ax_master.transData.transform((x,y)))
norm_x = x / master_xlims[1]
norm_width = width / master_xlims[1]
# Normalize y position and height relative to the figure's dimensions
# Calculate the bottom position in normalized coordinates considering the figure's height
fig_bottom = (y - master_ylims[0]) / (master_ylims[1] - master_ylims[0])
fig_height = height / (master_ylims[1] - master_ylims[0])
# Add subplot
ax_sub = fig.add_axes([norm_x, fig_bottom, norm_width, fig_height])
ax_sub.set_ylim([y, y + height]) # Matching the master plot's y-coordinates exactly
ax_sub.plot() # You may add actual plot commands here
ax_sub.set_title(f"Subplot from y={y} to y={y+height}")
ax_sub.margins(0.00)
# pos = ax_sub.get_position()
# ax_sub.set_position([pos.x0, fig_bottom, pos.width, fig_height])
plt.show()
</code>
import matplotlib.pyplot as plt
# Master plot dimensions
master_xlims = (0, 2500)
master_ylims = (-40, 40)
# Create a master figure
fig = plt.figure(figsize=(10, 5),dpi = 300)
# fig.tight_layout()
# Master Axes - Setting limits to master plot dimensions
ax_master = fig.add_subplot(111)
ax_master.set_xlim(master_xlims)
ax_master.set_ylim(master_ylims)
ax_master.plot([0, 2500], [0, 0], color='blue') # Example line
ax_master.fill_between([0, 2500], [-25, -25], [0, 0], color='gray', alpha=0.5) # Example shaded area
master_trans = ax_master.transAxes.inverted()
x,y = master_trans.transform((2500,-40))
print(x,y)
# Subplot information: (x, y, width, height) in master coordinates
subplots_info = [
(100, 0, 300, 10),
(1500, -20, 300, 15),
(500, 0, 300, 20)
]
# Create each subplot within the master plot
for x, y, width, height in subplots_info:
# Normalize x position and width relative to the master plot's width
# x,y = master_trans.transform(ax_master.transData.transform((x,y)))
norm_x = x / master_xlims[1]
norm_width = width / master_xlims[1]
# Normalize y position and height relative to the figure's dimensions
# Calculate the bottom position in normalized coordinates considering the figure's height
fig_bottom = (y - master_ylims[0]) / (master_ylims[1] - master_ylims[0])
fig_height = height / (master_ylims[1] - master_ylims[0])
# Add subplot
ax_sub = fig.add_axes([norm_x, fig_bottom, norm_width, fig_height])
ax_sub.set_ylim([y, y + height]) # Matching the master plot's y-coordinates exactly
ax_sub.plot() # You may add actual plot commands here
ax_sub.set_title(f"Subplot from y={y} to y={y+height}")
ax_sub.margins(0.00)
# pos = ax_sub.get_position()
# ax_sub.set_position([pos.x0, fig_bottom, pos.width, fig_height])
plt.show()
this is the result
enter image description here
As you can see, for example the middle plot should extend from 0 to 20 but goes below zero and above 20 of the master plot. How do I solve this?
Thanks
Tried removing the padding but did not work. tight_layout() also did not work