I am using matplotlib to draw a plot. What I want to achieve is to connect points if one condition is met. For instance, if I have a dataframe like the following:
import os
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
df=pd.DataFrame({'dates': [2001, 2002, 2003, 2004, 2005, 2006], 'census_people': [306,327,352,478,250, 566], 'census_houses': [150,200,249,263, 180, 475]}) #I changed the dates from strings to ints
I could create plots like this use the following codes:
plt.plot('dates','census_houses',data=df[df['dates'] < 2004] ,marker='o',color='orange', linewidth=2)
plt.plot('dates','census_houses',data=df[df['dates'] > 2002] ,marker='o',color='orange', linewidth=2, linestyle = '--')
The plot is like the following:
However, what I truely want is, for instance, use the dashed line to connect points if the census_houses
is bigger than 250. How to achieve this using matplotlib? Any suggestions and insights are welcomed! Thank you~