如何使用 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
的可运行代码示例
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 引用),并且我想要执行以下操作:
- 制作 xticks code> 对应于确切的日期值。我听说过 matplotlib.dates.DateLocator,但我不知道如何创建一个并将其与特定的 Axes 对象关联。
- 更严格地控制所使用的图表类型(条形图、折线图、点图等)
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:
- Make the
xticks
correspond to the exact date values. I have heard ofmatplotlib.dates.DateLocator
but I have no idea how create one and then associate it with a specificAxes
object. - Get tighter control over the type of graph being used (bar, line, points, etc.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您所做的事情非常简单,最简单的方法是仅使用plot,而不是plot_date。 plot_date 对于更复杂的情况非常有用,但是没有它也可以轻松完成所需的设置。
例如,根据上面的示例:
如果您更喜欢条形图,只需使用
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:
If you'd prefer a bar plot, just use
plt.bar()
. To understand how to set the line and marker styles, seeplt.plot()
Plot with date labels at marker locations http://www.geology.wisc.edu/~jkington/matplotlib_date_labels.png