fig.add_axes() Subplot normalized coordinates do not match

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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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>
<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

New contributor

user24901980 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật