Python motplotlib双Y轴数据对比

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
def create_picture():
    data = {'a': range(10),
            'b': range(4, 14),
            'c': range(2,12),
            'd': range(12, 22)}

    a = [x for x in data['a']]
    b = [x for x in data['b']]
    c = [x for x in data['c']]
    d = [x for x in data['d']]

    image_path = os.path.join(os.getcwd(), '{}.png'.format('data'))
    fig, ax = plt.subplots(figsize=(10, 5), dpi=600)

    ax.set(ylim=(0, max(a)), xlabel='point', ylabel='Value', title='TEST')
    ax.plot(range(len(a)), a, '-', label='a', color='red')
    ax2 = ax.twinx()
    ax2.plot(range(len(b)), b, '-', label='b', markersize=1, color='blue')
    ax2.plot(range(len(c)), c, '-', label='c', markersize=1, color='green')
    ax2.plot(range(len(d)), d, '-', label='d', markersize=1, color='purple')

    lines = ax.get_lines() + ax2.get_lines()
    labels = [line.get_label() for line in lines]
    ax.legend([lines[0]], [labels[0]], loc='upper left')

    ax2.legend(lines[1:], labels[1:], loc='upper right')

    ax.ticklabel_format(style='plain')
    plt.savefig(image_path, format='png', dpi=600)
    plt.close()

    print(f"Image saved to {image_path}")


if __name__ == '__main__':
    create_picture()

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值