Matplotlib imshow and dna_features_viewer: Align X axis

I can’t find a solution to align on the X-axis a matplotlib imshow with a dna features viewer plot.

The python3 code I used is:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>from dna_features_viewer import GraphicFeature, GraphicRecord
import matplotlib
from matplotlib import rcParams
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
rcParams["figure.figsize"] = 15, 12
fig, (ax0, ax1) = plt.subplots(2, 1, height_ratios=[5, 1], sharex=True)
# ax0
data_heatmap = np.random.rand(850, 850)
heatmap = ax0.imshow(data_heatmap, cmap="bone")
fig.colorbar(heatmap, ax=ax0)
ax0.set_xlabel("Scored Residue")
ax0.set_ylabel("Aligned Residue")
ax0.set_ylim(len(data_heatmap) + 1, 1)
# ax1
domains = pd.DataFrame({"domain": ["dom1", "dom2", "dom3"],
"start": [9, 516, 714],
"end": [459, 689, 850],
"color": ["#0000b6", "#02eded", "#ab0000"]})
features = []
for _, row in domains.iterrows():
features.append(GraphicFeature(start=row["start"], end=row["end"], strand=+1, color=row["color"],
label=row["domain"]))
record = GraphicRecord(sequence_length=row["end"] + 1, features=features, plots_indexing="genbank")
record.plot(ax=ax1)
plt.tight_layout()
plt.show()
</code>
<code>from dna_features_viewer import GraphicFeature, GraphicRecord import matplotlib from matplotlib import rcParams import matplotlib.pyplot as plt import numpy as np import pandas as pd rcParams["figure.figsize"] = 15, 12 fig, (ax0, ax1) = plt.subplots(2, 1, height_ratios=[5, 1], sharex=True) # ax0 data_heatmap = np.random.rand(850, 850) heatmap = ax0.imshow(data_heatmap, cmap="bone") fig.colorbar(heatmap, ax=ax0) ax0.set_xlabel("Scored Residue") ax0.set_ylabel("Aligned Residue") ax0.set_ylim(len(data_heatmap) + 1, 1) # ax1 domains = pd.DataFrame({"domain": ["dom1", "dom2", "dom3"], "start": [9, 516, 714], "end": [459, 689, 850], "color": ["#0000b6", "#02eded", "#ab0000"]}) features = [] for _, row in domains.iterrows(): features.append(GraphicFeature(start=row["start"], end=row["end"], strand=+1, color=row["color"], label=row["domain"])) record = GraphicRecord(sequence_length=row["end"] + 1, features=features, plots_indexing="genbank") record.plot(ax=ax1) plt.tight_layout() plt.show() </code>
from dna_features_viewer import GraphicFeature, GraphicRecord
import matplotlib
from matplotlib import rcParams
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd


rcParams["figure.figsize"] = 15, 12
fig, (ax0, ax1) = plt.subplots(2, 1, height_ratios=[5, 1], sharex=True)

# ax0
data_heatmap = np.random.rand(850, 850)
heatmap = ax0.imshow(data_heatmap, cmap="bone")
fig.colorbar(heatmap, ax=ax0)
ax0.set_xlabel("Scored Residue")
ax0.set_ylabel("Aligned Residue")
ax0.set_ylim(len(data_heatmap) + 1, 1)

# ax1
domains = pd.DataFrame({"domain": ["dom1", "dom2", "dom3"],
                        "start": [9, 516, 714],
                        "end": [459, 689, 850],
                        "color": ["#0000b6", "#02eded", "#ab0000"]})
features = []
for _, row in domains.iterrows():
    features.append(GraphicFeature(start=row["start"], end=row["end"], strand=+1, color=row["color"],
                                   label=row["domain"]))
record = GraphicRecord(sequence_length=row["end"] + 1, features=features, plots_indexing="genbank")
record.plot(ax=ax1)


plt.tight_layout()
plt.show()

Which produces the following plot:

But I would like the X axis of ax0 and ax1 to be on the same physical scale (same length).

Any idea?

To shrink ax1 to the width of ax0, add the following lines after plt.tight_layout():

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>ax0p, ax1p = ax0.get_position(), ax1.get_position()
ax1.set_position([ax0p.x0, ax1p.y0, ax0p.width, ax1p.height])
</code>
<code>ax0p, ax1p = ax0.get_position(), ax1.get_position() ax1.set_position([ax0p.x0, ax1p.y0, ax0p.width, ax1p.height]) </code>
ax0p, ax1p = ax0.get_position(), ax1.get_position()
ax1.set_position([ax0p.x0, ax1p.y0, ax0p.width, ax1p.height]) 

1

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