python製作多種常用統計圖

一:線形圖

import matplotlib.pyplot as plt 
import numpy as np 
import seaborn as sns 
import warnings ****#忽略告警信息****
warnings.filterwarnings("ignore") ****#忽略告警信息****
x = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15] 
y = [12,13,15,12,14,24,35,32,27,29,21,27,26,21,10] 

plt.figure(figsize=(15,5)) #設置面板大小
plt.plot(x,y) 
plt.title('Fifteen days of weather change') #標題
plt.xlabel('date') 
plt.ylabel('℃') 
plt.show() 

在這裏插入圖片描述
二:直方圖+線狀圖

import numpy as np
import matplotlib.pyplot as plt
x = np.array([1,2,3,4,5,6,7,8])
y = np.array([4,5,6,12,2,3,10,15])
plt.plot(x,y,'b',lw=2)#x軸,y軸,線條顏色b:blue藍色
x = np.array([1,2,3,4,5,6,7,8])
y = np.array([15,13,27,36,11,16,20,15])
plt.bar(x,y,0.2,alpha=0.5,color='r')
plt.show()

在這裏插入圖片描述
三:柱狀圖

y1 = [12,14,25,12,14,24,35,34,21,29,24,28,20,21,10] 
y2 = [24,25,22,25,21,24,10,27,26,25,28,29,23,21,27] 
x1 = [0.25,1.25,2.25,3.25,4.25,5.25,6.25,7.25,8.25,9.25,10.25,11.25,12.25,13.25,14.25] 
x2 = [0.75,1.75,2.75,3.75,4.75,5.75,6.75,7.75,8.75,9.75,10.75,11.75,12.75,13.75,14.75] 
 
plt.figure(figsize=(10,5))
plt.bar(x1,y1,width = 0.5,label = 'city A') 
plt.bar(x2,y2,width = 0.5,label = 'city B') 
plt.title('Fifteen days of weather change') 
plt.xlabel('date') 
plt.ylabel('℃') 
plt.legend() 
plt.show() 

在這裏插入圖片描述
四:點狀圖

plt.figure(figsize=(10,5))
plt.scatter(x,y) 
plt.title('Fifteen days of weather change') 
plt.xlabel('date') 
plt.ylabel('℃') 
plt.show() 

在這裏插入圖片描述

x = range(20) 
y = x + np.random.randn(20)*1.5  
plt.figure(figsize=(10,5)) 
plt.plot(x,y,'*') 
plt.plot(x,x) 
plt.title('x VS y') 
plt.xlabel('X') 
plt.ylabel('Y') 
plt.legend(('real data','fitted line')) 
plt.show() 

在這裏插入圖片描述五:盒狀圖

y1 = [123,144,225,132,154,224,35,341,241,295,243,28,230,211,110] 
y2 = [124,225,221,252,231,243,102,217,216,25,238,229,23,21,127] 
plt.figure(figsize=(10,5)) 
plt.boxplot([y1,y2]) 
plt.xticks([1,2],['A','B'])
plt.xlabel('different city')
plt.show() 

在這裏插入圖片描述

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