在使用matplotlib畫圖的時候?qū)⒊霈F(xiàn)坐標(biāo)軸的標(biāo)簽太長而出現(xiàn)重疊的現(xiàn)象,本文主要通過自身測過好用的解決辦法進(jìn)行展示,希望也能幫到大家,原圖出現(xiàn)重疊現(xiàn)象例如圖1:
代碼為:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
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均為我用到的數(shù)據(jù),數(shù)據(jù)的形式等可自行更換。 ##將4個圖畫在一張圖上 fig = plt.figure(figsize = ( 13 , 11 )) ax1 = fig.add_subplot( 2 , 2 , 1 ) ##左右布局 ax2 = fig.add_subplot( 2 , 2 , 2 ) ax3 = fig.add_subplot( 2 , 2 , 3 ) ##上下布局 ax4 = fig.add_subplot( 2 , 2 , 4 ) plt.sca(ax1) labels = [ 'Today is Sunday' , 'Today is Monday' , 'Today is Tuesday' , 'Today is Wednesday' ] #標(biāo)簽 plt.boxplot(data1,labels = labels,boxprops = { 'linewidth' : '2' },capprops = { 'linewidth' : '2' },whiskerprops = { 'linewidth' : '2' },medianprops = { 'linewidth' : '2' }) #linewidth設(shè)置線條的粗細(xì);boxprops、capprops、whiskerprops、medianprops表示盒圖中各個線條的類型 plt.ylabel( 'Today' ,fontsize = 16 ) plt.xlabel( '(a)' ,fontsize = 16 ) plt.sca(ax2) labels = [ 'Today is Sunday' , 'Today is Monday' , 'Today is Tuesday' , 'Today is Wednesday' ] plt.boxplot(data2,labels = labels,boxprops = { 'linewidth' : '2' },capprops = { 'linewidth' : '2' },whiskerprops = { 'linewidth' : '2' },medianprops = { 'linewidth' : '2' }) plt.xlabel( '(b)' ,fontsize = 16 ) plt.sca(ax3) labels = [ 'Today is Sunday' , 'Today is Monday' , 'Today is Tuesday' , 'Today is Wednesday' ] plt.boxplot(data3,labels = labels,boxprops = { 'linewidth' : '2' },capprops = { 'linewidth' : '2' },whiskerprops = { 'linewidth' : '2' },medianprops = { 'linewidth' : '2' }) plt.ylabel( 'Today' ,fontsize = 16 ) plt.xlabel( '(c)' ,fontsize = 16 ) plt.sca(ax4) labels = [ 'Today is Sunday' , 'Today is Monday' , 'Today is Tuesday' , 'Today is Wednesday' ] plt.boxplot(data4,labels = labels,boxprops = { 'linewidth' : '2' },capprops = { 'linewidth' : '2' },whiskerprops = { 'linewidth' : '2' },medianprops = { 'linewidth' : '2' }) plt.xlabel( '(d)' ,fontsize = 16 ) plt.show() |
1、解決辦法1: 將軸標(biāo)簽分兩行顯示,如圖2:
只需在原代碼中每個子圖畫圖中加上代碼:
1
|
ax1.set_xticklabels([ 'Today is Sunday' , '\n' + 'Today is Monday' , 'Today is Tuesday' , '\n' + 'Today is Wednesday' ],fontsize = 16 ) |
'\n'+則表示換行顯示的意思,想要哪個標(biāo)簽換行顯示,則在標(biāo)簽前面加上此符號,也可以換多行,一個\n表示一行,例如'\n\n'+則表示換兩行顯示。fontsize是設(shè)置顯示標(biāo)簽的字體大小。
2、解決辦法2:軸標(biāo)簽傾斜顯示
同樣只需在原代碼的基礎(chǔ)上加上一句代碼:
1
|
ax1.set_xticklabels([ 'Today is Sunday' , 'Today is Monday' , 'Today is Tuesday' , 'Today is Wednesday' ],fontsize = 16 ,rotation = 10 ) |
rotation表示傾斜的角度,10即為傾斜10度,可任意設(shè)置,也可結(jié)合上面換行顯示一同使用。
3、解決辦法3:利用matplotlib里面的自動調(diào)整語句
只需在原代碼的畫圖部分的最后加上matplotlib自動調(diào)整的語句,圖則會自動調(diào)整標(biāo)簽大小:
1
|
plt.tight_layout() |
以上這篇python matplotlib畫盒圖、子圖解決坐標(biāo)軸標(biāo)簽重疊的問題就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/baidu_37995814/article/details/98727766