gpt4 book ai didi

matplotlib - 如何在 seaborn 中有一个带有暗网格背景的透明图形?

转载 作者:行者123 更新时间:2023-12-05 07:17:02 25 4
gpt4 key购买 nike

保存透明图像时的默认行为是将背景设置为透明,但我希望背景保持原样,并且图中绘图周围的空白区域是透明的。

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)

如图笔记本中所示(有darkgrid) Figure as shown in the notebook (with the darkgrid).

保存后的图(无背景) Figure as it looks saved (no background)

最佳答案

您可以更新 matplotlibrcParams 以使图形背景透明但保持网格处于事件状态,将其添加到您的代码中:

from matplotlib import rc

matplotlib.rcParams['axes.grid'] = True
matplotlib.rcParams['savefig.transparent'] = True

关于matplotlib - 如何在 seaborn 中有一个带有暗网格背景的透明图形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58936013/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com