【问题标题】:Plotting a hydrograph-precipitation plot绘制水文-降水图
【发布时间】:2014-01-28 10:27:38
【问题描述】:

我有两个要绘制的 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 模块来学习它,现在我有点卡住了......

【问题讨论】:

标签: python numpy matplotlib plot


【解决方案1】:

这是一个想法:

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

当然有更好的方法可以做到这一点,从@Pierre_GM 的回答看来,有一种现成的方法可能更好。

【讨论】:

  • 感谢您的帮助...我现在使用了您的解决方案,但会更深入地研究 hydroclimpy 模块。
【解决方案2】:

如果您不介意阅读代码并自己弄清楚: http://hydroclimpy.sourceforge.net/plotlib.flows.html#plotting-hydrographs

请注意,scikits.timeseries 不再受支持,scikits.hydroclimpy 也不支持。因此,它不是一个关键的解决方案。 不过,阅读代码应该会给您一些想法。

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2019-08-06
      • 1970-01-01
      • 2019-05-29
      • 1970-01-01
      • 1970-01-01
      • 2015-09-14
      • 2018-10-29
      • 2014-04-30
      • 1970-01-01
      相关资源
      最近更新 更多