基本函数

1
2
bar(x, height, [width], **kwargs)   #竖条形图
barh(x, height, [width], **kwargs) #横条形图

x:数据标签(横坐标);
height:个数或一个数组,条形的高度;
[width]:可选参数,一个数或一个数组,条形的宽度,默认为 0.8

竖条形图

1
2
3
4
5
6
7
8
9
10
11
import matplotlib.pyplot as plt
# 解决plt中文显示问题
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

x = ('A', 'B', 'C', 'D', 'E')
y = [1, 2, 3, 4, 5]

plt.bar(x, y)
plt.title('结果')
plt.show()

横条形图

1
2
3
4
5
6
7
8
9
10
11
import matplotlib.pyplot as plt
# 解决plt中文显示问题
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

x = ('A', 'B', 'C', 'D', 'E')
y = [1, 2, 3, 4, 5]

plt.barh(x, y)
plt.title('结果')
plt.show()

并列条形图

两个类别并列条形图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

# 数据
x = ('A', 'B', 'C', 'D', 'E')
y1 = [10, 12, 8, 6, 7]
y2 = [6, 7, 8, 9, 10]

bar_width = 0.2 # 条形宽度
index_y1 = np.arange(len(x)) # y1条形图的横坐标
index_y2 = index_y1 + bar_width # y2条形图的横坐标

# 使用两次 bar 函数画出两组条形图
plt.bar(index_y1, height=y1, width=bar_width, color='#499c9f', label='y1')
plt.bar(index_y2, height=y2, width=bar_width, color='#c76813', label='y2')

plt.legend() #图例
plt.xticks(index_y1 + bar_width/2, x) # 标签+位置
plt.ylabel('数量') # 纵坐标轴标题
plt.title('结果') # 图形标题

plt.show()

多个类别并列条形图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import matplotlib.pyplot as plt
import numpy as np

# 自定义x轴标签
x_labels = ['Jan', 'Feb', 'Mar', 'Apr', 'May']

# 数据
categories = ['Category 1', 'Category 2', 'Category 3', 'Category 4', 'Category 5']
values = np.array([[15, 25, 35, 45, 55],
[20, 30, 40, 50, 60],
[25, 35, 45, 55, 65],
[30, 40, 50, 60, 70],
[35, 45, 55, 65, 75]])

# 设置每个柱形的宽度和间隔
bar_width = 0.15
month_gap = 0.3 # 月份之间的间隔
bar_positions = np.arange(len(x_labels)) * (len(categories) * bar_width + month_gap)

# 绘图
for i, category in enumerate(categories):
plt.bar(bar_positions + i * bar_width, values[i], width=bar_width, label=category)

# 添加标题和标签
plt.title('Monthly Comparison Bar Chart')
plt.xlabel('Months')
plt.ylabel('Values')
plt.xticks(bar_positions + (len(categories) - 1) * bar_width / 2, x_labels) # 设置x轴刻度标签,并居中对齐

# 添加图例
plt.legend()

# 显示图形
plt.show()

添加标签

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import matplotlib.pyplot as plt
import numpy as np

plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

# 数据
x = ('A', 'B', 'C', 'D', 'E')
y1 = [10, 12, 8, 6, 7]
y2 = [6, 7, 8, 9, 10]

bar_width = 0.3 # 条形宽度
index_y1 = np.arange(len(x)) # y1条形图的横坐标
index_y2 = index_y1 + bar_width # y2条形图的横坐标

# 使用两次 bar 函数画出两组条形图
plt.bar(index_y1, height=y1, width=bar_width, color='#499c9f', label='y1')
plt.bar(index_y2, height=y2, width=bar_width, color='#c76813', label='y2')

index = np.arange(len(y1))
for a,b in zip(index,y1): #柱子上的数字显示
plt.text(a*1.02,b*1.02,'%.2f'%b,ha='center',va='bottom',fontsize=7);
for a,b in zip(index+bar_width*0.85,y2):
plt.text(a*1.02,b*1.02,'%.2f'%b,ha='center',va='bottom',fontsize=7);

plt.legend() # 显示图例
plt.xticks(index_y1 + bar_width/2, x) # 让横坐标轴刻度显示 waters 里的饮用水, index_male + bar_width/2 为横坐标轴刻度的位置
plt.ylabel('数量') # 纵坐标轴标题
plt.title('结果') # 图形标题

plt.show()

堆叠柱形图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

x = ('A', 'B', 'C', 'D', 'E')
y1 = [10, 12, 8, 6, 7]
y2 = [6, 7, 8, 9, 10]

bar_width = 0.3 # 条形宽度

plt.bar(x, y1, bar_width, color = '#A65F58', label = 'y1')
plt.bar(x, y2, bar_width, bottom = y1, # 堆叠在第一个上方
color = '#99886B', label = 'y2')

plt.legend()