Python利用matplotlib生成圖片背景及圖例透明的效果
前言
最近工作中遇到一個需求,在使用matplotlib生成圖片,想要背景透明,而且圖例部分也顯示透明效果,通過查找相關(guān)資料找到了大概的設(shè)置方法,特此記錄,方便自己或者有需要的朋友們參考學(xué)習(xí)。
示例代碼
# coding=utf-8
# matplotlib背景透明示例圖
# python 3.5
import numpy as np
import matplotlib.pyplot as plt
from pylab import mpl
import scipy.stats as stats
# 設(shè)置中文字體
mpl.rcParams['font.sans-serif'] = ['SimHei']
def autolabel(rects):
# attach some text labels
for rect in rects:
height = rect.get_height()
# 設(shè)置標(biāo)注文字及位置
ax.text(rect.get_x() + rect.get_width() / 2, 0.03 + height, '%.4f' % height, ha='center', va='bottom')
# 數(shù)據(jù)
testData = [[0.87, 0.40, 0.56],
[0.97, 0.50, 0.33],
[0.88, 0.30, 0.44],
[0.25, 0.23, 0.17],
[0.73, 0.33, 0.45]]
N = 3
width = 0.5
ind = np.arange(width, width*6*N, width*6)
fig, ax = plt.subplots()
rectsTest1 = ax.bar(ind, (testData[0][0], testData[0][1], testData[0][2]), width, color=(0, 0, 1, 1), edgecolor=(0, 0, 1, 1))
rectsTest2 = ax.bar(ind + width, (testData[1][0], testData[1][1], testData[1][2]), width, color=(1, 0, 0, 1), edgecolor=(1, 0, 0, 1))
rectsTest3 = ax.bar(ind + 2*width, (testData[2][0], testData[2][1], testData[2][2]), width, color=(0, 1, 0, 1), edgecolor=(0, 1, 0, 1))
rectsTest4 = ax.bar(ind + 3*width, (testData[3][0], testData[3][1], testData[3][2]), width, color=(1, 0.6471, 0, 1), edgecolor=(1, 0.6471, 0, 1))
rectsTest5 = ax.bar(ind + 4*width, (testData[4][0], testData[4][1], testData[4][2]), width, color=(0.5804, 0, 0.8275, 1), edgecolor=(0.5804, 0, 0.8275, 1))
ax.set_xlim(0, 9.5)
ax.set_ylim(0, 1.4)
ax.set_ylabel('數(shù)值')
ax.yaxis.grid(True)
ax.set_xticks(ind + width * 2.5)
ax.set_xticklabels(('P', 'R', 'F'))
# 設(shè)置圖例
legend = ax.legend((rectsTest1, rectsTest2, rectsTest3, rectsTest4, rectsTest5), ('test1', 'test2', 'test3', 'test4', 'test5'))
frame = legend.get_frame()
frame.set_alpha(1)
frame.set_facecolor('none') # 設(shè)置圖例legend背景透明
# 給每個數(shù)據(jù)矩形標(biāo)注數(shù)值
autolabel(rectsTest1)
autolabel(rectsTest2)
autolabel(rectsTest3)
autolabel(rectsTest4)
autolabel(rectsTest5)
plt.savefig('C:/Users/XX/Desktop/test.png', format='png', bbox_inches='tight', transparent=True, dpi=600) # bbox_inches='tight'
圖片邊界空白緊致, 背景透明
效果可能在網(wǎng)頁上看不出來,但還是把圖片貼上來吧。

總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家學(xué)習(xí)或者使用python能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
python flask sqlalchemy連接數(shù)據(jù)庫流程介紹
這篇文章主要介紹了python flask sqlalchemy連接數(shù)據(jù)庫流程,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-09-09
pycharm中dgl安裝報錯FileNotFoundError:Could not find&nb
這篇文章主要介紹了pycharm中dgl安裝報錯FileNotFoundError:Could not find module ‘E:\XXXX\XXXX\lib\site-packages\dgl\dgl.dl問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02
聊聊Python代碼中if?__name__?==?‘__main__‘的作用是什么
一個python文件通常有兩種使用方法,第一是作為腳本直接執(zhí)行,第二是 import 到其他的python腳本中被調(diào)用執(zhí)行,這篇文章主要給大家介紹了關(guān)于Python代碼中if?__name__?==?‘__main__‘的作用是什么的相關(guān)資料,需要的朋友可以參考下2022-03-03
python?aeon庫進行時間序列算法預(yù)測分類實例探索
這篇文章主要介紹了python?aeon庫進行時間序列算法預(yù)測分類實例探索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-02-02

