国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python利用matplotlib生成圖片背景及圖例透明的效果

2019-11-25 16:13:30
字體:
來源:轉載
供稿:網友

前言

最近工作中遇到一個需求,在使用matplotlib生成圖片,想要背景透明,而且圖例部分也顯示透明效果,通過查找相關資料找到了大概的設置方法,特此記錄,方便自己或者有需要的朋友們參考學習。

示例代碼

# 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  # 設置中文字體 mpl.rcParams['font.sans-serif'] = ['SimHei']   def autolabel(rects):  # attach some text labels  for rect in rects:   height = rect.get_height()   # 設置標注文字及位置   ax.text(rect.get_x() + rect.get_width() / 2, 0.03 + height, '%.4f' % height, ha='center', va='bottom')  # 數據 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('數值') ax.yaxis.grid(True) ax.set_xticks(ind + width * 2.5) ax.set_xticklabels(('P', 'R', 'F'))  # 設置圖例 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') # 設置圖例legend背景透明  # 給每個數據矩形標注數值 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'

圖片邊界空白緊致, 背景透明 

效果可能在網頁上看不出來,但還是把圖片貼上來吧。

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家學習或者使用python能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對武林網的支持。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 二连浩特市| 张北县| 高陵县| 紫阳县| 玛多县| 平陆县| 五家渠市| 太原市| 叙永县| 新邵县| 乾安县| 教育| 阿荣旗| 镇江市| 肥西县| 天气| 永安市| 东光县| 濮阳县| 时尚| 南漳县| 溧水县| 万源市| 厦门市| 温宿县| 宝丰县| 文水县| 将乐县| 从化市| 大新县| 平湖市| 桐柏县| 宁海县| 杨浦区| 通城县| 华亭县| 巫溪县| 德保县| 河南省| 景东| 肥东县|