Plotting a hydrograph-precipitation plot
matplotlib, numpy, plot, python
Solution
Here is an idea:
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()
There are certainly better ways to do this and from @Pierre_GM's answer it looks like there is a ready made way which is probably better.
Problem
I have two numpy arrays which I would like to plot: ``` runoff = np.array([1,4,5,6,7,8,9]) precipitation = np.array([4,5,6,7,3,3,7]) ``` The precipitation array should come from the top as bars. The runoff as line on the bottom part of the plot. Both have to different axis on the left and the right side. It kind of hard to describe that plot so I just add a link of a plot I found searching with google pics. Universtity of Jena, Hydrograph plot I could do it with R but I would like to learn it with the matplotlib module and now I am kind of stuck ...