How to remove space at the bottom of matplotlib chart?

I looked through the matplotlib user guide and cannot find a way to remove the empty space that is created at the bottom of my graph.

fig = plt.figure(1,figsize=(5,10)) axis = fig.add_subplot(211, autoscale_on=False,xlim=(1,10),ylim=(0,1)) 

I use the configurations that I use on the chart. I tried using frameon = False, but did not notice that it was doing something. I would like the graph to occupy the entire size of the output image.

Here is a photo: Notice that half the graph is half white space at the bottom.

I want to remove all this space. Both of the answers provided do not do this ... did I miss something else?

+6
source share
5 answers

You create space for the second plot and do not use it. Line

 axis = fig.add_subplot(211, autoscale_on=False,xlim=(1,10),ylim=(0,1)) 

adds a subtitle to the drawing, but 211 means "two graphs that are one plot wide, position 1". You can add a second graph below your current one with another subheading call by passing “212” (position 2).

To create a separate subplot and fix the problem, change your add_subplot call to:

 axis = fig.add_subplot(111, autoscale_on=False,xlim=(1,10),ylim=(0,1)) 

111 means one chart, the first position.

+9
source

If you save the output to a file, you can use bbox_inches='tight' :

 import matplotlib.pyplot as plt import numpy as np fig=plt.figure(1,figsize=(5,10)) axis = fig.add_subplot(211, autoscale_on=False, xlim=(1,10), ylim=(0,1)) N=100 x = np.linspace(1,10, N) y = np.cumsum(np.random.random(N)-0.5) y -= y.min() y /= y.max() plt.plot(x,y) plt.savefig('test.png', bbox_inches='tight', pad_inches=0.0) 
+4
source

I'm not quite sure what you mean. If you want to control the position and size of your subtask, try using add_axes ([left, bottom, width, height]) . Below is a sample code, I set the border to black to make sure you see clearly:

 import matplotlib.pyplot as plt fig = plt.figure(figsize=(6, 2.5)) ax = fig.add_axes([0.1, 0.18, 0.86, 0.75]) ax.plot([1, 2, 3, 9, 10], [0.85, 0.6, 0.5, 0.3, 0.32], 'b--', linewidth=2, label='Single Path') ax.plot([1, 2, 3, 9, 10], [1, 0.6, 0.5, 0.27, 0.3], 'r:', linewidth=2, label='QoS First') ax.legend(fancybox=True, shadow=True) ax.set_xlabel('Simulations: Flows per Second Varying') ax.set_ylabel('ACAR') ax.axis([1, 10, 0, 1]) ax.grid() plt.savefig('rm_wspace.png', edgecolor='k', dpi=300) 

rm_wsapce.png

+3
source

You should probably use the subplots_adjust () function. I found that

fig.subplots_adjust (bottom = 0.06)

seems to work best for me. This leaves enough space at the bottom for ticks and axis labels. If you want the graph to handle all the output, just set the bottom to a lower number.

Side note: subplots_adjust () can also control white space at the top, left and right, as well as the interval between subheadings. Coordinates are units of the whole figure. That is, (0,0) is the lower left and (1,1) is the upper left.

0
source

I usually remove all spaces in my shapes by removing the border around the chart. This is done using the frameon=False command defined here .

 fig = plt.figure(1,figsize=(5,10),frameon=False) axis = fig.add_subplot(211, autoscale_on=False,xlim=(1,10),ylim=(0,1)) 
0
source

Source: https://habr.com/ru/post/894668/


All Articles