修改python plot折線圖的座標軸刻度方法

今天小編就爲大家分享一篇修改python plot折線圖的座標軸刻度方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

修改python plot折線圖的座標軸刻度,這裏修改爲整數:

python plot折線圖的座標軸刻度

代碼如下:

from matplotlib import pyplot as plt
import matplotlib.ticker as ticker
import numpy as np


def std_plot():
 overall_std = [34.369, 21.366, 16.516, 11.151]
 max_std = [36.769, 21.794, 14.390, 4.684]
 plt.figure()
 plt.plot(overall_std, label='average_std')

 plt.plot(max_std, label='max_std')
 plt.legend()
 plt.xlabel('window')
 plt.ylabel('std')
 plt.xticks(range(len(max_std)))
 # plt.gca().xaxis.set_major_formatter(ticker.FormatStrFormatter('%1.1f'))

 plt.show()

std_plot()

可以發現,通過上面的方法可以自定義x軸的刻度顯示爲其他樣式,比如根據時間顯示。只需要修改爲:

plt.xticks(pd.date_range(‘2014-09-01','2014-09-30'),rotation=90)#設置時間標籤顯示格式

如果希望保留小數點後一位,可以這樣:

python plot折線圖的座標軸刻度

from matplotlib import pyplot as plt
import matplotlib.ticker as ticker
import numpy as np


def std_plot():
 overall_std = [34.369, 21.366, 16.516, 11.151]
 max_std = [36.769, 21.794, 14.390, 4.684]
 plt.figure()
 plt.plot(overall_std, label='average_std')

 plt.plot(max_std, label='max_std')
 plt.legend()
 plt.xlabel('window')
 plt.ylabel('std')
 # plt.xticks(range(len(max_std)))
 plt.gca().xaxis.set_major_formatter(ticker.FormatStrFormatter('%1.1f'))

 plt.show()


std_plot()

以上這篇修改python plot折線圖的座標軸刻度方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持神馬文庫。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章