0

I have a df with two columns, e.g. 'date', and 'sales';
I want to plot df['sales'] but in different style: e.g. plot in 'solid' line when ds<= and plot in 'dash dot' line for the rest ds.

dingx
  • 1,621
  • 3
  • 20
  • 38
  • I wonder if you can use line for this differentiation. More like using different markers for the check would be appropriate: https://stackoverflow.com/questions/43090817/matplotlib-scatter-plot-change-color-based-on-value-on-list – SKPS Feb 10 '20 at 04:25

1 Answers1

0

Maybe something like this? Note that lines go between the days, so they mark periods.


from matplotlib import pyplot as plt
import pandas as pd
import numpy as np

df = pd.DataFrame({'date': pd.date_range('20200201', periods=29, freq='D'), 'sales' : np.random.binomial(200, .1, 29)})
df['sales'][df.date <= '20200210'].plot(style='-', color='crimson')
df['sales'][df.date >= '20200210'].plot(style='-.', color='crimson')
ticks, _labels = plt.xticks()
ticks = ticks[1:-1]
plt.xticks(ticks=ticks, labels=df['date'][ticks].dt.strftime('%m/%d/%Y'))
plt.tight_layout()
plt.show()

resulting plot

JohanC
  • 71,591
  • 8
  • 33
  • 66