python制作多种常用统计图
程序员文章站
2022-04-27 12:17:27
...
一:线形图
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()
下一篇: Scipy_常见随机分布