SoFunction
Updated on 2024-12-15

python matplotlib draw box plots, subplots to solve the problem of overlapping axis labels

In the use of matplotlib drawing will often appear when the axes of the label is too long and the phenomenon of overlap, this article is mainly through their own measured good solution to show, I hope to help you, the original overlap phenomenon such as Figure 1:

Code for:

data1=[[0.3765,0.3765,0.3765,0.3765,0.3765],[0.3765,0.3765,0.3765,0.3765,0.3765],[0.3765,0.3765,0.3765,0.3765,0.3765],[0.3765,0.3765,0.3765,0.3765,0.3765]]
data2=[[0.2985,0.2268,0.2985,0.2996,0.2985],[0.2022,0.3203,0.3141,0.2926,0.2681],[0.2985,0.2668,0.2786,0.2985,0.2985],[0.2985,0.2985,0.2984,0.2978,0.2966]]
data3=[[0.7789,0.7698,0.6999,0.7789,0.7789],[0.7788,0.7758,0.7768,0.7698,0.8023],[0.7789,0.7781,0.7789,0.7789,0.7789],[0.7789,0.7782,0.7752,0.7852,0.7654]]
data4=[[0.6688,0.6688,0.6688,0.6981,0.6618],[0.6688,0.5644,0.5769,0.5858,0.5882],[0.6688,0.6688,0.6688,0.6688,0.6646],[0.6688,0.6646,0.6646,0.6688,0.6746]]  #date1-date4 are the data I used, the form of the data etc. can be changed.
## Drawing 4 diagrams on a single chart
fig=(figsize=(13,11))
ax1=fig.add_subplot(2, 2, 1)## Left and right layout
ax2=fig.add_subplot(2, 2, 2)
ax3=fig.add_subplot(2, 2, 3)## Up and down layout
ax4=fig.add_subplot(2, 2, 4)

(ax1)   
labels=['Today is Sunday','Today is Monday','Today is Tuesday','Today is Wednesday']#hashtag
(data1,labels=labels,boxprops={'linewidth':'2'},capprops={'linewidth':'2'},whiskerprops={'linewidth':'2'},medianprops={'linewidth':'2'}) #linewidth sets the thickness of the line; boxprops, capprops, whiskerprops, medianprops indicate the type of each line in the box plot
('Today',fontsize=16)
('(a)',fontsize=16)

(ax2)   
labels=['Today is Sunday','Today is Monday','Today is Tuesday','Today is Wednesday']
(data2,labels=labels,boxprops={'linewidth':'2'},capprops={'linewidth':'2'},whiskerprops={'linewidth':'2'},medianprops={'linewidth':'2'})
('(b)',fontsize=16)

(ax3)   
labels=['Today is Sunday','Today is Monday','Today is Tuesday','Today is Wednesday']
(data3,labels=labels,boxprops={'linewidth':'2'},capprops={'linewidth':'2'},whiskerprops={'linewidth':'2'},medianprops={'linewidth':'2'})
('Today',fontsize=16)
('(c)',fontsize=16)

(ax4)   
labels=['Today is Sunday','Today is Monday','Today is Tuesday','Today is Wednesday']
(data4,labels=labels,boxprops={'linewidth':'2'},capprops={'linewidth':'2'},whiskerprops={'linewidth':'2'},medianprops={'linewidth':'2'})
('(d)',fontsize=16)
()

1, solution 1: axis label will be displayed in two lines, such as Figure 2:

Simply add the code to each sub-picture map in the original code:

ax1.set_xticklabels(['Today is Sunday','\n'+'Today is Monday','Today is Tuesday','\n'+'Today is Wednesday'],fontsize=16) 

'\n'+ means to change the line display, which label you want to change the line display, then add this symbol in front of the label, you can also change more than one line, a \n means a line, for example, '\n\n'+ means to change the display of two lines. fontsize is to set the font size of the display label.

2、Solution 2: Tilted display of axis labels

Again just add a line of code to the original code:

ax1.set_xticklabels(['Today is Sunday','Today is Monday','Today is Tuesday','Today is Wednesday'],fontsize=16,rotation=10)

rotation means the angle of tilt, 10 means tilt 10 degrees, can be set arbitrarily, can also be used in conjunction with the above line feed display.

3, solution 3: the use of matplotlib inside the automatic adjustment statement

Simply add the matplotlib auto-adjustment statement at the end of the drawing part of the original code, and the graph will be automatically adjusted to the size of the labels:

plt.tight_layout()

The above this python matplotlib draw box plots, subplots to solve the problem of overlapping axes labels is all I have to share with you, I hope it can give you a reference, and I hope you support me more.