Python 如何使用 matplotlib 在 x 轴上针对特定日期绘制数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3486121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to plot data against specific dates on the x-axis using matplotlib
提问by Kit
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.
我有一个由日期值对组成的数据集。我想用 x 轴中的特定日期将它们绘制在条形图中。
My problem is that matplotlibdistributes the xticksover the entire date range; and also plots the data using points.
我的问题是在整个日期范围内matplotlib分布xticks;并使用点绘制数据。
The dates are all datetimeobjects. 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 Axesinstance ready (referenced by graphin the code above) and I want to do the following:
问题摘要:
我的情况更像是我Axes准备好了一个实例(graph在上面的代码中引用),我想做以下事情:
- Make the
xtickscorrespond to the exact date values. I have heard ofmatplotlib.dates.DateLocatorbut I have no idea how create one and then associate it with a specificAxesobject. - Get tighter control over the type of graph being used (bar, line, points, etc.)
- 使
xticks对应于确切的日期值。我听说过,matplotlib.dates.DateLocator但我不知道如何创建一个,然后将其与特定Axes对象相关联。 - 更严格地控制所使用的图形类型(条形、线形、点等)
采纳答案by Joe Kington
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
如果您更喜欢条形图,只需使用plt.bar(). 要了解如何设置线条和标记样式,请参阅在标记位置使用日期标签绘图 http://www.geology.wisc.edu/~jkington/matplotlib_date_labels.pngplt.plot()

