[英]Change xaxis range values in a plot with matplotlib
I am doing a plot of dataframe df_no_missing. 我正在绘制数据帧df_no_missing的图。
df_no_missing.head()
TIMESTAMP datetime64[ns]
P_ACT_KW float64
PERIODE_TARIF object
P_SOUSCR float64
SITE object
TARIF object
depassement float64
dtype: object
Out[236]:
TIMESTAMP P_ACT_KW PERIODE_TARIF P_SOUSCR SITE TARIF depassement date time
2015-08-01 23:10:00 248.0 HC 425.0 ST GEREON TURPE_HTA5 0.0 2015-08-01 23:10:00
2015-08-01 23:20:00 244.0 HC 425.0 ST GEREON TURPE_HTA5 0.0 2015-08-01 23:20:00
2015-08-01 23:30:00 243.0 HC 425.0 ST GEREON TURPE_HTA5 0.0 2015-08-01 23:30:00
2015-08-01 23:40:00 238.0 HC 425.0 ST GEREON TURPE_HTA5 0.0 2015-08-01 23:40:00
2015-08-01 23:50:00 234.0 HC 425.0 ST GEREON TURPE_HTA5 0.0 2015-08-01 23:50:00
I did a plot representing the variation of P_ACT_KW and P_SOUSCR with TIMESTAMP. 我做了一个图,表示P_ACT_KW和P_SOUSCR随着TIMESTAMP的变化。 The python code is below : python代码如下:
fig = plt.figure(figsize=(11, 6), dpi=100)
ax = fig.add_subplot(111)
yearFmt = mdates.DateFormatter("%H:%M:%S")
ax.xaxis.set_major_formatter(yearFmt)
sns.set_style("darkgrid")
x = pd.to_datetime(df_no_missing.TIMESTAMP, format="%h:%m")
y = df_no_missing.P_ACT_KW
z = df_no_missing.P_SOUSCR
plt.plot(x, y, marker='o', label='P_SOUSCR')
plt.plot(x, z, marker='o', linestyle='--', color='g', label='P_ACT_KW')
plt.xlabel('temps')
plt.ylabel('puissance')
plt.title('variation de la puissance')
plt.legend()
plt.show()
I get a plot like this (the attached image) 我得到了这样的情节(附图)
My question is how can I show the timestamp in the xaxis, I mean I need the see the timestamp indicated in the dataframe for exampel here : 23:10:00, 23:20:00 , 23:30:00, 23:40:00, 23:50:00. 我的问题是如何在x轴上显示时间戳,我的意思是我需要查看数据帧中指示的时间戳,例如:23:10:00,23:20:00,23:30:00,23:40 :00,23:50:00 and not 01:00 , 04:00, 07:00 ... 而不是01:00,04:00,07:00 ...
Thank you if you can help me 谢谢你能帮我
Bests 贝斯茨
You can add set_major_locator
before set_major_formatter
: 您可以在set_major_locator
之前添加set_major_formatter
:
import matplotlib.dates as mdates
yearFmt = mdates.DateFormatter("%H:%M:%S")
minx = pd.to_datetime( '2015-08-01 23:10:00' )
minx = pd.to_datetime( '2015-08-01 23:50:00' )
ax.set_xlim( [ minx, maxx ] )
ax.xaxis.set_major_locator( mdates.MinuteLocator(byminute=range(0,60,10)) )
ax.xaxis.set_major_formatter( yearFmt )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.