堆叠柱形图
import matplotlib.pyplot as plt
# 设置支持中文
plt.rcParams['font.family'] = 'Microsoft YaHei'
# 国家数
countries = ['Germany', 'America', 'France', 'China', 'England']
# 总人数
accumulations = [78258, 215417, 57749, 82725, 29865]
current_confirmed = [58356, 201685, 42664, 2945, 27329]
accumulations_std = [int(_ / 5) for _ in accumulations]
current_confirmed_std = [int(_ / 5) for _ in current_confirmed]
width = 0.4
# 每个柱的宽度
p1 = plt.bar(countries, accumulations, width=width, yerr=accumulations_std)
p2 = plt.bar(countries, current_confirmed, width=width, bottom=accumulations, yerr=current_confirmed_std)
# 设置图标 y 轴描述
plt.ylabel('人数')
# 设置图标标题
plt.title('疫情感染人数')
# 设置柱颜色对应含义
plt.legend((p1[0], p2[0]), ('累积人数', '现有人数'))
plt.show()

staked_bar_graph.png
并列柱形图
import matplotlib.pyplot as plt
import numpy as np
# 设置支持中文
plt.rcParams['font.family'] = 'Microsoft YaHei'
# 国家数
countries = ['Germany', 'America', 'France', 'China', 'England']
# 总人数
accumulations = [78258, 215417, 57749, 82725, 29865]
current_confirmed = [58356, 201685, 42664, 2945, 27329]
accumulations_std = [int(_ / 5) for _ in accumulations]
current_confirmed_std = [int(_ / 5) for _ in current_confirmed]
ind = np.arange(len(countries))
# 设置柱宽度
width = 0.35
fig, ax = plt.subplots()
# accumulations 容器
bar_1 = ax.bar(ind - width / 2, accumulations, width, color='SkyBlue', label="累积")
# current_confirmed 容器
bar_2 = ax.bar(ind + width / 2, current_confirmed, width, color='IndianRed')
# 从容器中获取柱宽度、高度以及x轴的起始位置,然后在选择位置插入柱描述
for i in bar_1:
ax.text(i.get_x() + i.get_width() * 0.43, 1.01 * i.get_height(), '{}'.format(i.get_height()),
ha='right', va='bottom')
for j in bar_2:
ax.text(j.get_x() + j.get_width() * 0.57, 1.01 * j.get_height(), '{}'.format(j.get_height()),
ha='left', va='bottom')
ax.set_ylabel("人数")
ax.set_title("疫情感染人数")
ax.set_xticks(ind)
ax.set_xticklabels(countries)
plt.show()

grouped_bar_graph.png
水平柱形图
# 水平柱形图
import matplotlib.pyplot as plt
# 设置支持中文
plt.rcParams['font.family'] = 'Microsoft YaHei'
# 国家数
countries = ['Germany', 'America', 'France', 'China', 'England']
# 总人数
accumulations = [78258, 215417, 57749, 82725, 29865]
current_confirmed = [58356, 201685, 42664, 2945, 27329]
accumulations_std = [int(_ / 5) for _ in accumulations]
current_confirmed_std = [int(_ / 5) for _ in current_confirmed]
width = 0.4
# 每个柱的宽度
p1 = plt.barh(countries, accumulations, xerr=accumulations_std)
p2 = plt.barh(countries, current_confirmed, left=accumulations, xerr=current_confirmed_std)
# 设置图标 y 轴描述
plt.ylabel('人数')
# 设置图标标题
plt.title('疫情感染人数')
# 设置柱颜色对应含义
plt.legend((p1[0], p2[0]), ('累积人数', '现有人数'))
plt.show()

horizontal_bar_graph.png
破损柱形图
# 破损柱形图
import matplotlib.pyplot as plt
# 设置支持中文
plt.rcParams['font.family'] = 'Microsoft YaHei'
fig, ax = plt.subplots()
# 设置x开始点和长度以及 颜色
ax.broken_barh([(110, 30), (150, 10)], (10, 19), facecolors='blue')
ax.broken_barh([(10, 50), (100, 20), (130, 10)], (30, 19),
facecolors=('red', 'yellow', 'green'))
# x y 长度限定
ax.set_ylim(5, 35)
ax.set_ylim(0, 200)
# x 轴描述
ax.set_xlabel('开始时间(单位: s)')
# y 轴柱起点
ax.set_yticks([20, 40])
# y 轴柱标签
ax.set_yticklabels(['Bill', 'Jim'])
ax.grid(True)
# 增加注释
ax.annotate('比赛中断点', (61, 49), xytext=(0.8, 0.9), textcoords='axes fraction',
arrowprops=dict(facecolor='green', shrink=1), fontsize=16, horizontalalignment='right',
verticalalignment='top')
plt.show()

broken_bar_graph.png
分类变量图
# 分类变量图
import matplotlib.pyplot as plt
plt.rcParams['figure.figsize'] = (15.0, 8.0)
data = {'apple': 10, 'orange': 15, 'lemons': 5, 'limes': 20}
fruits = data.keys()
fruit_num = data.values()
# 设置画布列数以及画布总宽度及每个宽度
fig, axs = plt.subplots(1, 3, figsize=(9, 3))
# 子柱形图
axs[0].bar(fruits, fruit_num)
# 子散点图
axs[1].scatter(fruits, fruit_num)
# 子线图
axs[2].plot(fruits, fruit_num)
# 设置图片title及title位置
fig.suptitle("Categorical Plotting", y=1)
plt.show()

plotting_categorical_variables.png
网友评论