4

Scatterplots with many ticks on the y-axis have a large whitespace on the top and on the bottom as you can see with the gridlines. How to remove whitespace on top and bottom of seaborn scatterplot?

scatterplot

The code for a minimal working example:

import matplotlib.pyplot as plt
import seaborn as sns

data = sns.load_dataset("car_crashes")

plt.figure(figsize=(5, 15))
sns.set_style("whitegrid")
sns.scatterplot(
    data=data,
    x='alcohol',
    y='abbrev',
    size='ins_losses',
    legend=False,
)

plt.show()
Matt Hall
  • 7,614
  • 1
  • 23
  • 36
Wuff
  • 257
  • 1
  • 8
  • 2
    I was looking into it, and it looks like you can adjust the margins. `plt.margins(0.015, tight=True)` – r-beginners Jul 23 '21 at 12:18
  • @r-beginners thanks that works :) The 0.015 is guess work, right? Because I realized when setting it to zero then the dots get cut off. The nicest solution would be that the margin is the same as the space between the y-ticks. – Wuff Jul 23 '21 at 12:45
  • You can see what units are used on the y-axis by running `ax = sns.scatterplot(...); ax.get_ylim()`. And what you want is `ax.set_ylim(-1, len(data['abbrev']))`. – Patrick FitzGerald Jul 23 '21 at 12:56
  • Yes, I'm guessing. I did some research from your comment and found some great individual [responses](https://stackoverflow.com/questions/44863375/how-to-change-spacing-between-ticks-in-matplotlib). The result of applying this code to your code led me to the correct spacing of 0.02. – r-beginners Jul 23 '21 at 12:57

1 Answers1

2

If you switch to the object oriented plotting style, passing ax around, you can easily get at the tick positions. Then you can adjust the spacing at the ends to whatever you like, e.g. by changing the 2 in the code below. I think doing it this way reduces the guesswork, because you're adjusting to a proportion of the tick interval. You'll also get sensible results no matter how many rows you're plotting.

For example, here's how I'd go about it (using fewer states to make the plot a bit smaller):

import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style("whitegrid")

# Get some example data.
data = sns.load_dataset("car_crashes")

# Make the plot.
fig, ax = plt.subplots(figsize=(5, 5))
sc = sns.scatterplot(data=data[:15],
                     x='alcohol',
                     y='abbrev',
                     size='ins_losses',
                     legend=False,
                     ax=ax,
                    )

# Get the first two and last y-tick positions.
miny, nexty, *_, maxy = ax.get_yticks()

# Compute half the y-tick interval (for example).
eps = (nexty - miny) / 2  # <-- Your choice.

# Adjust the limits.
ax.set_ylim(maxy+eps, miny-eps)

plt.show()

This gives:

enter image description here

Matt Hall
  • 7,614
  • 1
  • 23
  • 36
  • 1
    Thanks a lot! Works like a charm! Nice that it also works for the x-axis (which also seems to have different margins) by just using `ax.get_xticks()` and `ax.set_xlim()`. – Wuff Jul 23 '21 at 14:02