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

发布于 2024-09-14 08:47:31 字数 1212 浏览 3 评论 0原文

我有一个由日期值对组成的数据集。我想将它们绘制在条形图中,其中 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 的可运行代码示例

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

问题摘要:
我的情况更像是我已经准备好了一个 Axes 实例(由上面代码中的 graph 引用),并且我想要执行以下操作:

  1. 制作 xticks code> 对应于确切的日期值。我听说过 matplotlib.dates.DateLocator,但我不知道如何创建一个并将其与特定的 Axes 对象关联。
  2. 更严格地控​​制所使用的图表类型(条形图、折线图、点图等)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南街九尾狐 2024-09-21 08:47:40

您所做的事情非常简单,最简单的方法是仅使用plot,而不是plot_date。 plot_date 对于更复杂的情况非常有用,但是没有它也可以轻松完成所需的设置。

例如,根据上面的示例:

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文