[英]Rotate matplotlib pyplot with curve by 90 degrees
I have plot with one line as this: 我有一条线的情节是这样的:
import numpy as np
import matplotlib.pyplot as pl
a = np.array([4, 4, 4, 4, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9])
b = np.array([i/len(a) for i in range(1, len(a)+1)])
aa = np.array([i/10 for i in range(40, 91)])
ss = np.array([ 0.06200455, 0.07389492, 0.08721351, 0.10198928, 0.11823225,
0.13593267, 0.15506088, 0.1755675 , 0.19738431, 0.22042543,
0.244589 , 0.26975916, 0.29580827, 0.32259936, 0.34998862,
0.377828 , 0.40596767, 0.43425846, 0.46255411, 0.49071331,
0.51860153, 0.54609255, 0.57306977, 0.5994272 , 0.62507019,
0.64991591, 0.67389356, 0.69694438, 0.71902138, 0.74008905,
0.76012273, 0.77910799, 0.79703987, 0.81392209, 0.82976609,
0.84459023, 0.85841887, 0.87128143, 0.88321163, 0.89424658,
0.90442608, 0.91379189, 0.92238706, 0.93025537, 0.93744079,
0.94398702, 0.94993712, 0.95533313, 0.96021585, 0.96462454,
0.96859684])
pl.scatter(a,b,color = "blue", marker = 'o', s = 20)
pl.plot(aa, ss, 'r-')
and I need to rotate it as this: 我需要这样旋转它:
x-axis should be 0-1 (so the b
) and y-axis should be reverse sorted as, such as x轴应为0-1(因此
b
),y轴应反向排序为,例如
a2 = sorted(a, reverse = True)
aa2 = sorted(aa, reverse = True)
so basically rotate it reverse clockwise and change order in x-axis of rotated plot. 因此基本上将其顺时针旋转并更改旋转图的x轴顺序。 My nearest try was as this:
我最近的尝试是这样的:
pl.scatter(b,a2,color = "blue", marker = 'o', s = 20)
pl.plot(ss, aa2, 'r-')
but logically the curve didnt rotate as I wanted. 但从逻辑上讲,曲线没有按照我的意愿旋转。 Any ideas?
有任何想法吗?
I readed this post but with little help. 我读了这篇文章,但几乎没有帮助。
pl.scatter
seems doesnt have orientation
attribute and scipy.ndimage
retured me pl.scatter
似乎没有orientation
属性,而scipy.ndimage
我失望
File "C:\Users\rpaca\Desktop\WinPython-64bit-3.5.2.2\python-3.5.2.amd64\lib\site-packages\scipy\ndimage
\interpolation.py", line 663, in rotate
ix = input.shape[axes[1]]
IndexError: tuple index out of range
Moreover, in rotated plot there should be more points I will add. 此外,在旋转绘图中,应该添加更多点。 So I really want to do this by changing position of points of that curve rather than with any foggy function.
因此,我真的想通过更改该曲线的点的位置而不是使用任何模糊函数来做到这一点。 I am using python 3 in winpython.
我在winpython中使用python 3。
A rotation must always happen about a point in space (let's call it origin
). 旋转必须始终围绕空间的某个点发生(我们称其为
origin
)。
To implement a rotation, you would need to shift your points to the origin, rotate them about an angle of choice and shift them back. 要实现旋转,您需要将点移动到原点,将它们旋转一个选择角度并向后移动。 In case your angle is 90°, rotation is straight forward
如果您的角度为90°,旋转方向为正
x_new = -y
y_new = x
In such a way the image can be rotated: 通过这种方式可以旋转图像:
import numpy as np
import matplotlib.pyplot as plt
a = np.array([4, 4, 4, 4, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9])
b = np.array([i/float(len(a)) for i in range(1, len(a)+1)])
A = np.array([i/10. for i in range(40, 91)])
B = np.array([ 0.06200455, 0.07389492, 0.08721351, 0.10198928, 0.11823225,
0.13593267, 0.15506088, 0.1755675 , 0.19738431, 0.22042543,
0.244589 , 0.26975916, 0.29580827, 0.32259936, 0.34998862,
0.377828 , 0.40596767, 0.43425846, 0.46255411, 0.49071331,
0.51860153, 0.54609255, 0.57306977, 0.5994272 , 0.62507019,
0.64991591, 0.67389356, 0.69694438, 0.71902138, 0.74008905,
0.76012273, 0.77910799, 0.79703987, 0.81392209, 0.82976609,
0.84459023, 0.85841887, 0.87128143, 0.88321163, 0.89424658,
0.90442608, 0.91379189, 0.92238706, 0.93025537, 0.93744079,
0.94398702, 0.94993712, 0.95533313, 0.96021585, 0.96462454,
0.96859684])
def rotate(x,y, origin=(0,0)):
# shift to origin
x1 = x - origin[0]
y1 = y - origin[1]
#rotate
x2 = -y1
y2 = x1
# shift back
x3 = x2 + origin[1]
y3 = y2 + origin[0]
return x3, y3
# now let's do the rotation
origin = (9.,0.5)
a1, b1 = rotate(a,b, origin )
A1, B1 = rotate(A,B, origin )
fig, (ax1, ax2) = plt.subplots(1,2, figsize=(7,3.3))
ax1.set_title("original")
ax1.scatter(a, b, color = "blue", marker = 'o', s = 20)
ax1.plot (A, B, 'r-')
ax2.set_title(u"90° ccw rotated")
ax2.scatter(a1, b1, color = "blue", marker = 'o', s = 20)
ax2.plot (A1, B1, 'r-')
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.