[英]Matplotlib secondary axis with equal aspect ratio
I'm trying to create a secondary axis to an axis with equal aspect ratio using twinx()
, however, as can be seen in the example below, the secondary axis does not match the aspect ratio of the original. 我正在尝试使用
twinx()
将辅助轴创建为具有相同长宽比的轴,但是,如下面的示例所示,辅助轴与原始宽高比不匹配。
Is there something additional that needs to be done, or is this a bug in matplotlib? 还有什么需要做的吗,还是matplotlib中的错误?
Code: 码:
from matplotlib import pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(111, adjustable='box-forced', aspect='equal')
ax2 = ax1.twinx()
plt.show()
Output figure: 输出图:
Creating the host axis with host_subplot
like in the matplotlib example on parasitic axes instead of fig.add_subplot
appears to resolve the issue. 像在matplotlib示例中在寄生轴上使用
host_subplot
创建宿主轴,而不是在fig.add_subplot
上创建宿主轴可以解决该问题。 The plots now occupy the same space and have equal aspect ratios. 现在,这些图占据相同的空间并具有相等的纵横比。
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()
I was able to achieve the same as above with the object-oriented API used in my actual program by creating the host axis with SubplotHost
通过使用
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.