Reputation: 1511
I can draw a plot with two lines using the below code no problem.
# libraries
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
# Data
df=pd.DataFrame({'dates': ['2001','2002','2003','2030'], 'census_people': [306,327,352,478], 'census_houses': [150,200,249,263]})
# multiple line plot
plt.plot('dates', 'census_people', data=df, marker='o', color='green', linewidth=2)
plt.plot('dates','census_houses',data=df,marker='o',color='orange',linewidth=2)
My question is, I want both lines to be solid, EXCEPT for the part of the line between 2003 and 2030, which I want to be dashed (as it is a projection of what will happen in the future). So something like this, where the past data (i.e. the line connecting the first three data points) is a solid line, and the future projected data (i.e. the line connecting the third and fourth data point) is a dashed line.
(I can find lots of examples of plotting a dashed line, just not plotting solid and dashed on the same line).
Upvotes: 0
Views: 7254
Reputation: 2763
I don't know if there's a way to do it how you seem to want to do it, but what I would do is plot them separately:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
# Data
df=pd.DataFrame({'dates': [2001, 2002, 2003, 2030], 'census_people': [306,327,352,478], 'census_houses': [150,200,249,263]}) #I changed the dates from strings to ints
# multiple line plot
plt.plot('dates', 'census_people', data=df[df['dates'] < 2004], marker='o', color='green', linewidth=2)
plt.plot('dates', 'census_people', data=df[df['dates'] > 2002], marker='o', color='green', linewidth=2, linestyle = '--')
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 = '--')
Upvotes: 1