簡體   English   中英

Matplotlib輔助軸具有相同的長寬比

[英]Matplotlib secondary axis with equal aspect ratio

我正在嘗試使用twinx()將輔助軸創建為具有相同長寬比的軸,但是,如下面的示例所示,輔助軸與原始寬高比不匹配。

還有什么需要做的嗎,還是matplotlib中的錯誤?


碼:

from matplotlib import pyplot as plt

fig = plt.figure()
ax1 = fig.add_subplot(111, adjustable='box-forced', aspect='equal')
ax2 = ax1.twinx()
plt.show()

輸出圖:

matplotlib輸出

像在matplotlib示例中在寄生軸上使用host_subplot創建宿主軸,而不是在fig.add_subplot上創建宿主軸可以解決該問題。 現在,這些圖占據相同的空間並具有相等的縱橫比。

from matplotlib import pyplot as plt
from mpl_toolkits.axes_grid1 import host_subplot

fig = plt.figure()
ax1 = host_subplot(111, adjustable='box-forced', aspect='equal')
ax2 = ax1.twinx()
plt.show()

通過使用SubplotHost創建宿主軸,我可以在實際程序中使用面向對象的API來實現與上述相同的SubplotHost

from PyQt5 import QtWidgets
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from mpl_toolkits.axes_grid1.parasite_axes import SubplotHost

app = QtWidgets.QApplication([])

fig = Figure()
ax1 = SubplotHost(fig, 111, adjustable='box-forced', aspect='equal')
ax2 = ax1.twinx()
fig.add_subplot(ax1)
canvas = FigureCanvas(fig)
canvas.show()

app.exec_()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM