如何使用matplotlib在x轴上绘制特定日期的数据?

时间:2021-08-14 01:21:24

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.

我的问题是matplotlib在整个日期范围内分发xtick;并利用点绘制数据。

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

日期都是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)]

Here is a runnable code sample using pyplot

下面是一个使用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. 使xtick与确切的日期值相对应。我听说过matplotli .date。但我不知道如何创建一个DateLocator,然后将它与特定的坐标轴对象关联起来。
  3. Get tighter control over the type of graph being used (bar, line, points, etc.)
  4. 对所使用的图形类型(bar、line、points等)进行更严格的控制。

1 个解决方案

#1


28  

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.

您所做的非常简单,只需使用plot,而不是plot_date。plot_date对于更复杂的情况来说是很好的,但是如果没有它,设置您所需要的东西是很容易的。

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

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

#1


28  

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.

您所做的非常简单,只需使用plot,而不是plot_date。plot_date对于更复杂的情况来说是很好的,但是如果没有它,设置您所需要的东西是很容易的。

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

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