Python 绘制过程线降水图
我有两个numpy阵列,我想绘制它们:Python 绘制过程线降水图,python,numpy,matplotlib,plot,Python,Numpy,Matplotlib,Plot,我有两个numpy阵列,我想绘制它们: runoff = np.array([1,4,5,6,7,8,9]) precipitation = np.array([4,5,6,7,3,3,7]) 降水阵列应以条形从顶部开始。径流作为地块底部的一条线。两者在左侧和右侧都有不同的轴。这有点难以描述的情节,所以我只是添加了一个链接的情节,我发现搜索谷歌图片 我可以用R来做,但我想用matplotlib模块来学习,现在我有点卡住了…如果您不介意阅读代码并自己解决它的话: 请注意,scikits.t
runoff = np.array([1,4,5,6,7,8,9])
precipitation = np.array([4,5,6,7,3,3,7])
降水阵列应以条形从顶部开始。径流作为地块底部的一条线。两者在左侧和右侧都有不同的轴。这有点难以描述的情节,所以我只是添加了一个链接的情节,我发现搜索谷歌图片
我可以用R来做,但我想用matplotlib模块来学习,现在我有点卡住了…如果您不介意阅读代码并自己解决它的话: 请注意,
scikits.timeseries
不再受支持,而scikits.hydrocklimpy
也不受支持。因此,这不是一个现成的解决方案。
不过,阅读这些代码应该会给你一些想法。这里有一个想法:
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的回答来看,似乎有一种现成的方法可能更好 @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()
如果你已经可以在R中解决这个问题,也许你应该这样做?如果您想学习matplotlib,那么是一个很好的起点,并且有大量的示例。首先,请尝试(多数据比例)和(绘图栏)谢谢您的帮助。。。我现在使用了您的解决方案,但将更深入地了解hydroclimpy模块。