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中使用matplotlib模块绘制数据图的示例
May 04 Python
python结合shell查询google关键词排名的实现代码
Feb 27 Python
简单谈谈python中的Queue与多进程
Aug 25 Python
python保存网页图片到本地的方法
Jul 24 Python
python调用百度语音识别api
Aug 30 Python
python学生管理系统开发
Jan 30 Python
python 利用pandas将arff文件转csv文件的方法
Feb 12 Python
python 字典有序并写入json文件过程解析
Sep 30 Python
Pyspark获取并处理RDD数据代码实例
Mar 27 Python
python argparse模块通过后台传递参数实例
Apr 20 Python
Python如何实现爬取B站视频
May 20 Python
浅谈pandas dataframe对除数是零的处理
Jul 20 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 IP及IP段进行访问限制的代码
2008/12/17 PHP
PHP中创建图像并绘制文字的例子
2014/11/19 PHP
PHP使用mongoclient简单操作mongodb数据库示例
2019/02/08 PHP
laravel多条件查询方法(and,or嵌套查询)
2019/10/09 PHP
一段实时更新的时间代码
2006/07/07 Javascript
在JavaScript中遭遇级联表达式陷阱
2007/03/08 Javascript
JavaScript面向对象编程
2008/03/02 Javascript
style、 currentStyle、 runtimeStyle区别分析
2010/08/01 Javascript
JavaScript判断DOM何时加载完毕的技巧
2012/11/11 Javascript
用jquery写的菜单从左往右滑动出现
2014/04/11 Javascript
jQuery扁平化风格下拉框美化插件FancySelect使用指南
2015/02/10 Javascript
JS+CSS实现简易实用的滑动门菜单效果
2015/09/18 Javascript
AngularJS extend用法详解及实例代码
2016/11/15 Javascript
JavaScript 控制字体大小设置的方法
2016/11/23 Javascript
vue 1.0 结合animate.css定义动画效果
2018/07/11 Javascript
python使用cookielib库示例分享
2014/03/03 Python
python实现DNS正向查询、反向查询的例子
2014/04/25 Python
tensorflow建立一个简单的神经网络的方法
2018/02/10 Python
python 读取目录下csv文件并绘制曲线v111的方法
2018/07/06 Python
Python pymongo模块常用操作分析
2018/09/01 Python
详解DeBug Python神级工具PySnooper
2019/07/03 Python
Python3 tkinter 实现文件读取及保存功能
2019/09/12 Python
基于Python实现简单学生管理系统
2020/07/24 Python
anaconda升级sklearn版本的实现方法
2021/02/22 Python
迪梵英国官方网站:Darphin英国
2017/12/06 全球购物
Calphalon美国官网:美国顶级锅具品牌
2020/02/05 全球购物
华为智利官方商店:Huawei Chile
2020/05/09 全球购物
.NET程序员的数据库面试题
2012/10/10 面试题
员工拾金不昧表扬信
2014/01/09 职场文书
2015公司年度工作总结
2015/05/14 职场文书
2015年小学教导处工作总结
2015/05/26 职场文书
高中体育课教学反思
2016/02/16 职场文书
建立共青团委员会的请示
2019/04/02 职场文书
行政后勤人员工作计划应该怎么写?
2019/08/16 职场文书
MyBatis配置文件解析与MyBatis实例演示
2022/04/07 Java/Android
Java 轮询锁使用时遇到问题
2022/05/11 Java/Android