其余图表类型
一、柱状图
基本属性:color、width
plt.bar(x, y, color = 'green', width = 0.7) # 柱状图并不会紧邻
plt.show()
- 使用text方法为每根柱子填上数字,由于一个text只能添加一条数据,所以应该使用for循环
- title、xlabel、ylabel使用方式与前面一致
plt.bar(x, y, color = ['black','red','green'], width = 0.9) # 传入多个颜色时会交替显示
for i, j in zip(x, y):
plt.text( i, j, j , ha = 'center', va = 'bottom') # 会遍历每根柱子,并添加上 y 值
plt.show()
facecolor作为面板颜色只能接受一个字符串,不能像color一样接受列表
1. 描边 - edgecolor
- edgecolor:边缘颜色,简写为 ec
- linestyle:边缘线条样式,简写为 ls
- linewidth:边缘宽度,简写为 lw
x = np.linspace(1, 10, 7)
y = np.linspace(1, 10, 7)
plt.bar(x, y, color = ['m', 'k', 'r'], edgecolor="black", linestyle = '-.',linewidth = 2 )
<BarContainer object of 7 artists>
2. 填充
x = np.linspace(1, 10, 7)
y = np.linspace(1, 10, 7)
plt.bar(x, y, color = ['m', 'k', 'r'], hatch = 'o')
<BarContainer object of 7 artists>
3. 堆叠
x = np.linspace(1, 10, 7)
y1 = np.linspace(1, 10, 7)
y2 = np.linspace(10,20, 7)
plt.bar(x, y1, label = 'y1') # label 配合 legend 使用
plt.bar(x, y2, label = 'y2', bottom = y1)
plt.legend()
plt.show()
4. 并列
x = np.linspace(1, 10, 7)
y1 = np.linspace(1, 10, 7)
y2 = np.linspace(10,20, 7)
width = 0.7
plt.bar(x, y1, width = width, label = 'y1')
plt.bar(x + width, y2, width = width,label = 'y2')
plt.xticks(x + width /2, ['A','B','C','D','E','F','G']) # 骑线显示标签
plt.legend()
plt.show()
二、饼状图
# 显示标签
x = [1, 2, 3, 4]
labels = ['one', 'two', 'three', 'four']
plt.pie(x, labels = labels)
plt.show()
# 显示数据
x = [1, 2, 3, 4]
plt.pie(x, labels = x)
plt.show()
# 显示占比
x = [1, 2, 3, 4]
plt.pie(x, autopct='%.2f%%')
plt.show()
三、散点图
plt.scatter(x, y)
plt.show()
x = np.random.rand(100)
y = np.random.rand(100)
plt.scatter(x, y)
plt.show()