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


Posted in Python onApril 27, 2017

前言

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

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家学习或者使用python能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对三水点靠木的支持。

Python 相关文章推荐
由Python运算π的值深入Python中科学计算的实现
Apr 17 Python
用Python编写脚本使IE实现代理上网的教程
Apr 23 Python
python实现逻辑回归的方法示例
May 02 Python
Python输入二维数组方法
Apr 13 Python
Python 修改列表中的元素方法
Jun 26 Python
python实现本地图片转存并重命名的示例代码
Oct 27 Python
pandas dataframe添加表格框线输出的方法
Feb 08 Python
利用OpenCV和Python实现查找图片差异
Dec 19 Python
Python内置数据类型list各方法的性能测试过程解析
Jan 07 Python
20行Python代码实现视频字符化功能
Apr 13 Python
python3.6使用SMTP协议发送邮件
May 20 Python
如何利用python生成MD5并去重
Dec 07 Python
python使用matplotlib绘图时图例显示问题的解决
Apr 27 #Python
Python中生成Epoch的方法
Apr 26 #Python
python 网络编程详解及简单实例
Apr 25 #Python
python 全文检索引擎详解
Apr 25 #Python
window下eclipse安装python插件教程
Apr 24 #Python
Python处理PDF及生成多层PDF实例代码
Apr 24 #Python
python爬虫框架scrapy实战之爬取京东商城进阶篇
Apr 24 #Python
You might like
PHP实现文件安全下载
2006/10/09 PHP
js几个验证函数代码
2010/03/25 Javascript
javascript Array对象基础知识小结
2010/11/16 Javascript
jquery插件之信息弹出框showInfoDialog(成功/错误/警告/通知/背景遮罩)
2013/01/09 Javascript
解析JavaScript中的不可见数据类型
2013/12/02 Javascript
原生JS和JQuery动态添加、删除表格行的方法
2015/05/28 Javascript
Javascript生成全局唯一标识符(GUID,UUID)的方法
2016/02/27 Javascript
基于jQuery的网页影音播放器jPlayer的基本使用教程
2016/03/08 Javascript
浅析Javascript中bind()方法的使用与实现
2016/05/30 Javascript
深入理解ECMAScript的几个关键语句
2016/06/01 Javascript
javascript实现图片左右滚动效果【可自动滚动,有左右按钮】
2016/09/19 Javascript
浅析Ajax语法
2016/12/05 Javascript
React BootStrap用户体验框架快速上手
2018/03/06 Javascript
[05:31]DOTA2英雄梦之声_第08期_莉娜
2014/06/23 DOTA
用Python脚本生成Android SALT扰码的方法
2013/09/18 Python
python回调函数的使用方法
2014/01/23 Python
Python pickle类库介绍(对象序列化和反序列化)
2014/11/21 Python
如何将python中的List转化成dictionary
2016/08/15 Python
Python编程之string相关操作实例详解
2017/07/22 Python
5分钟 Pipenv 上手指南
2018/12/20 Python
使用Python进行防病毒免杀解析
2019/12/13 Python
Python浮点型(float)运算结果不正确的解决方案
2020/09/22 Python
Python classmethod装饰器原理及用法解析
2020/10/17 Python
解决python3输入的坑——input()
2020/12/05 Python
Css3圆角边框制作代码
2015/11/18 HTML / CSS
CSS3中新增的对文本和字体的设置
2020/02/03 HTML / CSS
车间班组长的职责
2013/12/13 职场文书
中专毕业生个人职业生涯规划
2014/02/19 职场文书
《春晓》教学反思
2014/04/20 职场文书
小学母亲节活动总结
2015/02/10 职场文书
高中生打架检讨书1000字
2015/02/17 职场文书
为什么阅读对所有年龄段的孩子都很重要?
2019/07/08 职场文书
详解Python描述符的工作原理
2021/06/11 Python
springboot临时文件存储目录配置方式
2021/07/01 Java/Android
Java 多态分析
2022/04/26 Java/Android
输入框跟随文字内容适配宽实现示例
2022/08/14 Javascript