matplotlib绘制柱状图

代码

import matplotlib.pyplot as plt
import numpy as np

# 数据
categories = ['denoise', 'double-digit', '100% 5R']
existence = [0.9778, 0.9768, 0.9767]
non_existence = [0.9772, 0.9767, 0.9778]

# 设置每组柱状图的宽度
bar_width = 0.25

# 计算每组柱状图的位置
x = np.arange(len(categories))

# 设置全局字体大小
plt.rcParams['font.size'] = 18

# 绘制柱状图
plt.bar(x, existence, width=bar_width, label='Apply')
plt.bar(x + bar_width, non_existence, width=bar_width, label="Not apply")

# 添加标题、标签和图例
plt.title('Transformer')
# plt.ylabel('Acc')
plt.xticks(x + bar_width / 2, categories)


plt.yticks([0.976, 0.977, 0.978], ["97.6%", "97.7%", "97.8%"])  # 设置Y轴刻度

plt.legend()
plt.ylim(0.974, 0.98)  # 必须再 legend() 后面

# 显示图表
plt.tight_layout()
plt.grid()
plt.show()

结果

matplotlib绘制柱状图_第1张图片

代码

import matplotlib.pyplot as plt

# 示例数据
categories = ['linear', 'conv1d+linear', 'reshape+linear']

# 绘制三个单独的柱状图,每个柱状图有两个标签
plt.bar(categories[0], 0.9778, label='1 channel', color = "blue", width=0.3)
plt.bar(categories[1], 0.9922, label='4 channel', color = "green", width=0.3)
plt.bar(categories[2], 0.9925, color = "green", width=0.3)

plt.yticks([0.9778, 0.9925], ["97.78%", "99.25%"])  # 设置Y轴刻度

# 添加图例
plt.legend(['1 channel', '4 channel'])
plt.ylim(0.95, 0.9950)  # 必须再 legend() 后面

# 显示图表
plt.tight_layout()
# 显示水平方向的网格线
plt.grid(axis='y')
plt.show()

结果

matplotlib绘制柱状图_第2张图片

你可能感兴趣的:(ECG,matplotlib)