使用matplotlib.pyplot.plot_date绘制多个数据集 [英] Multiple data set plotting with matplotlib.pyplot.plot_date

查看:168
本文介绍了使用matplotlib.pyplot.plot_date绘制多个数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于大多数使用matplotlib的人来说,这可能确实是一个简单的问题.请帮帮我.我想在同一图中绘制两个数组,如[1,2,3,4]和[4,5,6,7]与时间的关系.我正在尝试使用matplotlib.pyplot.plot_date但无法弄清楚该怎么做.在我看来,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.

提前谢谢

推荐答案

要使用具有多个趋势的绘图日期,最容易多次调用.例如:

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

或者,可以根据需要使用单个plot(而不是plot_date)调用,然后再调用plt.gca().xaxis_date(). plot_date只会先调用plot,然后再调用ax.xaxis_date().

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

这篇关于使用matplotlib.pyplot.plot_date绘制多个数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆