Python matplotlib繪圖設置圖例案例
一、語法簡介
plt.legend(loc=2,edgecolor='red',facecolor='green',shadow='True',fontsize=10)
edgecolor
圖例邊框線顏色- ?
facecolor
圖例背景色 shadow
是否添加陰影- ?
title
圖例標題 fontsize
設置字體大小
'''
設置圖例位置loc參數(shù)簡介
best???????? 0? 根據(jù)圖標區(qū)域自動選擇最合適的位置
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'] # 指定默認字體:解決plot不能顯示中文問題 plt.rcParams['axes.unicode_minus'] = False #用來正常顯示負號 x=np.arange(8) y=np.arange(100,900,100) print(y) #建立畫布 figsize,它用width和height來控制畫布的寬和高 plt.figure(figsize=(8,6),dpi=90) #facecolor='red'設置畫布顏色 plt.subplot(1,1,1)#建立坐標系 plt.bar(x,y,label='銷售數(shù)量') #繪制柱狀圖 plt.xlabel("銷售月份",fontsize=10,color='red',fontweight='bold',loc='center',backgroundcolor='black',labelpad=6) #顯示橫坐標標題 fontsize設置字體大小,color設置字的顏色,fontweight設置標簽是否加粗 #loc設置標簽位置(具體值有center left right) backgroundcolor設置標簽的背景顏色 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設置刻度值傾斜角度 plt.xlim(-1,9) #設置x軸刻度值的范圍 plt.ylim(0,900)#設置y軸刻度值的范圍 plt.axis("on") #plt.axis("off") #關(guān)閉坐標軸 plt.legend(loc=2,edgecolor='red',facecolor='green',shadow='True',fontsize=10) #edgecolor 圖例邊框線顏色 facecolor 圖例背景色 shadow 是否添加陰影 title 圖例標題 fontsize 設置字體大小 ''' 設置圖例位置loc參數(shù)簡介 best 0 根據(jù)圖標區(qū)域自動選擇最合適的位置 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
繪圖設置圖例案例的文章就介紹到這了,更多相關(guān)Python matplotlib繪圖設置圖例內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python常見MongoDB數(shù)據(jù)庫操作實例總結(jié)
這篇文章主要介紹了Python常見MongoDB數(shù)據(jù)庫操作,結(jié)合實例形式詳細總結(jié)了Python針對MongoDB數(shù)據(jù)庫相關(guān)pymongo庫安裝以及MongoDB數(shù)據(jù)庫的增刪改查等相關(guān)操作技巧與注意事項,需要的朋友可以參考下2018-07-07Python異步編程之協(xié)程任務的調(diào)度操作實例分析
這篇文章主要介紹了Python異步編程之協(xié)程任務的調(diào)度操作,結(jié)合實例形式分析了Python異步編程中協(xié)程任務的調(diào)度相關(guān)原理、實現(xiàn)方法與操作注意事項,需要的朋友可以參考下2020-02-02