Multiple data set building using matplotlib.pyplot.plot_date

This can be a very simple question for most of you using matplotlib. Please help me. I want to build two arrays of the type [1,2,3,4] and [4,5,6,7] compared to the time in the same plot. I am trying to use matplotlib.pyplot.plot_date but cannot figure out how to do this. It seems to me that only one trend can be built using plot_date in one plot.

Thank you in advance

+6
source share
1 answer

To use a chart date with several trends, it is easiest to call it several times. For instance:

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

Alternatively, you can use one call to plot (rather than plot_date ) and then call plt.gca().xaxis_date() if you want. plot_date just calls plot , and then ax.xaxis_date() .

+10
source

Source: https://habr.com/ru/post/896828/


All Articles