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 相关文章推荐
以Flask为例讲解Python的框架的使用方法
Apr 29 Python
尝试使用Python多线程抓取代理服务器IP地址的示例
Nov 09 Python
python处理html转义字符的方法详解
Jul 01 Python
一个基于flask的web应用诞生 组织结构调整(7)
Apr 11 Python
30秒轻松实现TensorFlow物体检测
Mar 14 Python
python 按照固定长度分割字符串的方法小结
Apr 30 Python
Python 运行 shell 获取输出结果的实例
Jan 07 Python
Python对象转换为json的方法步骤
Apr 25 Python
python区分不同数据类型的方法
Oct 14 Python
Python 时间戳之获取整点凌晨时间戳的操作方法
Jan 28 Python
filter使用python3代码进行迭代元素的实例详解
Dec 03 Python
pycharm debug 断点调试心得分享
Apr 16 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操作Memcache实例介绍
2013/06/14 PHP
php中PDO方式实现数据库的增删改查
2015/05/17 PHP
浅谈PHP中类和对象的相关函数
2017/04/26 PHP
iis 7下安装laravel 5.4环境的方法教程
2017/06/14 PHP
使用EXT实现无刷新动态调用股票信息
2008/11/01 Javascript
jQuery 联动日历实现代码
2012/05/31 Javascript
jquery datatable后台封装数据示例代码
2014/08/07 Javascript
jQuery实现多按钮单击变色
2014/11/27 Javascript
JS操作HTML自定义属性的方法
2015/02/10 Javascript
jQuery实现指定内容滚动同时左侧或其它地方不滚动的方法
2015/08/08 Javascript
JavaScript Length 属性的总结
2015/11/02 Javascript
jQuery实现左侧导航模块的显示与隐藏效果
2016/07/04 Javascript
Angularjs实现搜索关键字高亮显示效果
2017/01/17 Javascript
js时间戳和c#时间戳互转方法(推荐)
2017/02/15 Javascript
JavaScript递归算法生成树形菜单
2017/08/15 Javascript
AngularJS实现自定义指令及指令配置项的方法
2017/11/20 Javascript
微信小程序 scroll-view 实现锚点跳转功能
2019/12/12 Javascript
解决python3中解压zip文件是文件名乱码的问题
2018/03/22 Python
详解Python中where()函数的用法
2018/03/27 Python
Python实现对特定列表进行从小到大排序操作示例
2019/02/11 Python
Python通过TensorFLow进行线性模型训练原理与实现方法详解
2020/01/15 Python
TensorFlow dataset.shuffle、batch、repeat的使用详解
2020/01/21 Python
浅析python表达式4+0.5值的数据类型
2020/02/26 Python
PyCharm中Matplotlib绘图不能显示UI效果的问题解决
2020/03/12 Python
Xadmin+rules实现多选行权限方式(级联效果)
2020/04/07 Python
Jupyter notebook运行Spark+Scala教程
2020/04/10 Python
Python3如何在服务器打印资产信息
2020/08/27 Python
用css3制作纸张效果(外翻卷角)
2013/02/01 HTML / CSS
CSS3 实现侧边栏展开收起动画
2014/12/22 HTML / CSS
英国皇家邮政海外旗舰店:Royal Mail
2018/02/21 全球购物
中科创达面试题
2016/12/28 面试题
汽车驾驶求职信
2013/10/25 职场文书
客服工作职责
2013/12/11 职场文书
幼儿园毕业园长感言
2014/02/24 职场文书
献爱心活动总结
2014/05/07 职场文书
SQL Server数据库的三种创建方法汇总
2023/05/08 MySQL