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

作者:hfut_jf 时间:2023-08-22 08:35:18 

前言

最近工作中遇到一个需求,在使用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利用matplotlib生成图片背景及图例透明的效果

来源:http://blog.csdn.net/hfut_jf/article/details/52648033

标签:python,matplotlib,图片背景
0
投稿

猜你喜欢

  • 基于原生JavaScript实现SPA单页应用

    2024-04-30 09:59:43
  • python3实现mysql导出excel的方法

    2024-01-20 17:57:58
  • CSS样式表中继承关系的空格与不空格

    2007-12-12 13:05:00
  • Python复制Word内容并使用格式设字体与大小实例代码

    2023-01-10 05:48:20
  • Python3 replace()函数使用方法

    2023-07-21 22:25:13
  • Python开发之基于模板匹配的信用卡数字识别功能

    2021-08-09 22:30:27
  • 原生js编写贪吃蛇小游戏

    2023-07-02 05:19:17
  • 高效的删除HTML标签的函数

    2009-02-26 13:02:00
  • python+splinter实现12306网站刷票并自动购票流程

    2023-08-02 20:50:18
  • python解释器pycharm安装及环境变量配置教程图文详解

    2021-10-13 22:50:44
  • pytorch 如何使用amp进行混合精度训练

    2023-12-25 13:35:02
  • JS highcharts动态柱状图原理及实现

    2024-04-22 12:52:17
  • 解析pandas apply() 函数用法(推荐)

    2022-09-02 15:10:09
  • SQL2005查看死锁存储过程sp_who_lock

    2024-01-12 20:34:13
  • Python安装第三方库及常见问题处理方法汇总

    2022-11-22 04:40:51
  • 使用postman进行接口自动化测试

    2022-11-09 08:36:35
  • Python趣味挑战之给幼儿园弟弟生成1000道算术题

    2021-08-13 07:18:56
  • 5个常见的XHTML验证错误

    2010-01-15 20:24:00
  • 详解Go开发Struct转换成map两种方式比较

    2024-04-26 17:32:12
  • 在Python中操作字符串之rstrip()方法的使用

    2022-03-22 18:34:36
  • asp之家 网络编程 m.aspxhome.com