利用Python matplotlib包來畫圖
Python matplotlib包可以畫各種類型的圖,功能非常齊全。
# 曲線圖:matplotlib.pyplot.plot
# 柱狀圖:matplotlib.pyplot.hist
# 散點圖:matplotlib.pyplot.scatter
# 箱式圖:matplotlib.pyplot.boxplot
import matplotlib as mpl
from matplotlib import pyplot as plt
import seaborn as sns
import pandas as pd
#曲線圖執行個體
data = pd.read_csv('/data.csv', encoding = 'gbk') #資料中有中文字元時,讀取要加gbk
x = np.arange(30) + 1
y = data.iloc[0][1:31]
plt.plot(x,y, color = 'blue',linewidth = 1.5, linestyle = '-',marker = '.',label = 'mean')
#坐标軸設定
ax = plt.subplot(111)
ax.spines['right'].set_color('none') #去掉右邊的邊框線
ax.spines['top'].set_color('none') #去掉上邊的邊框線
#X軸從底部向上移動
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data',23))
#Y軸從左邊向右邊移動
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data',0))
#設定橫縱坐标的取值範圍
plt.xlim(x.min()*0.9, x.max()*1.1)
plt.ylim(y.min()*0.9, y.max()*1.1)
#設定橫縱坐标的刻度值
plt.xticks([0,5,10,15,20,25,30],['0','5','10','15','20','25','30'])
plt.yticks([25,30,35,40,45],['25','30','35','40','45'])
#特殊點添加注解
plt.scatter([23,],[y[22],],50,color = 'blue') #需要注解的點的位置,放大該點
plt.annotate('holiday', xy= (23,y[22]),xytext = (24,30.2), fontsize = 14, color = 'm', arrowprops = dict(arrowstyle = '->',
connectionstyle = 'arc3, rad = 0.1',color = 'm')) #給該點進行注解,先設定注解箭頭的位置,然後設定注解的位置
#設定标題,橫縱軸的名稱
plt.title('Title', fontsize = 12)
plt.xlabel('Time',fontsize = 12, labelpad = 6)
plt.ylabel('Numble',fontsize = 12, labelpad = 6)
#設定圖例及位置
plt.legend(loc = 'best')
#顯示網格線
plt.grid(True)
plt.show()
#柱狀圖
# x = np.random.normal(size=1000)
# plt.hist(x, bins=10) #bins設定柱狀圖中柱的數量
# plt.show()
#散點圖
# x = np.random.normal(size=1000)
# y = np.random.normal(size=1000)
# plt.scatter(x,y)
# plt.show()
#箱式圖
# x = np.random.normal(size=1000)
# plt.boxplot(x) #分布情況,異常點等
# plt.show()
根據自己的需求更改設定,不需要的可以去掉。