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编程中使用Pillow来处理图像的基础教程
Nov 20 Python
Python浅复制中对象生存周期实例分析
Apr 02 Python
python3实现钉钉消息推送的方法示例
Mar 14 Python
浅谈django2.0 ForeignKey参数的变化
Aug 06 Python
18个Python脚本可加速你的编码速度(提示和技巧)
Oct 17 Python
PyCharm使用Docker镜像搭建Python开发环境
Dec 26 Python
django 取消csrf限制的实例
Mar 13 Python
python matplotlib实现将图例放在图外
Apr 17 Python
python多进程使用函数封装实例
May 02 Python
终于搞懂了Keras中multiloss的对应关系介绍
Jun 22 Python
如何让PyQt5中QWebEngineView与JavaScript交互
Oct 21 Python
python实现高效的遗传算法
Apr 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结合飞信 免费天气预报短信
2009/05/07 PHP
php操作SVN版本服务器类代码
2011/11/27 PHP
114啦源码(114la)不能生成地方房产和地方报刊问题4级页面0字节的解决方法
2012/01/12 PHP
php中二分法查找算法实例分析
2016/09/22 PHP
解析 thinkphp 框架中的部分方法
2017/05/07 PHP
深入理解Yii2.0乐观锁与悲观锁的原理与使用
2017/07/26 PHP
PHP中散列密码的安全性分析
2019/07/26 PHP
JavaScript 直接操作本地文件的实现代码
2009/12/01 Javascript
JavaScript的内存释放问题详解
2015/01/21 Javascript
jQuery实现购物车表单自动结算效果实例
2015/08/10 Javascript
jquery转盘抽奖功能实现
2015/11/13 Javascript
Bootstrap简单表单显示学习笔记
2016/11/15 Javascript
概述一个页面从输入URL到页面加载完的过程
2016/12/16 Javascript
js实现首屏延迟加载实现方法 js实现多屏单张图片延迟加载效果
2017/07/17 Javascript
Intellij IDEA搭建vue-cli项目的方法步骤
2018/10/20 Javascript
js对象简介与基本用法示例
2020/03/13 Javascript
通过实例解析javascript Date对象属性及方法
2020/11/04 Javascript
[01:31]DOTA2上海特级锦标赛 SECRET战队完整宣传片
2016/03/16 DOTA
[02:49:21]2019完美盛典全程录像
2019/12/08 DOTA
详解Python中的type()方法的使用
2015/05/21 Python
Python通过paramiko远程下载Linux服务器上的文件实例
2018/12/27 Python
python实现图片二值化及灰度处理方式
2019/12/07 Python
Python如何读取、写入JSON数据
2020/07/28 Python
Python如何重新加载模块
2020/07/29 Python
css3针对移动端卡顿问题的解决(动画性能优化)
2020/02/14 HTML / CSS
浅析HTML5:'data-'属性的作用
2018/01/23 HTML / CSS
健身场所或家用健身设备:Life Fitness
2017/11/01 全球购物
电信专业毕业生推荐信
2013/11/18 职场文书
产品开发计划书
2014/04/27 职场文书
服务标语口号
2014/07/01 职场文书
校园元旦活动总结
2014/07/09 职场文书
青年教师个人总结
2015/02/11 职场文书
给校长的建议书作文400字
2015/09/14 职场文书
事业单位工作人员岗前培训心得体会
2016/01/08 职场文书
分析JVM源码之Thread.interrupt系统级别线程打断
2021/06/29 Java/Android
python数据可视化使用pyfinance分析证券收益示例详解
2021/11/20 Python