Matplotlib日期在y轴上 [英] Matplotlib date on y axis

查看:171
本文介绍了Matplotlib日期在y轴上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在matplotlib中绘制一系列日落时间,但出现以下错误:
TypeError:空'DataFrame':没有要绘制的数字数据

I am trying to plot a series of sunset times in matplotlib but I get the following error: "TypeError: Empty 'DataFrame': no numeric data to plot"

我看过几种转换的方法,例如plt.dates.date2num,但这并不能真正满足我的需求,因为我想以一种可读的格式(即时间)进行打印。我发现的所有示例在x轴上都有时间,而在y轴上没有时间。

I have looked at several options to convert, e.g. plt.dates.date2num but that doesn't really fullfil my needs as i would like to plot it in a readable format, i.e. times. All examples I have found have times on the x-axis but non have them on the y-axis.

有没有办法完成此任务?有人知道吗?

Is there no way of accomplishing this task? Has anyone got an idea?

我非常期待您的答复。
最好的问候,阿恩

I am looking very forward to your replies. Best regards, Arne

3 Jan 2013          16:44:00
4 Jan 2013          16:45:00
5 Jan 2013          16:46:00
6 Jan 2013          16:47:00
7 Jan 2013          16:48:00
8 Jan 2013          16:49:00
9 Jan 2013          16:51:00
10 Jan 2013         16:52:00
11 Jan 2013         16:53:00
12 Jan 2013         16:55:00
13 Jan 2013         16:56:00
14 Jan 2013         16:57:00


推荐答案

从您的问题还不清楚,您是要在x轴上绘制一些未指定的数据,还是要在y轴上绘制日期/时间,或者是否要绘制

It's not quite clear from your question if you're trying to plot some unspecified data on the x-axis with date/time on the y-axis or if you're trying to plot days on the x-axis with times on the y-axis.

不过,从您的问题中,我将假定是后者。

From your question, though, I'm going to assume it's the latter.

听起来您可能正在使用 pandas ,但目前,我仅假设您有两个字符串序列:一个带日期,另一个带时间序列。

It sounds like you might be using pandas, but for the moment, I'll just assume you have two sequences of strings: One with the day, and another sequence with the time.

要将给定轴视为日期,只需调用 ax.xaxis_date () ax.yaxis_date()。在这种情况下,两个实际上都是日期。 (时间将以今天为一天,尽管您不会直接看到它。)

To treat a given axis as dates, just call ax.xaxis_date() or ax.yaxis_date(). In this case, both will actually be dates. (The times will have today as the day, though you won't see this directly.)

import matplotlib.pyplot as plt
import matplotlib.dates as mdates

date = ['3 Jan 2013', '4 Jan 2013', '5 Jan 2013', '6 Jan 2013', '7 Jan 2013',
        '8 Jan 2013', '9 Jan 2013', '10 Jan 2013', '11 Jan 2013', '12 Jan 2013',
        '13 Jan 2013', '14 Jan 2013']
time = ['16:44:00', '16:45:00', '16:46:00', '16:47:00', '16:48:00', '16:49:00',
        '16:51:00', '16:52:00', '16:53:00', '16:55:00', '16:56:00', '16:57:00']

# Convert to matplotlib's internal date format.
x = mdates.datestr2num(date)
y = mdates.datestr2num(time)

fig, ax = plt.subplots()

ax.plot(x, y, 'ro-')
ax.yaxis_date()
ax.xaxis_date()

# Optional. Just rotates x-ticklabels in this case.
fig.autofmt_xdate()
plt.show()

这篇关于Matplotlib日期在y轴上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆