0

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).

1
  • 1
    Right, because a line is either solid or dashed, not both. But clearly you can plot two lines, one solid, one dashed. Commented Jul 10, 2019 at 15:48

1 Answer 1

1

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 = '--')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.