Reputation: 5000
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:
Upvotes: 2
Views: 645
Reputation: 5000
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_()
Upvotes: 1