上次說到的,使用如下代碼保存矢量圖時,放在外側的圖例往往顯示不完整:
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
|
import numpy as np import matplotlib.pyplot as plt fig, ax = plt.subplots() x1 = np.random.uniform( - 10 , 10 , size = 20 ) x2 = np.random.uniform( - 10 , 10 , size = 20 ) #print(x1) #print(x2) number = [] x11 = [] x12 = [] for i in range ( 20 ): number.append(i + 1 ) x11.append(i + 1 ) x12.append(i + 1 ) plt.figure( 1 ) # you can specify the marker size two ways directly: plt.plot(number, x1, 'bo' , markersize = 20 ,label = 'a' ) # blue circle with size 20 plt.plot(number, x2, 'ro' , ms = 10 ,label = 'b' ) # ms is just an alias for markersize lgnd = plt.legend(bbox_to_anchor = ( 1.05 , 1 ), loc = 2 , borderaxespad = 0 ,numpoints = 1 ,fontsize = 10 ) lgnd.legendHandles[ 0 ]._legmarker.set_markersize( 16 ) lgnd.legendHandles[ 1 ]._legmarker.set_markersize( 10 ) plt.show() fig.savefig( 'scatter.png' ,dpi = 600 ) |
保存為scatter.png之后的效果為:
可以看到放在圖像右上的圖例只顯示了左邊一小部分。
這里的原因很簡單,使用savefig()函數進行保存矢量圖時,它是通過一個bounding box (bbox, 邊界框),進行范圍的框定,只將落入該框中的圖像進行保存,如果圖例沒有完全落在該框中,自然不能被保存。
懂得了其原理,再進行解決問題就比較簡單了。
這里有兩個解決思想:
1. 將沒有完全落入該bbox的圖像,通過移動的方法,使其完全落入該框中,那么bbox截取的圖像即是完整的 (將圖像移入bbox中);
2. 改變bbox的大小,使其完全包含該圖像,尤其是往往落入bbox外側的圖例 (將bbox擴大到完全包含圖像)。
下面分別介紹基于這兩個思想解決這個問題的兩種方法:
1. 利用函數subplots_adjust()
在該官方文檔中可以看到,subplots_adjust()函數的作用是調整子圖布局,它包含6個參數,其中4個參數left, right, bottom, top的作用是分別調整子圖的左部,右部,底部,頂部的位置,另外2個參數wspace, hspace的作用分別是調整子圖之間的左右之間距離和上下之間距離。
其默認數值分別為:
以上述圖為例,現考慮既然圖例右側沒有顯示,則調整subplots_adjust()函數的right參數,使其位置稍往左移,將參數right默認的數值0.9改為0.8,那么可以得到一個完整的圖例:
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
|
import numpy as np import matplotlib.pyplot as plt fig, ax = plt.subplots() x1 = np.random.uniform( - 10 , 10 , size = 20 ) x2 = np.random.uniform( - 10 , 10 , size = 20 ) #print(x1) #print(x2) number = [] x11 = [] x12 = [] for i in range ( 20 ): number.append(i + 1 ) x11.append(i + 1 ) x12.append(i + 1 ) plt.figure( 1 ) # you can specify the marker size two ways directly: plt.plot(number, x1, 'bo' , markersize = 20 ,label = 'a' ) # blue circle with size 20 plt.plot(number, x2, 'ro' , ms = 10 ,label = 'b' ) # ms is just an alias for markersize lgnd = plt.legend(bbox_to_anchor = ( 1.05 , 1 ), loc = 2 , borderaxespad = 0 ,numpoints = 1 ,fontsize = 10 ) lgnd.legendHandles[ 0 ]._legmarker.set_markersize( 16 ) lgnd.legendHandles[ 1 ]._legmarker.set_markersize( 10 ) fig.subplots_adjust(right = 0.8 ) plt.show() fig.savefig( 'scatter1.png' ,dpi = 600 ) |
保存為scatter1.png之后和scatter.png的對比效果為:
可以看到這時scatter1.png的圖例顯示完整,它是通過圖像的右側位置向左移動而被整體包含在保存的圖像中完成的。
同理,若legend的位置在圖像下側,使用savefig()保存時也是不完整的,這時需要修改的是函數subplots_adjust()的參數bottom,使其向上移,而被包含在截取圖像進行保存的框中,即下文介紹的bbox。
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
|
import numpy as np import matplotlib.pyplot as plt fig, ax = plt.subplots() x1 = np.random.uniform( - 10 , 10 , size = 20 ) x2 = np.random.uniform( - 10 , 10 , size = 20 ) #print(x1) #print(x2) number = [] x11 = [] x12 = [] for i in range ( 20 ): number.append(i + 1 ) x11.append(i + 1 ) x12.append(i + 1 ) plt.figure( 1 ) # you can specify the marker size two ways directly: plt.plot(number, x1, 'bo' , markersize = 20 ,label = 'a' ) # blue circle with size 20 plt.plot(number, x2, 'ro' , ms = 10 ,label = 'b' ) # ms is just an alias for markersize lgnd = plt.legend(bbox_to_anchor = ( 0.4 , - 0.1 ), loc = 2 , borderaxespad = 0 ,numpoints = 1 ,fontsize = 10 ) lgnd.legendHandles[ 0 ]._legmarker.set_markersize( 16 ) lgnd.legendHandles[ 1 ]._legmarker.set_markersize( 10 ) plt.show() fig.savefig( 'scatter#1.png' ,dpi = 600 ) |
由于subplots_adjust()中默認的bottom值為0.1,故添加fig.subplots_adjust(bottom=0.2),使其底部上移,修改為
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
|
import numpy as np import matplotlib.pyplot as plt fig, ax = plt.subplots() x1 = np.random.uniform( - 10 , 10 , size = 20 ) x2 = np.random.uniform( - 10 , 10 , size = 20 ) #print(x1) #print(x2) number = [] x11 = [] x12 = [] for i in range ( 20 ): number.append(i + 1 ) x11.append(i + 1 ) x12.append(i + 1 ) plt.figure( 1 ) # you can specify the marker size two ways directly: plt.plot(number, x1, 'bo' , markersize = 20 ,label = 'a' ) # blue circle with size 20 plt.plot(number, x2, 'ro' , ms = 10 ,label = 'b' ) # ms is just an alias for markersize lgnd = plt.legend(bbox_to_anchor = ( 0.4 , - 0.1 ), loc = 2 , borderaxespad = 0 ,numpoints = 1 ,fontsize = 10 ) lgnd.legendHandles[ 0 ]._legmarker.set_markersize( 16 ) lgnd.legendHandles[ 1 ]._legmarker.set_markersize( 10 ) fig.subplots_adjust(bottom = 0.2 ) plt.show() fig.savefig( 'scatter#1.png' ,dpi = 600 ) |
效果對比:
圖例legend在其它位置同理。
2. 利用函數savefig()
上個博客講到,使用savefig()函數中的三個參數fname, dpi, format可用以保存矢量圖,現用該函數中另一個參數bbox_inches使
未保存到圖中的圖例包含進來。
下圖可以看到,bbox_inches的作用是調整圖的bbox, 即bounding box(邊界框)
可以看到,當bbox_inches設為'tight'時,它會計算出距該圖像的較緊(tight)邊界框bbox,并將該選中的框中的圖像保存。
這里的較緊的邊界框應該是指完全包含該圖像的一個矩形,但和圖像有一定的填充距離,和Minimum bounding box(最小邊界框),個人認為,有一定區別。單位同樣是英寸(inch)。
這樣圖例就會被bbox包含進去,進而被保存。
完整代碼:
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
|
import numpy as np import matplotlib.pyplot as plt fig, ax = plt.subplots() x1 = np.random.uniform( - 10 , 10 , size = 20 ) x2 = np.random.uniform( - 10 , 10 , size = 20 ) #print(x1) #print(x2) number = [] x11 = [] x12 = [] for i in range ( 20 ): number.append(i + 1 ) x11.append(i + 1 ) x12.append(i + 1 ) plt.figure( 1 ) # you can specify the marker size two ways directly: plt.plot(number, x1, 'bo' , markersize = 20 ,label = 'a' ) # blue circle with size 20 plt.plot(number, x2, 'ro' , ms = 10 ,label = 'b' ) # ms is just an alias for markersize lgnd = plt.legend(bbox_to_anchor = ( 1.05 , 1 ), loc = 2 , borderaxespad = 0 ,numpoints = 1 ,fontsize = 10 ) lgnd.legendHandles[ 0 ]._legmarker.set_markersize( 16 ) lgnd.legendHandles[ 1 ]._legmarker.set_markersize( 10 ) #fig.subplots_adjust(right=0.8) plt.show() fig.savefig( 'scatter2.png' ,dpi = 600 ,bbox_inches = 'tight' ) |
保存為scatter2.png,下面是scatter.png, scatter1.png, scatter2.png三張圖的對比:
可以看到,scatter1.png,即第1種方法的思想,是將圖像的右側邊界向左移動,截取該圖用以保存的bbox未變;而scatter2.png,即第2種方法的思想,是直接將截取該圖用以保存的bbox擴大為整個圖像,而將其全部包括。
注:savefig()還有兩個參數需要說明
其中一個是pad_inches,它的作用是當前面的bbox_inches為'tight'時,調整圖像和bbox之間的填充距離,這里不需要設置,只要選擇默認值即可。
個人認為,如果設置pad_inches參數為0,即pad_inches=0,截取圖進行保存的bbox就是minimum bounding box (最小邊界框)。
另外一個是bbox_extra_artists,它的作用是計算圖像的bbox時,將其它的元素也包含進去。
這里舉個例子,如果在圖像左側再加一個文本框text,保存圖像時希望該文本框包含在bbox中,則可以使用該參數bbox_extra_artists將text包含進去(實際使用中,即使未使用bbox_extra_artists,保存的圖像也包含該text):
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
|
import numpy as np import matplotlib.pyplot as plt fig, ax = plt.subplots() x1 = np.random.uniform( - 10 , 10 , size = 20 ) x2 = np.random.uniform( - 10 , 10 , size = 20 ) #print(x1) #print(x2) number = [] x11 = [] x12 = [] for i in range ( 20 ): number.append(i + 1 ) x11.append(i + 1 ) x12.append(i + 1 ) plt.figure( 1 ) # you can specify the marker size two ways directly: plt.plot(number, x1, 'bo' , markersize = 20 ,label = 'a' ) # blue circle with size 20 plt.plot(number, x2, 'ro' , ms = 10 ,label = 'b' ) # ms is just an alias for markersize lgnd = plt.legend(bbox_to_anchor = ( 1.05 , 1 ), loc = 2 , borderaxespad = 0 ,numpoints = 1 ,fontsize = 10 ) lgnd.legendHandles[ 0 ]._legmarker.set_markersize( 16 ) lgnd.legendHandles[ 1 ]._legmarker.set_markersize( 10 ) text = ax.text( - 0.3 , 1 , "test" , transform = ax.transAxes) #fig.subplots_adjust(right=0.8) plt.show() fig.savefig( 'scatter3.png' ,dpi = 600 , bbox_extra_artists = (lgnd,text),bbox_inches = 'tight' ) |
顯示效果:
為防止有的元素沒有被包含在bbox中,可以考慮使用該參數
到此這篇關于Python matplotlib圖例放在外側保存時顯示不完整問題解決的文章就介紹到這了,更多相關matplotlib外側保存顯示不完整內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/Poul_henry/article/details/88311964