【问题标题】:How to add x and y axis line in seaborn scatter plot如何在seaborn散点图中添加x和y轴线
【发布时间】:2021-03-27 22:21:14
【问题描述】:

我使用以下代码创建散点图(数据以导入为例)。但是,该图是在没有 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'
);

有什么建议吗?非常感谢。

【问题讨论】:

  • 我们无法在不知道snsdf 的情况下重现您的代码。
  • @SuperCiocia: sns 通常是这样定义的:import seaborn as sns
  • @SuperCiocia sns 是 seaborn 库,但是是的,我也无法重现此问题,而且我看不到任何与您显示的内容匹配的 seaborn 预定义样式
  • 我相信stackoverflow.com/questions/62884183/… 会有所帮助
  • 您似乎已经明确设置了默认的 seaborn 主题。它没有边框(x 和 y 轴也没有线),灰色的 facecolor 和白色的网格线。您可以使用sns.set_style("whitegrid") 来获得白色的面部颜色。您也可以使用sns.despine() 仅显示 x 轴和 y 轴,但在顶部和右侧不显示“刺”。见Controlling figure aesthetics

标签: python plot seaborn scatter-plot


【解决方案1】:

您似乎已经明确设置了默认的 seaborn 主题。它没有边框(x 和 y 轴也没有线),灰色的 facecolor 和白色的网格线。您可以使用sns.set_style("whitegrid") 来获得白色的面部颜色。您也可以使用sns.despine() 仅显示 x 轴和 y 轴,但在顶部和右侧不显示“刺”。有关微调绘图外观的更多信息,请参阅Controlling figure aesthetics

这是一个比较。请注意,样式应在创建轴之前设置,因此出于演示目的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()

【讨论】:

    猜你喜欢
    • 2022-01-18
    • 1970-01-01
    • 2020-10-10
    • 1970-01-01
    • 2019-07-07
    • 1970-01-01
    相关资源
    最近更新 更多