this might be really a simple question for most of you guys using matplotlib. Please help me out. I want to plot two array like [1,2,3,4] and [4,5,6,7] versus time in a same plot. I am trying to use matplotlib.pyplot.plot_date but couldn't figure out how to do it. It seems to me that only one trend can be plotted with plot_date in one plot.
Thank you in advance
In Matplotlib, we can draw multiple graphs in a single plot in two ways. One is by using subplot() function and other by superimposition of second graph on the first i.e, all graphs will appear on the same plot.
To use plot date with multiple trends, it's easiest to call it multiple times. For example:
import datetime
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
# Generate Data
time = mdates.drange(datetime.datetime(2010, 1, 1),
datetime.datetime(2011, 1, 1),
datetime.timedelta(days=10))
y1 = np.cumsum(np.random.random(time.size) - 0.5)
y2 = np.cumsum(np.random.random(time.size) - 0.5)
# Plot things...
fig = plt.figure()
plt.plot_date(time, y1, 'b-')
plt.plot_date(time, y2, 'g-')
fig.autofmt_xdate()
plt.show()
Alternately you can use a single plot
(rather than plot_date
) call and then call plt.gca().xaxis_date()
, if you'd prefer. plot_date
just calls plot
and then ax.xaxis_date()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With