简体   繁体   English

以日期时间为索引绘制数据框,仅在 python 的 x 轴上显示小时、分钟和秒

[英]plot dataframe with datetime as index and only display hours, minutes, and seconds on x-axis in python

I am able to produce the following plot with this code:我可以使用此代码生成以下图:

df_day['Time'] = pd.to_datetime(df_day['Time'], format = '%H:%M:%S')
df_day = df_day.set_index('Time')

fig=plt.figure(dpi=900)
plt.title("TESTER Summary for Day %i " %y + "\n Average Chamber Pressure %.3f [torr] \n" %p_avg_temp  + str(dates[x]))

ax1 = df_day['DP-2'].plot(label = 'DP-2')
ax2 = df_day['FM-1'].plot(secondary_y = True, label = "Flow Rate")

ax1.set_ylabel('Temperatures - Pressure Drop\nInlet Pressure - Scale Weight')

ax2.set_ylabel('Flow Rate - Heat Rej.')

ax1.set_xlabel('Time ')

handles,labels = [],[]
for ax in fig.axes:
    for h,l in zip(*ax.get_legend_handles_labels()):
        handles.append(h)
        labels.append(l)

plt.legend(handles, labels, loc = 'lower center', bbox_to_anchor = (0.5, -0.5), ncol = 3)
fig.subplots_adjust(bottom = 0.25)

ax1.grid(True, linestyle = ':')
ax2.grid(True, linestyle = ':')
plt.show()

在此处输入图像描述

But I don't want the date to show up in the xtick labels.但我不希望日期出现在 xtick 标签中。 I want hours, minutes and seconds.我想要小时、分钟和秒。 I have also tried:我也试过:

df_day['Time'] = pd.Series([val.time() for val in df_day['Time']])

instead of代替

df_day['Time'] = pd.to_datetime(df_day['Time'], format = '%H:%M:%S')

but then I get但后来我得到

在此处输入图像描述

and the time intervals are arbitrary and don't include seconds when the seconds are 00.并且时间间隔是任意的,当秒为 00 时不包括秒。

Try running this lines before you code:在编码之前尝试运行此行:

import matplotlib.dates as mdates

myFmt = mdates.DateFormatter('%H:%M:%S') # here you can format your datetick labels as desired
plt.gca().xaxis.set_major_formatter(myFmt)

And look at this post plot_date function set xticks for hourly data并查看此帖子plot_date function set xticks for hourly data

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python plot x 轴仅显示选择项 - Python plot x-axis display only select items 如何使用 Prophet 绘图功能在 x 轴上显示小时 - How to display Hours on the x-axis using Prophet plot function 如何在 Python 中将分钟时间设置为 Matplotlib plot 的 x 轴? - How to set minutes time as x-axis of a Matplotlib plot in Python? Python plot 具有 24 小时 x 和 y 轴,仅使用时间戳的小时和分钟 - Python plot with 24 hrs x and y axis using only hours and minutes from timestamp 散点图表单数据框,x 轴上有索引 - Scatter plot form dataframe with index on x-axis 使用 Pandas dataframe 索引作为 matplotlib plot 中 x 轴的值 - Using a Pandas dataframe index as values for x-axis in matplotlib plot Pandas 数据框 plot():x 轴日期标签显示但不显示数据 - Pandas dataframe plot(): x-axis date labels display but not data 如何格式化 Pandas / Matplotlib 图形,以便 x 轴刻度只有小时和分钟? - How to format Pandas / Matplotlib graph so the x-axis ticks are ONLY hours and minutes? 如何在 Python 中将字符串(X 小时 X 分钟 X 秒)转换为日期时间? - How to convert a string (X hours X minutes X seconds) to datetime in Python? 是否有一种简单的方法可以在 Python 中绘制和操作持续时间(小时/分钟/秒)数据? 非日期时间数据 - Is there an easy way to plot and manipulate time duration (hours/minutes/seconds) data in Python? NOT datetime data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM