Pandas and seaborn plot unexpected time frame on x axis

57 Views Asked by At

I'm attempting to create a simple scatter plot using pandas and seaborn. This code :

import pandas as pd 
import seaborn as sns

lst = ['2019-01-31', '2019-02-25', '2019-03-31'] 
lst2 = [11, 22, 33] 

df = pd.DataFrame(list(zip(lst, lst2)), 
               columns =['date', 'count']) 

df['date'] = pd.to_datetime(df['date'])
sns.scatterplot(x='date', y='count', data=df)

renders plot :

enter image description here

How can I remove the dates that do not have a corresponding count value ? The dataframe just contains year 2019 values but the plot renders back to 2000.

1

There are 1 best solutions below

0
On BEST ANSWER

You can use set_xlim to manually specify the range of the x-axes, e.g.:

f = sns.scatterplot(x='date', y='count', data=df)
f.set_xlim('2019-01-01', '2019-12-31')