2011-11-23 28 views
3

也許我已經提出了標題比問題更復雜,但是這裏有...!用matplotlib繪製笛卡爾空間中有角度纏繞的數據

我有一些角度數據,在xy平面上鄰接的,即跨在360 => 0度線 - 即,358,359,0,1,2 ....

如果我正在策劃這些和設置:

plt.xlim(0,360) 

我當然會有三個點在劇情的最左邊,兩個在最右邊。你可以在這裏(比較複雜,與實際)地塊看到這個(x軸限制故意顛倒):

the angularly-wrapped dataset

我真正喜歡的是把所有點左右在相同的位置繪製情節窗口,也許朝向情節的中心。在這個方案下,x軸減小到360-0度邊界的左邊,並且增加到右邊。

我不想對數據本身進行任何翻譯/轉換(這是一個大型的數據集等),所以我會用matplotlib這個技巧來做這件事。

我打算用hexbin繪製數據點,如果這有什麼區別的話。

爲希望感謝,並感謝您在您的幫助,

戴夫

+0

最簡單的「絕招」。這使我的心是兩次策劃你的數據,即具有變化的x值繪製同樣的東西所以第二個的左邊與第一個的右邊相重合('x_new = x_old + 360')。這是一個選項嗎? – Avaris

回答

4

老實說,我認爲只是將您的數據會快很多。 x[x>180] -= 360非常快。除非您的數據集大小爲幾GB,否則轉換數據所需的時間僅爲幾毫秒。

所以,這裏是最簡單的方式(將您的數據):

import matplotlib.pyplot as plt 
import numpy as np 

# Generate data to match yours... 
y = 60 * np.random.random(300) - 20 
x = 60 * (np.random.random(300) - 0.5) 
x[x < 0] += 360 

# Transform the data back to a -180 to 180 range... 
x[x > 180] -= 360 

# Plot the data 
fig, ax = plt.subplots() 
ax.plot(x, y, 'b.') 

# Set the ticks so that negative ticks represent >180 numbers 
ticks = ax.get_xticks() 
ticks[ticks < 0] += 360 
ax.set_xticklabels([int(tick) for tick in ticks]) 

plt.show() 

enter image description here

但是,如果你想避免將您的數據,你可以做這樣的事情......這是100不過,保證%的速度比轉換數據要慢。 (也許可以忽略慢,但它不會更快。)

import matplotlib.pyplot as plt 
import numpy as np 

# Generate data to match yours... 
y = 60 * np.random.random(300) - 20 
x = 60 * (np.random.random(300) - 0.5) 
x[x < 0] += 360 

fig, (ax1, ax2) = plt.subplots(ncols=2, sharey=True) 
fig.subplots_adjust(wspace=0) 

ax1.spines['right'].set_visible(False) 
ax2.spines['left'].set_visible(False) 
ax1.tick_params(right=False) 
ax2.tick_params(left=False) 
for label in ax2.get_yticklabels(): 
    label.set_visible(False) 

ax1.plot(x[x > 180], y[x > 180], 'b.') 
ax2.plot(x[x <= 180], y[x <= 180], 'b.') 

ax2.set_xticks(ax2.get_xticks()[1:]) 

plt.show() 

enter image description here

+0

+1'x [x <0]'uau!那是什麼? numpy的?請你可以添加一個鏈接到相關的美容文件? – joaquin

+0

這是一個numpy成語(也是「matlab-ism」)。 http://www.scipy.org/Tentative_NumPy_Tutorial#head-d55e594d46b4f347c20efe1b4c65c92779f06268(該鏈接是一個教程,而不是文檔。文檔的相關部分假定您更熟悉numpy。)numpy數組上的邏輯運算返回布爾值陣列。這些可以(除其他外)用於索引相同形狀的numpy數組。 –