如何使用matplotlib在x轴上绘制特定日期的数据 [英] How to plot data against specific dates on the x-axis using matplotlib

查看:306
本文介绍了如何使用matplotlib在x轴上绘制特定日期的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含日期值对的数据集。我想将它们绘制在一个条形图中,其特定的日期在x轴上。



我的问题是 matplotlib 在整个日期范围内分发 xticks 并且还使用点绘制数据。



日期都是 datetime 对象。以下是数据集的示例:

  data = [(DT.datetime.strptime('2010-02-05', %Y-%m-%d),123),
(DT.datetime.strptime('2010-02-19',%Y-%m-%d),678),
(DT.datetime.strptime('2010-03-05','%Y-%m-%d),987),
(DT.datetime.strptime('2010-03-19' ,%Y-%m-%d),345)]

这是一个可运行的代码样本使用 pyplot

 导入datetime作为DT 
从matplotlib import pyplot as plt

data = [(DT.datetime.strptime('2010-02-05',%Y-%m-%d),123),
DT.datetime.strptime('2010-02-19','%Y-%m-%d),678),
(DT.datetime.strptime('2010-03-05',% Y%m-%d),987),
(DT.datetime.strptime('2010-03-19',%Y-%m-%d),345)]

x = [数据中的(日期,值)的日期]
y = [数据中的(日期,值)的值]

fig = plt.figure()

graph = fig.add_subplot(111)
graph.plot_dat e(x,y)

plt.show()

问题摘要:

我的情况更像是我有一个 Axes 实例准备好(由$ code> / code>),我想执行以下操作:


  1. 使 xticks 对应于确切的日期值。我听说过 matplotlib.dates.DateLocator ,但我不知道如何创建一个,然后将它与一个特定的 Axes 对象。

  2. 更好地控制正在使用的图形类型(条形,线条,点等)


解决方案

你正在做的很简单,只需使用plot,而不是plot_date,最简单。 plot_date对于更复杂的案例来说是非常好的,但是如果没有这种情况,可以轻松地完成所需的设置。



例如,基于上面的示例:

 将datetime作为DT 
从matplotlib导入pyplot作为plt
从matplotlib.dates import date2num

data = [(DT.datetime.strptime('2010-02-05','%Y-%m-%d),123),
(DT.datetime.strptime('2010-02-19 ',%Y-%m-%d),678),
(DT.datetime.strptime('2010-03-05',%Y-%m-%d),987) ,
(DT.datetime.strptime('2010-03-19',%Y-%m-%d),345)]

x = [date2num(date)for (date,value)in data]
y = [value for(date,value)in data]

fig = plt.figure()

graph = fig .add_subplot(111)

#将数据绘制为具有圆形标记的红线
graph.plot(x,y,'r-o')

#将xtick位置设置为仅与您输入的日期对应。
graph.set_xticks(x)

#设置xtick标签与您输入的日期相对应。
graph.set_xticklabels(
[date.strftime(%Y-%m-%d)for(date,value)]


plt.show()

如果您更喜欢条形图,只需使用 plt.bar() 。要了解如何设置线条和标记样式,请参阅 plt.plot()
在标记位置绘制日期标签http://www.geology.wisc.edu/~jkington/matplotlib_date_labels.png


I have a dataset consisting of date-value pairs. I want to plot them in a bar graph with the specific dates in the x-axis.

My problem is that matplotlib distributes the xticks over the entire date range; and also plots the data using points.

The dates are all datetime objects. Here's a sample of the dataset:

data = [(DT.datetime.strptime('2010-02-05', "%Y-%m-%d"), 123),
        (DT.datetime.strptime('2010-02-19', "%Y-%m-%d"), 678),
        (DT.datetime.strptime('2010-03-05', "%Y-%m-%d"), 987),
        (DT.datetime.strptime('2010-03-19', "%Y-%m-%d"), 345)]

Here is a runnable code sample using pyplot

import datetime as DT
from matplotlib import pyplot as plt

data = [(DT.datetime.strptime('2010-02-05', "%Y-%m-%d"), 123),
        (DT.datetime.strptime('2010-02-19', "%Y-%m-%d"), 678),
        (DT.datetime.strptime('2010-03-05', "%Y-%m-%d"), 987),
        (DT.datetime.strptime('2010-03-19', "%Y-%m-%d"), 345)]

x = [date for (date, value) in data]
y = [value for (date, value) in data]

fig = plt.figure()

graph = fig.add_subplot(111)
graph.plot_date(x,y)

plt.show()

QUESTION SUMMARY:
My situation is more like I have an Axes instance ready (referenced by graph in the code above) and I want to do the following:

  1. Make the xticks correspond to the exact date values. I have heard of matplotlib.dates.DateLocator but I have no idea how create one and then associate it with a specific Axes object.
  2. Get tighter control over the type of graph being used (bar, line, points, etc.)

解决方案

What you're doing is simple enough that it's easiest to just using plot, rather than plot_date. plot_date is great for more complex cases, but setting up what you need can be easily accomplished without it.

e.g., Based on your example above:

import datetime as DT
from matplotlib import pyplot as plt
from matplotlib.dates import date2num

data = [(DT.datetime.strptime('2010-02-05', "%Y-%m-%d"), 123),
        (DT.datetime.strptime('2010-02-19', "%Y-%m-%d"), 678),
        (DT.datetime.strptime('2010-03-05', "%Y-%m-%d"), 987),
        (DT.datetime.strptime('2010-03-19', "%Y-%m-%d"), 345)]

x = [date2num(date) for (date, value) in data]
y = [value for (date, value) in data]

fig = plt.figure()

graph = fig.add_subplot(111)

# Plot the data as a red line with round markers
graph.plot(x,y,'r-o')

# Set the xtick locations to correspond to just the dates you entered.
graph.set_xticks(x)

# Set the xtick labels to correspond to just the dates you entered.
graph.set_xticklabels(
        [date.strftime("%Y-%m-%d") for (date, value) in data]
        )

plt.show()

If you'd prefer a bar plot, just use plt.bar(). To understand how to set the line and marker styles, see plt.plot() Plot with date labels at marker locations http://www.geology.wisc.edu/~jkington/matplotlib_date_labels.png

这篇关于如何使用matplotlib在x轴上绘制特定日期的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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