Skip to content

scatterplot - date x-axis is too wide #1641

Closed

Description

@barashe

When plotting a time series using scatterplot, the figure's x-axis is far too wide.

df = pd.DataFrame({'Depth': {0: 0.2,
  1: 0.4,
  2: 0.4,
  3: 0.4,
  4: 0.4,
  5: 0.4,
  6: 0.6000000000000001,
  7: 0.4,
  8: 3.2,
  9: 2.0},
 'DateTimeUTC': {0: pd.Timestamp('2018-03-28 06:25:08'),
  1: pd.Timestamp('2018-03-28 06:25:49'),
  2: pd.Timestamp('2018-03-28 06:27:06'),
  3: pd.Timestamp('2018-03-28 06:32:11'),
  4: pd.Timestamp('2018-03-28 06:32:59'),
  5: pd.Timestamp('2018-03-28 06:34:02'),
  6: pd.Timestamp('2018-03-28 06:35:38'),
  7: pd.Timestamp('2018-03-28 06:37:04'),
  8: pd.Timestamp('2018-03-28 06:39:08'),
  9: pd.Timestamp('2018-03-28 06:40:52')}})

sns.scatterplot('DateTimeUTC', 'Depth', data=df)

image

I got the same results on different python versions, using seaborn 0.9.0

When lineplot is used instead, the problem does not occur.

Activity

  1. CRiddler commented on Jan 7, 2019

    @CRiddler
    Contributor

    The issue you're experiencing isn't entirely seaborn's fault. Plotting with dates is a little different than plotting "typical" numbers. I'd suggest diving down to matplotlib to plot your data if you want to plot dates. Here's an example that may help you out. Potentially to make seaborn more flexible, we could add a type check to check for a datetime dtype and call ax.plot_date instead of ax.scatter to account for this?

    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    
    df = pd.DataFrame({'Depth': [0.2, 0.4, 0.4, 0.4, 0.4, 0.4, 0.6, 0.4, 3.2, 2.0],
                       'DateTimeUTC': [pd.Timestamp('2018-03-28 06:25:08'),
                                       pd.Timestamp('2018-03-28 06:25:49'),
                                       pd.Timestamp('2018-03-28 06:27:06'),
                                       pd.Timestamp('2018-03-28 06:32:11'),
                                       pd.Timestamp('2018-03-28 06:32:59'),
                                       pd.Timestamp('2018-03-28 06:34:02'),
                                       pd.Timestamp('2018-03-28 06:35:38'),
                                       pd.Timestamp('2018-03-28 06:37:04'),
                                       pd.Timestamp('2018-03-28 06:39:08'),
                                       pd.Timestamp('2018-03-28 06:40:52')]})
    
    fig, axes = plt.subplots(3, 1, figsize=(10, 8))
    ax1, ax2, ax3 = axes.flatten()
    
    # seaborn plots an empty scatter to get the current color/size rotation
    ## Adding the scout changes the xaxis relative to when we don't add the scout
    scout = ax1.scatter([], [])
    scout.remove()
    
    # Seaborn converts plotting inputs to numpy arrays
    x = np.asarray(df.DateTimeUTC)
    y = np.asarray(df.Depth)
    
    ax1.scatter(x, y)
    ax1.set_title("Plotting scatter how seaborn does internally")
    ax1.text(1.01, 0.5, 
             s=("This is the plot that\n"
                "we first plotted an empty 'scout' on\n"
                "which is how seaborn does it"), 
              transform=ax1.transAxes)
     
    ax2.scatter(x, y)
    ax2.set_title("Plotting dates as a normal scatterplot")
    ax2.text(1.01, 0.5, 
             s=("This is the plot that\n"
                "we did not plot an empty 'scout' on\n"
                "however our xaxis doesnt look quite right\n"
                "just yet, meaning that the scout wasn't\n"
                "entirely at fault for the oddly aligned xaxis"), 
             transform=ax2.transAxes)
    
    ax3.plot_date(x, y)
    ax3.set_title("using Axes.plot_date to plot dates as a scatterplot")
    ax3.text(1.01, 0.5, 
             s=("We used plot_date to create this plot\n"
                "the xaxis is scaled correctly"), 
             transform=ax3.transAxes)   
    
    fig.tight_layout()
    

    download

  2. barashe commented on Jan 9, 2019

    @barashe
    Author

    Thank you, that is a good idea. You just forgot to add

    scout = ax3.scatter([], [])
    scout.remove()
    

    image

    Which improves the x-axis. You are correct that this is more of a matplotlib issue, but since seaborn gives a smoother experience to matplotlib, I think it would be great if the type check you mentioned will be implemented.

  3. mwaskom commented on Jan 13, 2019

    @mwaskom
    Owner

    Unfortunately that solution won’t work because plot_date is not a drop-in replacement for scatter in that it doesn’t allow control over the hue/size/style of the individual markers in the same way.

  4. ImportanceOfBeingErnest commented on Mar 9, 2019

    @ImportanceOfBeingErnest

    That is definitely a matplotlib issue. See matplotlib/matplotlib#7413. The problem is the autoscaling of collections.

  5. mwaskom commented on Mar 9, 2019

    @mwaskom
    Owner

    Closing as matplotlib issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions