2016-04-15 68 views
0

我正在嘗試使用從氣象數據中提取的值在底圖上生成地圖。示例代碼: -如何使用matplotlib作爲註釋來繪製數組中的數字?

y=[2.56422, 3.77284,3.52623,3.51468,3.02199] 
z=[0.15, 0.3, 0.45, 0.6, 0.75] 
n=[58,651,393,203,123] 

fig, ax = plt.subplots() 
ax.scatter(z, y) 

for i, txt in enumerate(n): 
    ax.annotate(txt, (z[i],y[i])) 

enter image description here

我使用的數據是numpy的陣列。我不知道如何遍歷每個數組來繪製類似於上面的地圖。我只想繪製數值(即沒有0​​或contourf)。

最初我試圖用pylab.plot函數繪製浮點值。然而,retured錯誤

ValueError: third arg must be a format string 

然後我試圖此numpy的數組轉換爲字符串,然後用這個命令繪製: -

temperature = np.array2string(data, precision=2) 

和print語句看起來像一個修改後的字符串: -

print temperature 
[[ 19.69 21.09 21.57 21.45 20.59 20.53 20.93 20.63 20.64 21.26 
    21.29 20.63 20.98 21.01 20.84 20.81 20.55 20.33 20.52 20.23 
    19.84] 
[ 20.77 21.35 20.81 20.64 20.9 20.78 20.79 23.57 20.11 21.07 
    21.06 21.33 21.48 21.18 21.4 21.09 20.5 20.31 20.12 19.8 
    19.97] 
[ 21.51 21.23 20.55 20.08 20.05 20.78 21.17 24.77 21.17 20.95 
    21.43 21.47 21.46 21.77 21.69 21.13 20.47 20.04 20.08 20.37 
    20.14] 
[ 21.29 21.1 20.63 20.32 20.22 20.37 24.4 23.82 22.23 21.03 
    22.11 22.62 22.71 22.37 21.73 21.35 21.03 20.67 20.58 20.89 
    20.93] 
[ 21.24 21.04 20.68 20.56 20.76 20.91 24.26 23.75 23.28 21.26 
    21.48 22. 21.94 21.78 21.36 21.14 20.96 20.92 21.1 21.19 
    21.31] 
[ 20.83 20.88 20.6 20.87 21.01 21.91 22.33 22.21 21.74 20.66 
    20.76 20.73 21.04 21.09 20.83 20.7 20.72 20.71 21.23 21.04 
    20.73] 
[ 20.32 20.41 20.19 20.05 20.68 22.17 21.82 20.67 19.85 19.02 
    18.91 19.6 20.15 20.64 20.64 20.09 19.81 19.76 19.9 19.94 
    19.46] 
[ 19.68 20.37 20.56 20.68 20.93 21.28 21.24 20.33 20.7 20. 
    18.72 18.94 19.56 19.57 19.83 19.74 19.17 18.53 18.1 18.72 
    19.12] 
[ 18.88 19.71 20.77 20.81 20.32 21.58 20.96 21.33 21.2 20.17 
    19.95 22.05 19.72 19.85 19.3 18.75 18.69 18.44 17.57 17.2 
    18.22] 
[ 19.11 19.19 20.13 20.78 21.25 21.98 21.15 20.96 20.66 20.14 
    20.51 21.92 20.36 20.27 19. 18.22 17.81 17.58 17.16 16.67 
    17.46] 
[ 18.5 19.28 19.57 20.01 21.16 21.01 21.06 20.93 20.62 19.89 
    20.3 20.7 19.7 19.76 18.24 17. 16.36 16.63 17.62 17.32 
    17.38] 
[ 17.6 18.33 20.27 19.97 20.63 20.51 21.09 21.39 20.81 19.55 
    20. 18.3 17.32 18.24 17.57 17.15 16.42 15.76 16.14 16.45 
    21.95] 
[ 17.04 17.55 18.16 18.32 21.23 20.5 20.41 19.82 20.7 20.55 
    20.41 18.47 18.05 17.63 17.11 15.6 16.02 15.46 14.29 13.88 
    23.04]] 

最後,我得到這個錯誤,當我試圖與這條線在地圖上繪製上述值

pylab.plot(x, y, temperature) 

    'Unrecognized character %c in format string' % c) 
ValueError: Unrecognized character [ in format string 

問題似乎與nparray字符串轉換。

任何幫助解決這個問題表示讚賞。

+0

你能解釋一下你的數據結構嗎?你是否有一個有每個像素溫度值的規則矩陣?你能提供一些文件作爲例子來做一些測試嗎? – Alejandro

+0

data - > http://www.mediafire.com/download/0epjjdm8auit611/mslp.txt x - > http://www.mediafire.com/download/1dn6p8nw96h2mmd/xlong.txt y - > http:/ /www.mediafire.com/download/31suzsz6j7u2bgz/xlat.txt。第一個鏈接不是溫度值,但數組長度相同。 –

+0

數據結構如何?爲什麼我得到形狀陣列(178,343)? – Alejandro

回答

1

您的原始解決方案ax.annotate對於您更通用的解決方案而言是完美無缺的。唯一需要改變的是在2d陣列的情況下,你需要在使用np.ravel()(這也是ndarray類的類方法)對它們進行循環之前將它們弄平。

然而,在特定情況下,你可以通過broadcast饒明確的索引使用ravel()荷蘭國際集團需要繪製三個數組:

import numpy as np 
import matplotlib.pyplot as plt 

# generate some dummy data 
z,y = np.meshgrid(range(3),range(3)) 
n = np.random.random_integers(low=50,high=500,size=z.shape) 

fig, ax = plt.subplots() 
ax.scatter(z, y) 

for zz,yy,txt in np.broadcast(z,y,n): 
    ax.annotate(txt, (zz,yy)) 

注意的np.broadcast的結果是一樣的如果我們使用zip(z.ravel(),y.ravel(),n.ravel()),與np.broadcast創建一個類似生成器的對象的巨大差異,所以它不會創建必要的巨大數組(這是完美的)。

+0

感謝您的回答。完美的作品。 –