绘制水文图-降水图

5

我有两个NumPy数组需要绘制:

runoff = np.array([1,4,5,6,7,8,9]) 
precipitation = np.array([4,5,6,7,3,3,7])
降水量的数组应该以条形图的形式从顶部显示出来。径流以线条的形式显示在绘图的底部。两者必须在左侧和右侧有不同的轴。这种绘图很难描述,因此我只添加了一个链接,其中包含我在谷歌图片搜索中找到的绘图。 耶拿大学,水文图 我可以使用R完成它,但我想使用matplotlib模块学习,并且现在我有点卡住了...

如果您已经能够在R中解决这个问题,那么为什么不这样做呢?如果您想学习matplotlib,那么tutorial是一个很好的起点,gallery有很多例子。首先,尝试一下http://matplotlib.org/examples/api/two_scales.html(多个数据刻度)和http://matplotlib.org/examples/api/barchart_demo.html(绘制条形图)。 - Bonlenfum
3个回答

阿里云服务器只需要99元/年,新老用户同享,点击查看详情
3
这里有一个想法:
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 的回答中看来,已经有现成的更好的方式了。


谢谢你的帮助...我现在使用了你的解决方案,但会深入研究hydroclimpy模块。 - MonteCarlo

1

0

@Greg Greg的回答很好。但是,通常情况下,您不需要反转y轴并手动修复轴标签。只需将以下代码替换为Greg的答案

# 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.gca().invert_yaxis()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,