二、Python开发——matplotlib画图x轴任意尺寸拉长

matplotlib画图x轴任意尺寸拉长:

我们在使用python的matplotlib进行绘图时,有时会发现画出的图片x轴跟y轴是等比例的,如下图1所示;但是有时我们需要看到x轴上非常多的数据信息,因此需要拉长x轴的需求。具体python实现方法见下,效果见下图2所示:
在这里插入图片描述
图1

import matplotlib.pyplot as plt

def demo_plot(x, y, x_maxsize, save_path):
    plt.figure()
    
    plt.plot(x, y)
    #plt.ylim((0, 1000))
    plt.title("Demo")
    plt.xlabel("x")
    plt.ylabel("y")
    
    # change x internal size
    plt.gca().margins(x=0)
    plt.gcf().canvas.draw()
    
    # set size
    maxsize = x_maxsize
    m = 0.2
    N =len(x)
    s = maxsize / plt.gcf().dpi * N + 2 * m
    margin = m / plt.gcf().get_size_inches()[0]
    
    plt.gcf().subplots_adjust(left=margin, right=1. - margin)
    plt.gcf().set_size_inches(s, plt.gcf().get_size_inches()[1])

    plt.savefig("%s%s.jpg"%(save_path, "Demo"), bbox_inches='tight')
    plt.close()

if __name__ == '__main__':
    x = [500,1000,1500,2000,2500,3000]
    y = [100,200,300,400,500,600]
    demo_plot(x, y, 200, 'C:/Users/xt/Desktop/')

生成的效果图如下:
在这里插入图片描述
图2

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

厚国兄

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

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

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

打赏作者

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

抵扣说明:

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

余额充值