如何使用 Python(Matplotlib)在 Seaborn 中儲存繪圖?


要在 Seaborn 中儲存繪圖,我們可以使用 savefig() 方法。

步驟

  • 設定圖形大小並調整子圖之間和周圍的填充。
  • 製作一個二維的、尺寸可變的、潛在異構的表格資料。
  • 在資料集中繪製成對關係。
  • 使用 savefig() 方法將繪圖儲存到檔案中。
  • 要顯示圖形,請使用 show() 方法。

示例

import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

df = pd.DataFrame(np.random.random((5, 5)), columns=["a", "b", "c", "d", "e"])
sns_pp = sns.pairplot(df)
sns_pp.savefig("sns-heatmap.png")

輸出

當我們執行程式碼時,它將建立以下繪圖並將其作為“sns-heatmap.png”儲存在專案資料夾中。

更新於: 10-6-2021

4K+ 瀏覽量

開啟你的職業

完成課程並獲得認證

開始
廣告
© . All rights reserved.