将 Pandas DataFrame 和 xaxis 绘制为 Timestamp 生成空图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40023746/
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
Plotting of pandas DataFrame and xaxis as Timestamp produces empty plot
提问by philbox2
I have a pandas.DataFrame (df), which consists of some values and a datetime which is a string at first but which I convert to a Timestamp using
我有一个 pandas.DataFrame (df),它由一些值和一个日期时间组成,它最初是一个字符串,但我使用它转换为时间戳
df['datetime'] = pd.to_datetime(df['Time [dd.mm.yyyy hh:mm:ss.ms]'], format="%d.%m.%Y %H:%M:%S.%f")
It seems to work and I can access the new column's element's properties like obj.day and such. So the resulting column contains a Timestamp. When I try to plot this by using either pyplot.plot(df['datetime'],df['value_name'])
or df.plot(x='datetime',y='value_name')
,the picture below is the reslut. I tried converting the Timestamps using obj.to_pydatetime()
but that did not change anything. The dataframe itself is populated by some data coming from csvs. What confuses me, is that with a certain csvs it works but with others not. I am pretty sure that the conversion to Timestamps was successful but I could be wrong. Also my time window should be from 2015-2016 not from 1981-1700. If I try to locate the min and max Timestamp from the DataFrame, I get the right Timestamps in 2015 and 2016 respectively.
它似乎工作,我可以访问新列的元素的属性,如 obj.day 等。所以结果列包含一个时间戳。当我尝试使用pyplot.plot(df['datetime'],df['value_name'])
或绘制此图时df.plot(x='datetime',y='value_name')
,下图是 reslut。我尝试使用转换时间戳,obj.to_pydatetime()
但这并没有改变任何东西。数据框本身由来自 csvs 的一些数据填充。让我感到困惑的是,对于某些 csvs 它可以工作,但对于其他人则不行。我很确定转换为时间戳是成功的,但我可能是错的。另外我的时间窗口应该是 2015-2016 年而不是 1981-1700 年。如果我尝试从 DataFrame 中找到最小和最大时间戳,我会分别在 2015 年和 2016 年获得正确的时间戳。
Resulting Picture form pyplot.plot
Edit:
df.head()
gives:
编辑:
df.head()
给出:
Sweep Time [dd.mm.yyyy hh:mm:ss.ms] Frequency [Hz] Voltage [V]
0 1.0 11.03.2014 10:13:04.270 50.0252 230.529
1 2.0 11.03.2014 10:13:06.254 49.9515 231.842
2 3.0 11.03.2014 10:13:08.254 49.9527 231.754
3 4.0 11.03.2014 10:13:10.254 49.9490 231.678
4 5.0 11.03.2014 10:13:12.254 49.9512 231.719
扫描时间[DD.MM.YYYY HH:MM:ss.ms]频率[Hz]电压[V]
0 1.0 2014年3月11日10:13:04.270 50.0252 230.529
1 2.0 2014年3月11日10:13:06.254 49.9515 231.842
2 3.0 11.03.2014 10:13:08.254 49.9527 231.754
3 4.0 11.03.2014 10:13:10.254 49.9490 231.678
4 5.0.20151437.215.215.2014
datetime
0 2014-03-11 10:13:04.270
1 2014-03-11 10:13:06.254
2 2014-03-11 10:13:08.254
3 2014-03-11 10:13:10.254
4 2014-03-11 10:13:12.254
日期时间
0 2014年3月11日10:13:04.270
1 2014年3月11日10:13:06.254
2 2014年3月11日10:13:08.254
3 2014年3月11日10:13:10.254
4 2014-03- 11 10:13:12.254
and df.info()
gives:
并df.info()
给出:
<class 'pandas.core.frame.DataFrame'>
Int64Index: 33270741 entries, 0 to 9140687
Data columns (total 5 columns):
Sweep float64
Time [dd.mm.yyyy hh:mm:ss.ms] object
Frequency [Hz] float64
Voltage [V] float64
datetime datetime64[ns]
dtypes: datetime64[ns](1), float64(3), object(1)
memory usage: 1.5+ GB
I am trying to plot 'Frequency [Hz]'vs 'datetime'.
我正在尝试绘制“频率 [Hz]”与“日期时间”的关系图。
回答by jezrael
I think you need set_index
and then set formatting of both axis:
我认为您需要set_index
然后设置两个轴的格式:
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
df['datetime'] = pd.to_datetime(df['Time [dd.mm.yyyy hh:mm:ss.ms]'],
format="%d.%m.%Y %H:%M:%S.%f")
print (df)
df.set_index('datetime', inplace=True)
ax = df['Frequency [Hz]'].plot()
ticklabels = df.index.strftime('%Y-%m-%d')
ax.xaxis.set_major_formatter(ticker.FixedFormatter(ticklabels))
ax.yaxis.set_major_formatter(ticker.FormatStrFormatter('%.2f'))
plt.show()