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