如何在Python中将 Seaborn Plot 保存到文件中?
Seaborn提供了一种将最终输出存储为不同所需文件格式的方法,例如.png、.pdf、.tiff、.eps等。让我们看看如何将输出图保存为特定的文件格式。
逐步实施
第1步:
使用内置函数load_dataset 从 seaborn 包中导入内置企鹅数据集。
句法:
seaborn.load_dataset(name, cache=True, data_home=None, **kws)
示例:
Python3
# code
# Install seaborn using pip install seaborn
# Import the seaborn package
import seaborn as sns
# load the inbuilt "penguins" dataset using
# seaborn inbuilt function load_dataset
data = sns.load_dataset("penguins")
# print the first 6 data
data.head()
Python3
# Install seaborn using pip install seaborn
# Import the seaborn package
import seaborn as sns
# load the inbuilt "penguins" dataset using
# seaborn inbuilt function load_dataset
data = sns.load_dataset("penguins")
scatter_plot = sns.scatterplot(
x=data['bill_length_mm'], y=data['bill_depth_mm'], hue=data['sex'])
# use get_figure function and store the plot i
# n a variable (scatter_fig)
scatter_fig = scatter_plot.get_figure()
# use savefig function to save the plot and give
# a desired name to the plot.
scatter_fig.savefig('scatterplot.png')
# this will store the plot in current working directory
Python3
# code
# Install seaborn using pip install seaborn
# Import the seaborn package
import seaborn as sns
# load the inbuilt "penguins" dataset using
# seaborn inbuilt function load_dataset
data = sns.load_dataset("penguins")
scatter_plot = sns.scatterplot(
x=data['bill_length_mm'], y=data['bill_depth_mm'], hue=data['sex'])
# use get_figure function and store the plot
# in a variable (scatter_fig)
scatter_fig = scatter_plot.get_figure()
# use savefig function to save the plot and give
# a desired name to the plot.
scatter_fig.savefig('scatterplot.jpg')
# this will store the plot in current working directory
Python3
# code
# Install seaborn using pip install seaborn
# Import the seaborn package
import seaborn as sns
# load the inbuilt "penguins" dataset using
# seaborn inbuilt function load_dataset
data = sns.load_dataset("penguins")
scatter_plot = sns.scatterplot(
x=data['bill_length_mm'], y=data['bill_depth_mm'], hue=data['sex'])
# use get_figure function and store the plot
# in a variable (scatter_fig)
scatter_fig = scatter_plot.get_figure()
# use savefig function to save the plot and give
# a desired name to the plot.
scatter_fig.savefig('scatterplot.tiff')
# this will store the plot in current working directory
Python3
# Install seaborn using pip install seaborn
# Import the seaborn package
import seaborn as sns
# load the inbuilt "penguins" dataset using
# seaborn inbuilt function load_dataset
data = sns.load_dataset("penguins")
scatter_plot = sns.scatterplot(
x=data['bill_length_mm'], y=data['bill_depth_mm'], hue=data['sex'])
# use get_figure function and store the plot
# in a variable (scatter_fig)
scatter_fig = scatter_plot.get_figure()
# use savefig function to save the plot and give
# a desired name to the plot.
scatter_fig.savefig(r'C:\Users\Documents\test\Plots\scatterplot.png')
# this will store the plot in specified directory
输出:
第2步:
对于这个例子,让我们使用散点图来检查雄性和雌性企鹅的 bill_length_mm 和 bill_depth_mm 之间是否存在任何关系。
句法:
seaborn.scatterplot(x, y, hue, style, size, data, palette,
hue_order, legend)
第 3 步:
这是将绘图保存为所需文件类型的主要步骤。使用 Seaborn get_figure函数获取步骤 2 中绘制的图形。
句法:
figure_name.get_figure()
get_figure函数将输出图存储到一个变量中。将绘图临时存储在变量中,如下所示
3a:将绘图保存为 .png:
最后,使用savefig函数并给出所需的名称和文件类型来存储绘图。下面的示例将绘图存储为当前工作目录中的 .png 文件。
Python3
# Install seaborn using pip install seaborn
# Import the seaborn package
import seaborn as sns
# load the inbuilt "penguins" dataset using
# seaborn inbuilt function load_dataset
data = sns.load_dataset("penguins")
scatter_plot = sns.scatterplot(
x=data['bill_length_mm'], y=data['bill_depth_mm'], hue=data['sex'])
# use get_figure function and store the plot i
# n a variable (scatter_fig)
scatter_fig = scatter_plot.get_figure()
# use savefig function to save the plot and give
# a desired name to the plot.
scatter_fig.savefig('scatterplot.png')
# this will store the plot in current working directory
输出:
3b:将 Seaborn 图形保存为 .jpg
Python3
# code
# Install seaborn using pip install seaborn
# Import the seaborn package
import seaborn as sns
# load the inbuilt "penguins" dataset using
# seaborn inbuilt function load_dataset
data = sns.load_dataset("penguins")
scatter_plot = sns.scatterplot(
x=data['bill_length_mm'], y=data['bill_depth_mm'], hue=data['sex'])
# use get_figure function and store the plot
# in a variable (scatter_fig)
scatter_fig = scatter_plot.get_figure()
# use savefig function to save the plot and give
# a desired name to the plot.
scatter_fig.savefig('scatterplot.jpg')
# this will store the plot in current working directory
输出:
3c:将 Seaborn 图保存为 .tiff
Python3
# code
# Install seaborn using pip install seaborn
# Import the seaborn package
import seaborn as sns
# load the inbuilt "penguins" dataset using
# seaborn inbuilt function load_dataset
data = sns.load_dataset("penguins")
scatter_plot = sns.scatterplot(
x=data['bill_length_mm'], y=data['bill_depth_mm'], hue=data['sex'])
# use get_figure function and store the plot
# in a variable (scatter_fig)
scatter_fig = scatter_plot.get_figure()
# use savefig function to save the plot and give
# a desired name to the plot.
scatter_fig.savefig('scatterplot.tiff')
# this will store the plot in current working directory
输出:
第4步:
如果您希望将 seaborn 绘图保存到特定文件夹,请按照以下步骤操作
Python3
# Install seaborn using pip install seaborn
# Import the seaborn package
import seaborn as sns
# load the inbuilt "penguins" dataset using
# seaborn inbuilt function load_dataset
data = sns.load_dataset("penguins")
scatter_plot = sns.scatterplot(
x=data['bill_length_mm'], y=data['bill_depth_mm'], hue=data['sex'])
# use get_figure function and store the plot
# in a variable (scatter_fig)
scatter_fig = scatter_plot.get_figure()
# use savefig function to save the plot and give
# a desired name to the plot.
scatter_fig.savefig(r'C:\Users\Documents\test\Plots\scatterplot.png')
# this will store the plot in specified directory
输出: