Update
From the OP's image, it looks like there may be some spurious or earlier dates in the df
. When I try with the example data provided, it works just fine. Here is a way to ensure the data is clean:
df = df.assign(
Date=pd.to_datetime(df['Date'])
).set_index('Date').sort_index()
# then, truncate anything before year 2020 in the plot:
ax = sns.scatterplot(
x='Date', y='Col2', hue='Label',
data=df.truncate(before='2020-01-01').groupby('Date').count())
# additionally, enforce a desired date format
from matplotlib.dates import DateFormatter
ax.xaxis.set_major_formatter(DateFormatter("%Y-%m-%d"))
ax.xaxis.set_tick_params(rotation=30)
Outcome (based on the example data):
Original answer:
Why not use sns.barplot
?
sns.barplot(x='Date', y='Col2', hue='Label', data=df_plot)
But personally, in that case I would prefer just making a series, and using the builtin pandas.plot
:
df.assign(
Date=pd.to_datetime(df['Date'])
).groupby(['Date']).size().plot.bar()
or, if you want a scatterplot:
df.assign(
Date=pd.to_datetime(df['Date'])
).groupby(['Date']).size().plot(style='o')
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…