Matplotlib boxplot spacing on x axis

5.5k views Asked by At

I'm a beginner with python and matplotlib. I have code to make boxplots. The code works fine on small datasets, where there are not too many boxplots to draw. When I scale up to more plots the spacing on the x axis becomes a problem and the boxes, and more importantly the x axis labels, are too close together. I would sorely like to fix this and I'm sure there's a simple solution, I'm just too much of a noob.

Here's my code

    %matplotlib inline    
    plt.boxplot(data_sgc)
    plt.ylabel("Alignment Gap Score")
    plt.xlabel("Locus")
    plt.xticks(range(1,len(alignments.keys())+1), [i[1] for i in medians_sgc], rotation=90)
    remove_border()

Infuriatingly I can't show you the image I get because I need 10 rep or something daft like that. Anyway, I expect I need to do something like set fig=plt.... etc. then do something downstream of that.

If you're kind enough to reply, please bear in mind that I'm a total noob, and that any explanatory information you can give me will be very much appreciated!

Here is a link to the data. The order of is data_sgc, then medians_sgc, then alignments https://paste.ee/p/ay8zQ . All of those were just printed - dat_sgc is not complete as you will see, but I hope this gives you context to understand the problem.

All I need is the code which will allow me to increase the spacing between the plots on the x axis. Here is a link to the current output that I'm getting https://maxjohnlunt12.files.wordpress.com/2014/11/download-2.png although this screenshot was taken before I added in the argument to rotate the xlabels

1

There are 1 answers

0
Max John On BEST ANSWER

OK, I've got it sorted. Increased the increment values in the range functions and reduced the xticks font size.

plt.boxplot(data_sgc, positions = range(4,(len(alignments.keys())*4), 4))
plt.ylabel("Alignment Gap Score")
plt.xlabel("Locus")
plt.xticks(range(4,((len(alignments.keys())+1)*4),4), [i[1] for i in medians_sgc], rotation=90, fontsize = 4)
plt.tick_params(axis='x', pad=4) 
plt.tight_layout()
remove_border()