Matplotlib 02 - Else Items

160 阅读1分钟

其余图表类型

一、柱状图

基本属性:color、width

 plt.bar(x, y, color = 'green', width = 0.7) # 柱状图并不会紧邻
 plt.show()

output_41_0.png

  • 使用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()

output_43_0.png

facecolor作为面板颜色只能接受一个字符串,不能像color一样接受列表

1. 描边 - edgecolor

  1. edgecolor:边缘颜色,简写为 ec
  2. linestyle:边缘线条样式,简写为 ls
  3. 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>

output_46_1.png

2. 填充

填充样式.PNG

 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>

output_48_1.png

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()

output_50_0.png

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()

output_52_0.png

二、饼状图

 # 显示标签
 x = [1, 2, 3, 4]
 labels = ['one', 'two', 'three', 'four']
 plt.pie(x, labels = labels)
 plt.show()

output_54_0.png

 # 显示数据
 x = [1, 2, 3, 4]
 plt.pie(x, labels = x)
 plt.show()

output_55_0.png

 # 显示占比
 x = [1, 2, 3, 4]
 plt.pie(x, autopct='%.2f%%')
 plt.show()

output_56_0.png

三、散点图

 
 plt.scatter(x, y)
 plt.show()

output_57_0.png

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

output_58_0.png