Python matplotlib生成图片背景透明

简介: 使用matplotlib生成图片,想要背景透明,而且图例部分也显示透明效果,找到了大概的设置方法,特此记录。# coding=utf-8# matplotlib背景透明示例图# python 3.

使用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' 图片边界空白紧致, 背景透明
效果可能在网页上看不出来,但还是把图片贴上来吧。



目录
相关文章
|
12天前
|
数据可视化 Python Windows
使用Python进行数据可视化(一、matplotlib)
使用Python进行数据可视化(一、matplotlib)
|
13天前
|
数据采集 数据可视化 数据挖掘
数据分析大神养成记:Python+Pandas+Matplotlib助你飞跃!
【6月更文挑战第12天】在数字时代,Python因其强大的数据处理能力和易用性成为数据分析首选工具。结合Pandas(用于高效数据处理)和Matplotlib(用于数据可视化),能助你成为数据分析专家。Python处理数据预处理、分析和可视化,Pandas的DataFrame简化表格数据操作,Matplotlib则提供丰富图表展示数据。掌握这三个库,数据分析之路将更加畅通无阻。
|
1月前
|
数据可视化 数据挖掘 Python
【Python DataFrame专栏】DataFrame的可视化探索:使用matplotlib和seaborn
【5月更文挑战第20天】本文介绍了使用Python的pandas、matplotlib和seaborn库进行数据可视化的步骤,包括创建示例数据集、绘制折线图、柱状图、散点图、热力图、箱线图、小提琴图和饼图。这些图表有助于直观理解数据分布、关系和趋势,适用于数据分析中的探索性研究。
【Python DataFrame专栏】DataFrame的可视化探索:使用matplotlib和seaborn
|
16天前
|
数据可视化 数据挖掘 大数据
Python中的数据可视化库Matplotlib及其应用
数据可视化是数据分析过程中至关重要的一环,而Matplotlib作为Python中最流行的数据可视化库之一,为用户提供了丰富的绘图工具和定制选项。本文将介绍Matplotlib的基本用法和常见应用,帮助读者更好地利用这一强大工具进行数据呈现和分析。
|
3天前
|
数据可视化 数据挖掘 API
Python数据可视化基础:使用Matplotlib绘制图表
Python的Matplotlib是数据可视化的首选库,它提供静态、动态和交互式图表。要开始,先通过`pip install matplotlib`安装。绘制基本折线图涉及导入`pyplot`,设定数据,然后用`plot()`函数画图,如: ```markdown import matplotlib.pyplot as plt x = [1, 2, 3, 4, 5] y = [2, 3, 5, 7, 11] plt.plot(x, y, 'o') plt.show() ``` 自定义图表包括更改线条样式、颜色等,例如: ```markdown
|
10天前
|
Python
Python学习笔记之Matplotlib模块入门(直线图、折线图、曲线图、散点图、柱状图、饼状图、直方图、等高线图和三维图的绘制)-2
Python学习笔记之Matplotlib模块入门(直线图、折线图、曲线图、散点图、柱状图、饼状图、直方图、等高线图和三维图的绘制)
|
10天前
|
数据可视化 开发者 Python
Python学习笔记之Matplotlib模块入门(直线图、折线图、曲线图、散点图、柱状图、饼状图、直方图、等高线图和三维图的绘制)-1
Python学习笔记之Matplotlib模块入门(直线图、折线图、曲线图、散点图、柱状图、饼状图、直方图、等高线图和三维图的绘制)
|
15天前
|
人工智能 数据可视化 开发者
|
1月前
|
数据可视化 BI 索引
【Python】—— matplotlib数据可视化
【Python】—— matplotlib数据可视化
28 1
|
27天前
|
数据可视化 数据处理 Python
Python数据可视化:Matplotlib库的使用与实战
Python数据可视化:Matplotlib库的使用与实战
39 0