簡體   English   中英

如何在 seaborn scatter plot 中添加 x 和 y 軸線

[英]How to add x and y axis line in seaborn scatter plot

我使用以下代碼創建散點圖(數據以導入為例)。 但是,plot 是在沒有 x 和 y 軸的情況下創建的,這看起來很奇怪。 我也想保留 facecolor='white' 。

import seaborn as sns
tips = sns.load_dataset("tips")

fig, ax = plt.subplots(figsize=(10, 8))
sns.scatterplot(
    x='total_bill',
    y='tip',
    data=tips,
    hue='total_bill',
    edgecolor='black',
    palette='rocket_r',
    linewidth=0.5,
    ax=ax
)
ax.set(
    title='title',
    xlabel='total_bill',
    ylabel='tip',
    facecolor='white'
);

有什么建議么? 非常感謝。

在此處輸入圖像描述

您似乎已經明確設置了默認的 seaborn 主題。 它沒有邊框(x 和 y 軸也沒有線),灰色的 facecolor 和白色的網格線。 您可以使用sns.set_style("whitegrid")獲得白色的 facecolor。 您也可以使用sns.despine()僅顯示 x 軸和 y 軸,但在頂部和右側不顯示“脊椎”。 有關微調 plot 外觀的更多信息,請參閱控制圖形美學

這是一個比較。 請注意,應在創建軸之前設置樣式,因此出於演示目的plt.subplot創建一個軸。

import matplotlib.pyplot as plt
import seaborn as sns

sns.set()  # set the default style
# sns.set_style('white')
tips = sns.load_dataset("tips")

fig = plt.figure(figsize=(18, 6))
for subplot_ind in (1, 2, 3):
    if subplot_ind >= 2:
        sns.set_style('white')
    ax = plt.subplot(1, 3, subplot_ind)
    sns.scatterplot(
        x='total_bill',
        y='tip',
        data=tips,
        hue='total_bill',
        edgecolor='black',
        palette='rocket_r',
        linewidth=0.5,
        ax=ax
    )
    ax.set(
        title={1: 'Default theme', 2: 'White style', 3: 'White style with despine'}[subplot_ind],
        xlabel='total_bill',
        ylabel='tip'
    )
    if subplot_ind == 3:
        sns.despine(ax=ax)
plt.tight_layout()
plt.show()

比較seaborn主題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM