0

I used the following code to create scatterplot (data is imported as an example). However, the plot was created without x and y axis, which looks weird. I would like to keep facecolor='white' as well.

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'
);

Any suggestions? Thanks a lot.

enter image description here

betahat
  • 27
  • 1
  • 7
  • 1
    we can't reproduce your code without knowing `sns` and `df `. – SuperCiocia Mar 27 '21 at 22:25
  • @SuperCiocia: `sns` is usually defined thusly: `import seaborn as sns`. – mechanical_meat Mar 27 '21 at 22:26
  • @SuperCiocia sns is seaborn library, but yes, I can't reproduce this issue either, and I can't see any of seaborn predefined styles matching what you show – dm2 Mar 27 '21 at 22:27
  • I believe https://stackoverflow.com/questions/62884183/trying-to-add-a-colorbar-to-a-seaborn-scatterplot can be helpful – Hook Mar 27 '21 at 22:28
  • 2
    You seem to have explicitly set the default seaborn theme. That has no border (so also no line for x and y axis), a grey facecolor and white grid lines. You can use `sns.set_style("whitegrid")` to have a white facecolor. You can also use `sns.despine()` to only show the x and y-axis but no "spines" at the top and right. See [Controlling figure aesthetics](https://seaborn.pydata.org/tutorial/aesthetics.html) – JohanC Mar 27 '21 at 22:42

1 Answers1

1

You seem to have explicitly set the default seaborn theme. That has no border (so also no line for x and y axis), a grey facecolor and white grid lines. You can use sns.set_style("whitegrid") to have a white facecolor. You can also use sns.despine() to only show the x and y-axis but no "spines" at the top and right. See Controlling figure aesthetics for more information about fine-tuning how the plot looks like.

Here is a comparison. Note that the style should be set before the axes are created, so for demo-purposes plt.subplot creates the axes one at a time.

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()

comparing seaborn themes

JohanC
  • 71,591
  • 8
  • 33
  • 66