matplotlib绘制柱状图和折线图

一、双柱状图 + 折线图源代码

import matplotlib
from matplotlib import pyplot as plt
from matplotlib.ticker import FuncFormatter
from matplotlib import ticker
from matplotlib.font_manager import FontManager
from matplotlib.font_manager import FontProperties

# 双柱状图 + 折线图
def bar_line():
    # x坐标相同
    x = ['A', 'B', 'C', 'D']
    y1 = [6.00, 3.88, 4.83, 6.02]
    y2 = [5.88, 4.07, 5.00, 5.99]
    y3 = [0.019, 0.017, 0.012, 0.010]
    # 引入系统中的字体(黑体)
    font = FontProperties(fname=r"C:\Windows\Fonts/simhei.ttf", size=10)

    # 解决导出图为空图层的问题
    matplotlib.use('TkAgg')
    # 解决中文乱码问题,并设置字体
    plt.rcParams['axes.unicode_minus'] = False
    plt.rcParams['font.family'] = ['SimHei']
    plt.rcParams['font.sans-serif'] = ['SimHei']
    plt.rc('font', family='SimHei', size=10)
    fontsize = 10

    # 调整画布大小 宽4.5 高2.2
    plt.rcParams['figure.figsize'] = (4.5, 2.2)

    # 柱状图柱子宽度
    bar_width = 0.2

    # x轴刻度位置 | 折线图x位置
    x_ticks = range(len(x))
    # 柱状图1 - x位置
    bar_1_x = [ii - bar_width + 0.05 for ii in x_ticks]
    # 柱状图1 - x位置
    bar_2_x = [ww + bar_width - 0.05 for ww in x_ticks]

    # 绘制双Y轴
    fig, ax1 = plt.subplots()
    # 设置x轴和y轴刻度字体
    labels1 = ax1.get_xticklabels() + ax1.get_yticklabels()
    [label.set_fontname('SimHei') for label in labels1]

    # 绘制柱状图1
    ax1.bar(bar_1_x, y1, lw=0.4, color="#9cd79c", edgecolor="k", label="非可信推理", width=bar_width)
    for a, b in zip(bar_1_x, y1):
        ax1.text(a-0.05, b + 0.05, '%.2f' % b, ha='center', va='bottom', fontsize=fontsize, font=font)

    # 绘制柱状图2
    # hatch设置填充内容,*3用于设置密度
    ax1.bar(bar_2_x, y2, lw=0.4, color="#ffff99", edgecolor="k", label="可信推理", width=bar_width, hatch='/'*3)
    for a, b in zip(bar_2_x, y2):
        ax1.text(a+0.05, b + 0.05, '%.2f' % b, ha='center', va='bottom', fontsize=fontsize, font=font)
    # 设置y轴的刻度范围
    ax1.set_ylim(0, 8)
    # 设置y轴label
    ax1.set_ylabel("推理时延(秒)", fontsize=fontsize, font=font)
    # 设置图表 loc/bbox_to_anchor-位置参数, borderaxespad-间隙, prop-设置字体
    ax1.legend(loc=3, bbox_to_anchor=(0, 1), borderaxespad=0.2, prop=font)

    # 绘制双Y轴
    ax2 = ax1.twinx()
    # 设置x轴和y轴刻度字体
    labels2 = ax2.get_xticklabels() + ax2.get_yticklabels()
    [label.set_fontname('SimHei') for label in labels2]

    # 绘制折线图
    ax2.plot(x, y3, 'o-', color="#04C0FF", label=" 可信验证开销")
    for a, b in zip(x, y3):
        ax2.text(a, b - 0.004, '{:.1f}%'.format(b*100), ha='center', va='bottom', fontsize=fontsize, font=font)
    # 设置y轴显示的数值区间
    ax2.set_ylim(0, 0.04)
    # y轴为百分比形式
    ax2.yaxis.set_major_formatter(ticker.PercentFormatter(xmax=1, decimals=1))
    ax2.set_ylabel("时间开销占比", fontsize=fontsize)
    ax2.legend(loc=3, bbox_to_anchor=(0.6, 1), borderaxespad=0.2, prop=font)

    plt.xticks(x_ticks, labels=list(x))
    # plt.figure(figsize=(4.5, 2.2))
    plt.tight_layout()
    plt.show()


if __name__ == '__main__':
    bar_line()

    # 查看电脑上的字体
    # fm = FontManager()
    # mat_fonts = set(f.name for f in fm.ttflist)
    # print('我的电脑上能显示中文的字体有:', mat_fonts)

二、效果图

在这里插入图片描述

  • 2
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

it00zyq

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值