Pour tracer une série temporelle avec matplotlib, le plus simple est de passer par pandas. Exemple simple:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
ts = pd.Series(np.random.randn(1000), \
index=pd.date_range('1/1/2000', \
periods=1000))
ts = ts.cumsum()
ts.plot()
plt.savefig('SerieTemporelle.png')
plt.show()
Exemple de série temporelle avec matplotlib avec les labels orientés verticalement (source)
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import dates
import datetime
a = np.array([
[1293605162197, 500, 1000],
[1293605477994, 200, 300],
[1293605478057, 50, 150],
[1293605478072, 2500, 2000],
[1293606162213, 500, 1000],
[1293606162229, 200, 600]])
d = a[:,0]
y1 = a[:,1]
y2 = a[:,2]
# convert epoch to matplotlib float format
s = d/1000
ms = d-1000*s # not needed?
dts = map(datetime.datetime.fromtimestamp, s)
fds = dates.date2num(dts) # converted
# matplotlib date format object
hfmt = dates.DateFormatter('%m/%d %H:%M')
fig = plt.figure()
ax = fig.add_subplot(111)
ax.vlines(fds, y2, y1)
ax.xaxis.set_major_locator(dates.MinuteLocator())
ax.xaxis.set_major_formatter(hfmt)
ax.set_ylim(bottom = 0)
plt.xticks(rotation='vertical')
plt.subplots_adjust(bottom=.3)
plt.savefig('SerieTemporelle_VerticalLabel.png')
plt.show()
Encore un autre exemple de série temporelle avec matplotlib (source)
from matplotlib.dates import WeekdayLocator
def plot_series(x, y):
fig, ax = plt.subplots()
ax.plot_date(x, y, fmt='g--') # x = array of dates, y = array of numbers
fig.autofmt_xdate()
# For tickmarks and ticklabels every fourth week
ax.xaxis.set_major_locator(WeekdayLocator(byweekday=MO, interval=4))
# For tickmarks (no ticklabel) every week
ax.xaxis.set_minor_locator(WeekdayLocator(byweekday=MO))
# Grid for both major and minor ticks
plt.grid(True, which='both')
plt.show()
Recherches associées
Liens | Site |
---|---|
Plotting with matplotlib & pandas | Pandas doc |
Creating graph with date and time in axis labels with matplotlib | stackoverflow |
Matthias Friedrich's Blog | blog |
How to plot time series in python | stackoverflow |
Ways to reduce high dimensional data for visualization | stackexchange |
Série temporelle Wikipedia | Wikipedia |
numpy.random.randn | Scipy Doc |
Cookbook / FittingData | Scipy Doc |
Custom date range (x-axis) in time series with matplotlib | stackoverflow |