How to enforce xlim and ylim when using ax.axis ('equal')?

I want to use ax.axis('equal')X and Y to force alignment, but I also want to specify specific ranges for the X and Y axes. If the fields are also fixed, the problem is limited, and the result is shown on the left side of Figure 1 . If instead the fields were allowed to automatically expand themselves to occupy the slack, then xlimthey ylimcan remain when I install them, while maintaining them axis('equal'). An example of what I need is shown on the right side of Figure 1 . How can I let field fields float?

f,ax=plt.subplots(1) #open a figure
ax.axis('equal') #make the axes have equal spacing
ax.plot([0,20],[0,20]) #test data set

#change the plot axis limits
ax.set_xlim([2,18]) 
ax.set_ylim([5,15])

#read the plot axis limits
xlim2=array(ax.get_xlim())
ylim2=array(ax.get_ylim())

#define indices for drawing a rectangle with xlim2, ylim2
sqx=array([0,1,1,0,0])
sqy=array([0,0,1,1,0])

#plot a thick rectangle marking the xlim2, ylim2
ax.plot(xlim2[sqx],ylim2[sqy],lw=3) #this does not go all the way around the edge

actual and desired result Figure 1: output from the above code snippet.

+1
1
ax.set_aspect('equal',adjustable='box')

enter image description here

+2

Source: https://habr.com/ru/post/1655991/


All Articles