設定 Seaborn 圖的背景顏色
    
    Manav Narula
    2024年2月15日
    
    Seaborn
    Seaborn Color
    
 
本教程將介紹如何更改使用 Python 中的 seaborn 模組建立的繪圖的背景顏色。
在 Python 中使用 seaborn.set() 函式更改 Seaborn 圖的背景顏色
    
set() 函式新增了不同的元素並配置了圖的美感。在 seaborn 中沒有直接的論據或方法來改變背景顏色。由於 seaborn 模組建立在 matplotlib 模組上,我們可以使用該模組中的引數來繪製 seaborn 圖。我們可以將它們作為字典鍵值對傳遞給 set() 函式中的 rc 引數。
我們將使用 axes.facecolor 和 figure.facecolor 引數來改變背景顏色。軸被視為繪製圖形的畫布,圖形是包含不同軸物件的整體物件。
我們可以使用上面的方法,如下所示。
import random
import seaborn as sns
import matplotlib as plt
s_x = random.sample(range(0, 100), 20)
s_y = random.sample(range(0, 100), 20)
sns.set(rc={"axes.facecolor": "cornflowerblue", "figure.facecolor": "cornflowerblue"})
sns.scatterplot(y=s_y, x=s_x)

在 Python 中使用 seaborn.set_style() 函式更改 Seaborn 圖的背景顏色
seaborn 模組中有許多不同的主題可用。這些主題可能不是我們問題的確切解決方案,但它允許我們稍微自定義圖形的背景。
set_style() 函式用於設定圖的主題。它可以接受以下值 - dark、white、whitegrid、darkgrid 和 tickers。dark 和 darkgrid 引數分別提供帶網格和不帶網格的灰色背景。同樣,我們可以得出 white 和 whitegrid 引數的結論。
例如,
import random
import seaborn as sns
import matplotlib as plt
s_x = random.sample(range(0, 100), 20)
s_y = random.sample(range(0, 100), 20)
sns.set_style("darkgrid")
sns.scatterplot(y=s_y, x=s_x)

        Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
    
作者: Manav Narula
    Manav is a IT Professional who has a lot of experience as a core developer in many live projects. He is an avid learner who enjoys learning new things and sharing his findings whenever possible.
LinkedIn