2014-01-28 32 views
1

我有兩個numpy的陣列,我想繪製:繪製水文沉澱情節

runoff = np.array([1,4,5,6,7,8,9]) 
precipitation = np.array([4,5,6,7,3,3,7]) 

降水陣列應該來自頂部,酒吧。圖中底部的徑流線。兩者都必須在左側和右側有不同的軸。這種情況很難描述,因此我只是添加了一個我發現用谷歌圖片搜索的情節鏈接。

Universtity of Jena, Hydrograph plot

我能有R做,但我想與matplotlib模塊,以瞭解它,現在我有點卡住......

+0

如果你已經可以在R中解決這個問題,也許你應該這樣做?如果你想學習matplotlib,那麼[tutorial](http://matplotlib.org/users/pyplot_tutorial.html)是一個很好的開始和[gallery](http://matplotlib.org/gallery.html)有很多例子。首先,請嘗試http://matplotlib.org/examples/api/two_scales.html(多個數據比例)和http://matplotlib.org/examples/api/barchart_demo.html(繪圖條) – Bonlenfum

回答

2

這裏有一個想法:

import matplotlib.pyplot as plt 
import numpy as np 

runoff = np.array([1,4,5,6,7,8,9]) 
precipitation = np.array([4,5,6,7,3,3,7]) 


fig, ax = plt.subplots() 

# x axis to plot both runoff and precip. against 
x = np.linspace(0, 10, len(runoff)) 

ax.plot(x, runoff, color="r") 

# Create second axes, in order to get the bars from the top you can multiply 
# by -1 
ax2 = ax.twinx() 
ax2.bar(x, -precipitation, 0.1) 

# Now need to fix the axis labels 
max_pre = max(precipitation) 
y2_ticks = np.linspace(0, max_pre, max_pre+1) 
y2_ticklabels = [str(i) for i in y2_ticks] 
ax2.set_yticks(-1 * y2_ticks) 
ax2.set_yticklabels(y2_ticklabels) 

plt.show() 

enter image description here

當然,還有更好的方法來做到這一點,從@ Pierre_GM的答案,它看起來像有這可能是更好的一個現成的辦法。

+0

感謝您的支持幫助...我現在使用了您的解決方案,但會深入研究Hydroclimpy模塊。 – MonteCarlo