[英]Change xaxis range values in a plot with matplotlib
我正在繪制數據幀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
我做了一個圖,表示P_ACT_KW和P_SOUSCR隨着TIMESTAMP的變化。 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()
我的問題是如何在x軸上顯示時間戳,我的意思是我需要查看數據幀中指示的時間戳,例如:23:10:00,23:20:00,23:30:00,23:40 :00,23:50:00 而不是01:00,04:00,07:00 ...
謝謝你能幫我
貝斯茨
您可以在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.