You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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()
Thank you, that is a good idea. You just forgot to add
scout = ax3.scatter([], [])
scout.remove()
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.
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.
Activity
CRiddler commentedon Jan 7, 2019
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_dateinstead ofax.scatterto account for this?barashe commentedon Jan 9, 2019
Thank you, that is a good idea. You just forgot to add
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.
mwaskom commentedon Jan 13, 2019
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.
ImportanceOfBeingErnest commentedon Mar 9, 2019
That is definitely a matplotlib issue. See matplotlib/matplotlib#7413. The problem is the autoscaling of collections.
mwaskom commentedon Mar 9, 2019
Closing as matplotlib issue.