Python matplotlib生成图片背景透明的示例代码


Posted in Python onAugust 30, 2019

使用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文件操作,open读写文件,追加文本内容实例
Dec 14 Python
Python中使用多进程来实现并行处理的方法小结
Aug 09 Python
python基础_文件操作实现全文或单行替换的方法
Sep 04 Python
对python中GUI,Label和Button的实例详解
Jun 27 Python
django如何实现视图重定向
Jul 24 Python
python修改FTP服务器上的文件名
Sep 11 Python
python 使用pygame工具包实现贪吃蛇游戏(多彩版)
Oct 30 Python
解决tensorflow由于未初始化变量而导致的错误问题
Jan 06 Python
解决Pycharm双击图标启动不了的问题(JetBrains全家桶通用)
Aug 07 Python
通俗讲解python 装饰器
Sep 07 Python
Python进行区间取值案例讲解
Aug 02 Python
5道关于python基础 while循环练习题
Nov 27 Python
Python 批量刷博客园访问量脚本过程解析
Aug 30 #Python
快速解决docker-py api版本不兼容的问题
Aug 30 #Python
Python 使用 Pillow 模块给图片添加文字水印的方法
Aug 30 #Python
python pillow模块使用方法详解
Aug 30 #Python
docker-py 用Python调用Docker接口的方法
Aug 30 #Python
tesserocr与pytesseract模块的使用方法解析
Aug 30 #Python
Django获取应用下的所有models的例子
Aug 30 #Python
You might like
BBS(php & mysql)完整版(一)
2006/10/09 PHP
PHP实现异步调用方法研究与分享
2011/10/27 PHP
PHP实现的自定义图像居中裁剪函数示例【测试可用】
2017/08/11 PHP
BOOM vs RR BO5 第三场 2.14
2021/03/10 DOTA
解决extjs在firefox中关闭窗口再打开后iframe中js函数访问不到的问题
2008/11/06 Javascript
jQuery 1.5最新版本的改进细节分析
2011/01/19 Javascript
JavaScript高级程序设计(第3版)学习笔记12 js正则表达式
2012/10/11 Javascript
关于火狐(firefox)及ie下event获取的两种方法
2012/12/27 Javascript
Js,alert出现乱码问题的解决方法
2013/06/19 Javascript
jquery 图片缩放拖动的简单实例
2014/01/08 Javascript
Javascript window对象详解
2014/11/12 Javascript
jQuery $命名冲突解决方案汇总
2014/11/13 Javascript
JS实现进入页面时渐变背景色的方法
2015/02/25 Javascript
JavaScript判断是否是微信浏览器
2016/06/13 Javascript
react性能优化达到最大化的方法 immutable.js使用的必要性
2017/03/09 Javascript
Vue Cli与BootStrap结合实现表格分页功能
2017/08/18 Javascript
AngularJS中scope的绑定策略实例分析
2017/10/30 Javascript
vue2.0与bootstrap3实现列表分页效果
2017/11/28 Javascript
详解JavaScript中关于this指向的4种情况
2019/04/18 Javascript
如何在vue中使用HTML 5 拖放API
2021/01/14 Vue.js
[32:56]完美世界DOTA2联赛PWL S3 Rebirth vs CPG 第二场 12.11
2020/12/16 DOTA
Django的HttpRequest和HttpResponse对象详解
2018/01/26 Python
Win8下python3.5.1安装教程
2020/07/29 Python
浅谈django orm 优化
2018/08/18 Python
Python包和模块的分发详细介绍
2020/06/19 Python
OpenCV+python实现实时目标检测功能
2020/06/24 Python
Styleonme中文网:韩国高档人气品牌
2017/06/21 全球购物
法国体育用品商店:GO Sport
2019/10/23 全球购物
澳大利亚最受欢迎的女士度假服装:Kabana Shop
2020/10/10 全球购物
《老王》教学反思
2014/02/23 职场文书
家长高考寄语
2015/02/27 职场文书
地道战观后感500字
2015/06/04 职场文书
读书笔记怎么写
2015/07/01 职场文书
2016教师校本研修心得体会
2016/01/08 职场文书
分析Python list操作为什么会错误
2021/11/17 Python
十大必看国产动漫排名,魁拔上线,第二曾在日本播出
2022/03/18 国漫