Python matplotlib繪圖設(shè)置圖例案例
一、語法簡介
plt.legend(loc=2,edgecolor='red',facecolor='green',shadow='True',fontsize=10)
edgecolor
圖例邊框線顏色- ?
facecolor
圖例背景色 shadow
是否添加陰影- ?
title
圖例標(biāo)題 fontsize
設(shè)置字體大小
'''
設(shè)置圖例位置loc參數(shù)簡介
best???????? 0? 根據(jù)圖標(biāo)區(qū)域自動(dòng)選擇最合適的位置
upper right? 1? 右上角
upper left?? 2? 左上角
lower left?? 3? 左下角
lower right? 4? 右下角
right??????? 5? 右側(cè)
center left? 6? 左側(cè)中心
center right 7? 右側(cè)中心
lower center 8? 底部中心
upper center 9? 頂部中心
center?????? 10 正中心位置
'''
二、完整代碼
import matplotlib.pyplot as plt import numpy as np plt.rcParams['font.sans-serif'] = ['STZhongsong'] # 指定默認(rèn)字體:解決plot不能顯示中文問題 plt.rcParams['axes.unicode_minus'] = False #用來正常顯示負(fù)號(hào) x=np.arange(8) y=np.arange(100,900,100) print(y) #建立畫布 figsize,它用width和height來控制畫布的寬和高 plt.figure(figsize=(8,6),dpi=90) #facecolor='red'設(shè)置畫布顏色 plt.subplot(1,1,1)#建立坐標(biāo)系 plt.bar(x,y,label='銷售數(shù)量') #繪制柱狀圖 plt.xlabel("銷售月份",fontsize=10,color='red',fontweight='bold',loc='center',backgroundcolor='black',labelpad=6) #顯示橫坐標(biāo)標(biāo)題 fontsize設(shè)置字體大小,color設(shè)置字的顏色,fontweight設(shè)置標(biāo)簽是否加粗 #loc設(shè)置標(biāo)簽位置(具體值有center left right) backgroundcolor設(shè)置標(biāo)簽的背景顏色 labelpad與軸的距離 plt.ylabel("銷售數(shù)量") plt.xticks(x,['2021年1月','2021年2月','2021年3月','2021年4月','2021年5月','2021年6月','2021年7月','2021年8月',],rotation=15) plt.yticks(y,['100k','200k','300k','400k','500k','600k','700k','800k',], rotation=30,fontsize=10,color='red',fontweight='bold',backgroundcolor='black')#rotation設(shè)置刻度值傾斜角度 plt.xlim(-1,9) #設(shè)置x軸刻度值的范圍 plt.ylim(0,900)#設(shè)置y軸刻度值的范圍 plt.axis("on") #plt.axis("off") #關(guān)閉坐標(biāo)軸 plt.legend(loc=2,edgecolor='red',facecolor='green',shadow='True',fontsize=10) #edgecolor 圖例邊框線顏色 facecolor 圖例背景色 shadow 是否添加陰影 title 圖例標(biāo)題 fontsize 設(shè)置字體大小 ''' 設(shè)置圖例位置loc參數(shù)簡介 best 0 根據(jù)圖標(biāo)區(qū)域自動(dòng)選擇最合適的位置 upper right 1 右上角 upper left 2 左上角 lower left 3 左下角 lower right 4 右下角 right 5 右側(cè) center left 6 左側(cè)中心 center right 7 右側(cè)中心 lower center 8 底部中心 upper center 9 頂部中心 center 10 正中心位置 ''' plt.show()
三、效果圖展示
到此這篇關(guān)于Python matplotlib
繪圖設(shè)置圖例案例的文章就介紹到這了,更多相關(guān)Python matplotlib繪圖設(shè)置圖例內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python學(xué)生管理系統(tǒng)代碼實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了python學(xué)生管理系統(tǒng)代碼實(shí)現(xiàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03Python常見MongoDB數(shù)據(jù)庫操作實(shí)例總結(jié)
這篇文章主要介紹了Python常見MongoDB數(shù)據(jù)庫操作,結(jié)合實(shí)例形式詳細(xì)總結(jié)了Python針對MongoDB數(shù)據(jù)庫相關(guān)pymongo庫安裝以及MongoDB數(shù)據(jù)庫的增刪改查等相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2018-07-07Python異步編程之協(xié)程任務(wù)的調(diào)度操作實(shí)例分析
這篇文章主要介紹了Python異步編程之協(xié)程任務(wù)的調(diào)度操作,結(jié)合實(shí)例形式分析了Python異步編程中協(xié)程任務(wù)的調(diào)度相關(guān)原理、實(shí)現(xiàn)方法與操作注意事項(xiàng),需要的朋友可以參考下2020-02-02