Issue
I have the simple python code below that rotates an ellipse. I want to plot with x and y axes the same range (from -3 to +3) to visually check the rotation is as expected. This works for the x axis, but not so for the y axis (see image below). What am I doing wrong?
import math
import numpy as np
import matplotlib.pyplot as plt
theta = np.arange(0, 2*np.pi, 0.01)
a = 1
b = 2
xpos = a*np.cos(theta)
ypos = b*np.sin(theta)
rotn = math.radians(45);
new_xpos = xpos*np.cos(rotn)+ypos*np.sin(rotn)
new_ypos = -xpos*np.sin(rotn)+ypos*np.cos(rotn)
plt.plot(xpos,ypos, 'b')
plt.plot(new_xpos,new_ypos, 'r')
plt.axis('equal')
plt.xlim=([-3, 3])
plt.ylim=([-3, 3])
plt.show()
Solution
There are 2 problems within this code:
1.
plt.xlim=([-3, 3])
plt.ylim=([-3, 3])
needs to be
plt.xlim([-3, 3])
plt.ylim([-3, 3])
The line plt.axis('equal')
prevents the xlim/ylim
settings. You have to remove that line from your code in order to make xlim/ylim
work.
plt.axis('equal')
Set equal scaling (i.e., make circles circular) by changing the axis limits. This is the same as ax.set_aspect('equal', adjustable='datalim'). Explicit data limits may not be respected in this case. https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.axis.html
Answered By - tetris programming
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.