Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple data set plotting with matplotlib.pyplot.plot_date

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

like image 765
Siv Niznam Avatar asked Sep 07 '11 21:09

Siv Niznam


People also ask

How do I plot multiple plots in Python matplotlib?

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.


1 Answers

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

enter image description here

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

like image 84
Joe Kington Avatar answered Sep 20 '22 04:09

Joe Kington