2016-11-24 94 views
0

我有情節與一個行這樣的:與曲線旋轉matplotlib pyplot 90度

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-') 

plot1

和我需要將其旋轉,這樣:

x軸應0-1(所以b)和y軸應該反向排序,如

a2 = sorted(a, reverse = True) 
aa2 = sorted(aa, reverse = True) 

所以基本上按順時針方向旋轉它並改變旋轉圖的X軸順序。我最近的嘗試是因爲這樣:

pl.scatter(b,a2,color = "blue", marker = 'o', s = 20) 
pl.plot(ss, aa2, 'r-') 

plot2

但邏輯didnt旋轉,我想要的曲線。有任何想法嗎?

我試了this後,但沒有什麼幫助。 pl.scatter似乎不必須orientation屬性和scipy.ndimage retured我

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 

而且,在旋轉的情節應該有更多的點,我會添加。所以我真的想通過改變曲線點的位置而不是使用任何模糊函數來做到這一點。我在winpython中使用python 3。

回答

1

的旋轉必須始終出現在左右空間中的點(我們稱之爲origin)。

要實現一個旋轉,你需要將你的點移動到原點,旋轉它們到一個選擇的角度並將它們移回去。如果你的角度爲90°,旋轉是直線前進

x_new = -y 
y_new = x 

在這樣的方式的圖像可以旋轉:

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() 

enter image description here

+0

我曾經讀過的最好的答案之一。 Thx很多,我完全忘了看這個問題,因爲關於一個點的旋轉。下一次我會更聰明! – Bobesh

+0

也許有一個問題,爲什麼我圍繞原點旋轉(9,0.5)? – Bobesh

+0

你不需要。任何其他的起源都很好,但由於你原來的y值在0到1之間,我認爲你希望它們在旋轉後處於相同的範圍內,這是通過使用0.5作爲中心來完成的。 – ImportanceOfBeingErnest

0

我可能會誤解你的問題,但爲什麼不只是切換x和y軸的值?

pl.scatter(b,a,color = "blue", marker = 'o', s = 20) 
pl.plot(ss,aa, 'r-') 

like this?

+0

這是軸的翻轉。但是,如果我理解正確,提問者需要輪換。 – ImportanceOfBeingErnest

+0

是的,我需要在這個答案的情節,但verticak軸應該減少而不是增加。 – Bobesh