Matplotlib draws x-axis to any size and elongates:
When we use python's matplotlib for drawing, sometimes we find that the x-axis and y-axis of the drawn picture are in the same proportion, as shown in Figure 1 below; but sometimes we need to see a lot of data information on the x-axis, so we need The need to elongate the x-axis. The specific python implementation method is shown below, and the effect is shown in Figure 2 below:
Figure 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/')
The generated renderings are as follows:
Figure 2