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)

二、效果图

matplotlib绘制柱状图和折线图_第1张图片

你可能感兴趣的:(matplotlib,python,开发语言)