直方图:
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure() #创建类
ax = fig.add_subplot() #给画布fig上添加坐标系风格的绘图区域ax
random_state = np.random.RandomState(19680801) #10000个随机数
random_x = random_state.randn(10000)
ax.hist(random_x, bins=25,color='orange') #25个橙色的矩形条的直方图
plt.show()
饼图:
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure() #创建类
ax = fig.add_subplot() #给画布fig上添加坐标系风格的绘图区域ax
data = np.array([20, 50, 10, 15, 30, 55])
pie_labels = np.array(['A', 'B', 'C', 'D', 'E', 'F'])
ax.pie(data, radius=1.5, labels=pie_labels, autopct='%.1f%%',startangle=90) #绘制饼图:半径为0.5,数值保留1为小数,调整90度
plt.show()
散点图:
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure() #创建类
ax = fig.add_subplot() #给画布fig上添加坐标系风格的绘图区域ax
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
#准备x轴和y轴的数据
x_speed = np.arange(10, 210, 10)
y_distance = np.array([0.5, 2.0, 4.4, 7.9, 12.3,
17.7, 24.1, 31.5, 39.9, 49.2,
59.5, 70.8, 83.1, 96.4, 110.7,
126.0, 142.2, 159.4, 177.6, 196.8])
area=np.linspace(20,210,20) #生成一个等距递增数列
ax.scatter(x_speed, y_distance, s=area, alpha=0.4) #s表示数据点大小,alpha表示透明度
plt.show()
误差棒图:
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure() #创建类
ax = fig.add_subplot() #给画布fig上添加坐标系风格的绘图区域ax
x = np.arange(5) #准备数据
y = (25, 32, 34, 20, 25)
y_offset = (3, 5, 2, 3, 3) #准备误差
ax.errorbar(x, y, yerr=y_offset, capsize=3, capthick=2,marker='*',linestyle='--') #capsize表示误差棒边界横杆的大小,capthick表示误差棒边界横杆的厚度
plt.show() # *做点, 折线型