Reducing the distance between two drawers

I am drawing a bloxplot shown below using python and matplotlib. Is there a way to reduce the distance between two drawers on the x axis?

enter image description here

This is the code I use to get the figure above:

import matplotlib.pyplot as plt from matplotlib import rcParams rcParams['ytick.direction'] = 'out' rcParams['xtick.direction'] = 'out' fig = plt.figure() xlabels = ["CG", "EG"] ax = fig.add_subplot(111) ax.boxplot([values_cg, values_eg]) ax.set_xticks(np.arange(len(xlabels))+1) ax.set_xticklabels(xlabels, rotation=45, ha='right') fig.subplots_adjust(bottom=0.3) ylabels = yticks = np.linspace(0, 20, 5) ax.set_yticks(yticks) ax.set_yticklabels(ylabels) ax.tick_params(axis='x', pad=10) ax.tick_params(axis='y', pad=10) plt.savefig(os.path.join(output_dir, "output.pdf")) 

And this is an example closer to what I would like to get visually (although I wouldn’t mind if the boxes were a little closer to each other):

enter image description here

+7
source share
3 answers

Try changing the aspect ratio using

 ax.set_aspect(1.5) # or some other float 

The larger the number, the narrower (and higher) the graph should be:

the circle will be stretched so that the height is num times the width. aspect=1 same as aspect='equal' .

http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.set_aspect

+5
source

You can either change the aspect ratio of the chart, or use widths kwarg (doc) as such:

 ax.boxplot([values_cg, values_eg], widths=1) 

to make the windows wider.

+10
source

When your code writes:

 ax.set_xticks(np.arange(len(xlabels))+1) 

You put the first square at 0 and the second at 1 (the event, although later you change the tick marks), as in the second, “desired” example, which you gave, they are set to 1,2, 3. Therefore, I think that the alternative the solution would be to play with the position of xticks and xlim plot.

for example using

 ax.set_xlim(-1.5,2.5) 

will put them closer.

+1
source

All Articles