Reputation: 145
I want to make x
and y
axes be of equal lengths (i.e the plot minus the legend should be square ). I wish to plot the legend outside (I have already been able to put legend outside the box). The span of x axis
in the data (x_max - x_min
) is not the same as the span of y axis
in the data (y_max - y_min
).
This is the relevant part of the code that I have at the moment:
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5), fontsize=15 )
plt.axis('equal')
plt.tight_layout()
The following link is an example of an output plot that I am getting : plot
How can I do this?
Upvotes: 3
Views: 1516
Reputation: 339102
Would plt.axis('scaled')
be what you're after? That would produce a square plot, if the data limits are of equal difference.
If they are not, you could get a square plot by setting the aspect of the axes to the ratio of xlimits and ylimits.
import numpy as np
import matplotlib.pyplot as plt
fig, (ax1, ax2) = plt.subplots(1,2)
ax1.plot([-2.5, 2.5], [-4,13], "s-")
ax1.axis("scaled")
ax2.plot([-2.5, 2.5], [-4,13], "s-")
ax2.set_aspect(np.diff(ax2.get_xlim())/np.diff(ax2.get_ylim()))
plt.show()
Upvotes: 6
Reputation: 79
One option you have to is manually set the limits, assuming that you know the size of your dataset.
axes = plt.gca()
axes.set_xlim([xmin,xmax])
axes.set_ylim([ymin,ymax])
A better option would be to iterate through your data to find the maximum x- and y-coordinates, take the greater of those two numbers, add a little bit more to that value to act as a buffer, and set xmax
and ymax
to that new value. You can use a similar method to set xmin
and ymin
: instead of finding the maximums, find the minimums.
To put the legend outside of the plot, I would look at this question: How to put the legend out of the plot
Upvotes: 1