第二章 使用matplotlib绘制简单图表
1、使用plot()绘制折线图 2、使用bar()绘制柱形图或者堆积柱形图 3、使用barh()绘制条形图或者堆积条形图 4、使用stackplot()绘制堆积面积图 5、使用hist()绘制直方图 6、使用pie()绘制饼图或者圆环图 7、使用scatter()绘制散点图或者气泡图 8、使用boxplot()绘制箱型图 9、使用polar()绘制雷达图 10、使用errorbar()绘制误差图 以下是课堂练习图表
import numpy as np
import matplotlib.pyplot as plt
y = np.arange (5)
x1 = np.array ([10, 8, 7, 11, 13])
bar_height = 0.3
plt.barh (y,x1,tick_label=['a','b','c','d','e'],height=bar_height)
plt.title("2020080603048")
plt.show()

y = np.arange (5)
x1 = np.array ([10, 8, 7, 11, 13])
x2 = np.array ([9, 6, 5, 10, 12])
bar_height = 0.3
plt.barh (y,x1,tick_label=['a','b','c','d','e'],height=bar_height)
plt.barh (y+bar_height,x2,height=bar_height)
plt.title("2020080603048")
plt.show()

plt.barh (y,x1,tick_label=['a','b','c','d','e'],height=bar_height)
plt.barh (y,x2,left=x1,height=bar_height)
plt.title("2020080603048")
plt.show()

import numpy as np
import matplotlib.pyplot as plt
x = np.arange (5)
y1 = np.array ([10, 8, 7, 11, 13])
bar_width = 0.3
plt.bar(y,x1,tick_label=['a','b','c','d','e'],width=bar_width)
plt.title("2020080603048")
plt.show()

x = np.arange (5)
y1 = np.array ([10, 8, 7, 11, 13])
y2 = np.array ([9, 6, 5, 10, 12])
bar_width = 0.3
plt.bar(y,x1,tick_label=['a','b','c','d','e'],width=bar_width)
plt.bar(x+bar_width,y2,width=bar_width)
plt.title("2020080603048")
plt.show()

import numpy as np
import matplotlib.pyplot as plt
x=np.arange(6)
y1=np.array([1,4,3,5,6,7])
y2=np.array([1,3,4,2,7,6])
y3=np.array([3,4,3,6,5,5])
plt.stackplot(x,y1,y2,y3)
plt.title("2020080603048")
plt.show()

scores=np.random.randint(0,100,50)
plt.hist(scores,bins=8,histtype='stepfilled')
plt.title("2020080603048")
plt.show()

data=np.array([20,50,10,15,30,55])
pie_labels=np.array(['A','B','C','D','E','F'])
plt.pie(data,radius=1.5,labels=pie_labels,autopct='%3.1f%%',pctdistance=0.75)
plt.title("2020080603048")
plt.show()

data=np.array([20,50,10,15,30,55])
pie_labels=np.array(['A','B','C','D','E','F'])
plt.pie(data,radius=1.5,wedgeprops={'width':0.7},labels=pie_labels,autopct='%3.1f%%',pctdistance=0.75)
plt.title("2020080603048")
plt.show()

import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams['font.sans-serif']=['SimHei']
mpl.rcParams['axes.unicode_minus']=False
kinds=['购物','人情来往','餐饮美食','通信物流','生活用品','交通出行','休闲娱乐','其他']
money_scale=[800/3000,100/3000,1000/3000,200/3000,300/3000,200/3000,200/3000,200/3000]
dev_position=[0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,]
plt.pie(money_scale,labels=kinds,autopct='%3.1f%%',shadow=True,explode=dev_position, startangle=90)
plt.title("2020080603048")
plt.show()

num=50
x=np.random.rand(num)
y=np.random.rand(num)
plt.scatter(x,y)
plt.show()

plt.rcParams['font.family']='SimHei'
plt.rcParams['axes.unicode_minus']=False
dim_num=6
data=np.array([[0.40,0.32,0.35,0.30,0.30,0.88],
[0.85,0.35,0.30,0.40,0.40,0.30],
[0.43,0.89,0.30,0.8,0.22,0.30],
[0.30,0.25,0.45,0.85,0.45,0.40],
[0.34,0.31,0.38,0.40,0.92,0.28],
[0.34,0.31,0.38,0.40,0.92,0.28]]
)
angles=np.linspace(0,2 * np.pi,dim_num,endpoint=False)
angles=np.concatenate((angles,[angles[0]]))
data=np.concatenate((data,[data[0]]))
radar_labels=['研究(I)','艺术(A)','社会(S)','企业(E)','传统(C)','现实(R)']
radar_labels=np.concatenate((radar_labels,[radar_labels[0]]))
plt.polar(angles.data)
plt.thetagrids(angles * 180/np.pi,labels=radar_labels)
plt.fill(angles,data,alpha=0.25)
plt.title("2020080603048")
plt.show()

x=np.arange(5)
y=(25,32,34,20,25)
y_offset=(3,5,2,3,3)
plt.errorbar(x,y,yerr=y_offset,capsize=3,capthick=2)
plt.title("2020080603048")
plt.show()

|