简体   繁体   中英

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.

Is there something additional that needs to be done, or is this a bug in 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:

matplotlib输出

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. 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

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_()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM