I need a figure in matplotlib where both axes are always the same length. For this I am using the option 'equal'. In most cases it works quite well and I get the expected results (see figure 1), but when the values of the y-axis are much higher than x, the figure shows an unexpected behaviour (see figure 2). Does anyone know this behavior of matplotlib?
Danke, Jörg
host = SubplotHost(fig, 111)
try:
min_x_val = min(x for x in self.x_values if x is not None)
max_x_val = max(self.x_values)
except ValueError:
return
max_y_val = list()
for n, evaluator in enumerate(self.cleaned_evaluators):
max_y_val.append(max(self.y_values[n]))
# axis settings
host.axis['left'].label.set_fontsize('small')
host.axis['left'].major_ticklabels.set_fontsize('small')
host.axis['bottom'].label.set_fontsize('small')
host.axis['bottom'].major_ticklabels.set_fontsize('small')
host.axis['bottom'].major_ticklabels.set_rotation(0)
host.set_ylabel(y_label)
host.set_xlabel(x_label)
host.set_xlim(0, max_x_val)
host.set_ylim(0, max_y_val)
host.minorticks_on()
host.toggle_axisline(False)
host.axes.set_aspect('equal')
host.grid(True, alpha=0.4)
return fig
Figure 1:
Figure 2:
set_aspect('equal')
means that one unit on the x-axis is of same length as one unit on the y-axis, as seen in your Figure 1. – Scarfaceset_aspect
, see the documentation. Taking the quotient ofx
andy
you should be able to scale the axes so that they have the same length. – Unimposingset_aspect('equal')
is forcing the same data scaling (data values per pixel) for both axes. – Lightner