日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Python matplotlib生成圖片背景透明的示例代碼

系統(tǒng) 2236 0

使用matplotlib生成圖片,想要背景透明,而且圖例部分也顯示透明效果,找到了大概的設(shè)置方法,特此記錄。

            
# 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)頁上看不出來,但還是把圖片貼上來吧。

Python matplotlib生成圖片背景透明的示例代碼_第1張圖片

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。?!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 山东省| 淳化县| 太仆寺旗| 柳江县| 莆田市| 兴文县| 安福县| 蒙山县| 璧山县| 新化县| 柘城县| 内江市| 桐城市| 龙南县| 鄂州市| 农安县| 肇东市| 大关县| 蓬溪县| 西青区| 慈利县| 饶河县| 三江| 曲水县| 康保县| 稻城县| 深泽县| 罗甸县| 精河县| 吉水县| 确山县| 洪江市| 铁岭县| 宾阳县| 梓潼县| 会东县| 屯昌县| 安岳县| 治县。| 阿拉善左旗| 通辽市|