matplotlib - 如何在seaborn中有一个带有暗网格背景的透明图形?
问题描述
保存透明图像时的默认行为是将背景设置为透明,但我希望背景保持原样,并且图中绘图周围的空白区域是透明的。
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
%load_ext autoreload
%autoreload 2
%matplotlib inline
%config InlineBackend.figure_format = 'svg'
tips = sns.load_dataset("tips")
# Set background of the charts' to be a dark grid.
sns.set_style("darkgrid")
g = sns.FacetGrid(tips, col="smoker", col_order=["Yes", "No"])
g = g.map(plt.hist, "total_bill")
# Save figure with a transparent background (but this unfortunately)
# overrides the dark grid, whereas I want the white surrounding the
# plots to be transparent.
plt.savefig("example.pdf", transparent=True)
解决方案
您可以更新matplotlib
'srcParams
以使图形背景透明但保持网格处于活动状态,将其添加到您的代码中:
from matplotlib import rc
matplotlib.rcParams['axes.grid'] = True
matplotlib.rcParams['savefig.transparent'] = True
推荐阅读
- ios - 在检查无效字符时获取 UILabel 的文本以更改
- java - 通过同一个activity中的activity解析不同的数据
- android - 从 Android WebView 获取 HTTPOnly Cookie
- c# - 如何调用方法并异步显示数据?当我尝试时它什么也没做
- jmeter - 使用正则表达式提取器 JMeter 提取值
- python - 在python中将多头csv转换为嵌套字典
- google-sheets - 此时对属性的写入过多。请等待最多 12 小时以处理更改
- r - 函数不执行 read.csv 行
- vba - 如何使用此 VBA 代码自动过滤第 2 列中的数字?
- sql - SQL - 将一列除以另一列