在独立轴上绘制时间
我有一个格式为 (HH:MM:SS.mmmmmm) 的时间戳数组和另一个浮点数数组,每个数组对应于时间戳数组中的一个值。
我可以使用 Matplotlib 在 x 轴上绘制时间并在 y 轴上绘制数字吗?
我试图这样做,但不知何故它只接受浮点数数组。我怎样才能让它绘制时间?我必须以任何方式修改格式吗?
I have an array of timestamps in the format (HH:MM:SS.mmmmmm) and another array of floating point numbers, each corresponding to a value in the timestamp array.
Can I plot time on the x axis and the numbers on the y-axis using Matplotlib?
I was trying to, but somehow it was only accepting arrays of floats. How can I get it to plot the time? Do I have to modify the format in any way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
更新:
此答案已过时自 matplotlib 版本 3.5 起。
plot
函数现在直接处理日期时间数据。请参阅 https://matplotlib.org/3.5.1/api /_as_gen/matplotlib.pyplot.plot_date.html旧的、过时的答案:
您必须首先将时间戳转换为 Python
datetime
对象(使用datetime.strptime
)。然后使用 date2num 将日期转换为 matplotlib 格式。使用
plot_date
绘制日期和值:Update:
This answer is outdated since matplotlib version 3.5. The
plot
function now handles datetime data directly. See https://matplotlib.org/3.5.1/api/_as_gen/matplotlib.pyplot.plot_date.htmlOld, outdated answer:
You must first convert your timestamps to Python
datetime
objects (usedatetime.strptime
). Then usedate2num
to convert the dates to matplotlib format.Plot the dates and values using
plot_date
:您还可以使用 pyplot.plot (从字符串表示形式解析它们之后)。 (使用 matplotlib 版本 1.2.0 和 1.3.1 进行测试。)
示例:
结果图像:
这里与散点图:
生成与此类似的图像:
You can also plot the timestamp, value pairs using pyplot.plot (after parsing them from their string representation). (Tested with matplotlib versions 1.2.0 and 1.3.1.)
Example:
Resulting image:
Here's the same as a scatter plot:
Produces an image similar to this:
7 年后,这段代码对我很有帮助。
然而,我的时间仍然没有正确显示。
使用 Matplotlib 2.0.0,我必须从 在 matplotlib 中编辑 x 轴刻度标签的日期格式 作者:Paul H。
我将格式更改为 (%H:%M) 并且时间显示正确。
感谢社区。
7 years later and this code has helped me.
However, my times still were not showing up correctly.
Using Matplotlib 2.0.0 and I had to add the following bit of code from Editing the date formatting of x-axis tick labels in matplotlib by Paul H.
I changed the format to (%H:%M) and the time displayed correctly.
All thanks to the community.
我在使用 matplotlib 版本时遇到了问题:2.0.2。运行上面的示例,我得到了一组居中堆叠的气泡。
我通过添加另一行“修复”了问题:
整个代码片段变为:
这会产生一个气泡根据需要分布的图像。
I had trouble with this using matplotlib version: 2.0.2. Running the example from above I got a centered stacked set of bubbles.
I "fixed" the problem by adding another line:
The entire code snippet becomes:
This produces an image with the bubbles distributed as desired.
尚未提及 Pandas 数据框。我想展示这些如何解决我的日期时间问题。我的日期时间精确到毫秒
2021-04-01 16:05:37
。我正在从 /proc 中提取 linux/haproxy 吞吐量,这样我就可以按照自己喜欢的方式对其进行格式化。这对于将数据输入实时图形动画非常有用。看一下 csv。 (忽略我在另一个图中使用的每秒数据包列)
通过使用
print(dataframe.dtype)
我可以看到数据是如何读入的:Pandas 将日期字符串作为“对象” ",这只是 char 类型。在脚本中按原样使用此内容:
Matplotlib 渲染所有毫秒时间数据。我添加了
plt.xticks(rotation=45)
来倾斜日期,但这不是我想要的。我可以将日期“对象”转换为 datetime64[ns]。哪个 matplotlib 确实知道如何渲染。这次我的日期类型为
datetime64[ns]
相同的脚本,但有 1 行差异。
这可能不适合您的用例,但可能对其他人有帮助。
Pandas dataframes haven't been mentioned yet. I wanted to show how these solved my datetime problem. I have datetime to the milisecond
2021-04-01 16:05:37
. I am pulling linux/haproxy throughput from /proc so I can really format it however I like. This is nice for feeding data into a live graph animation.Here's a look at the csv. (Ignore the packets per second column I'm using that in another graph)
By using
print(dataframe.dtype)
I can see how the data was read in:Pandas pulls the date string in as "object", which is just type char. Using this as-is in a script:
Matplotlib renders all the milisecond time data. I've added
plt.xticks(rotation=45)
to tilt the dates but it's not what I want. I can convert the date "object" to a datetime64[ns]. Which matplotlib does know how to render.This time my date is type
datetime64[ns]
Same script with 1 line difference.
This might not have been ideal for your usecase but it might help someone else.