Issue
I have the following dataframe:
df =pd.DataFrame( {'Area': ['Water', 'Water', 'Water', 'Water', 'Water'],
'Year': ['2021', '2022', '2023', '2024', '2025'],
'Amount': [1530.72, 1925.39, 1700.00, 2000.00, 2160.00],
'Type': ['Actuals', 'Actuals', 'Actuals', 'Budget', 'Budget']})
I'd like to plot make a line plot with the Year on the X axis and the amount on the Y axis. I'd like the linetype for the 'Actuals' to be solid, and to be dotted for the 'Budget'.
I can almost do this, but I get I am missing the connection between 2023 and 2024.
plt.plot('Year', 'Amount', data=df[df['Type'] == 'Actuals'], marker='o', color='grey', linewidth=2)
plt.plot('Year', 'Amount', data=df[df['Type'] == 'Budget'], marker='o', color='grey', linewidth=2, linestyle='--')
What am I doing wrong here?
Solution
As pointed out by @Gaberocksall in the comments, you are not asking matplotlib
to draw line from 2023
to 2024
, which is why it doesn't exist in your plot.
You could get the result you want by expanding the Budget
dataset to include the last line of the Actuals
dataset:
plt.plot('Year',
'Amount',
data=df[df['Type'] == 'Actuals'],
marker='o', color='grey', linewidth=2
)
plt.plot('Year',
'Amount',
data=pd.concat([df[df['Type']=='Actuals'].iloc[-1:], df[df['Type'] == 'Budget']]),
marker='o', color='grey', linewidth=2, linestyle='--'
)
plt.show()
Answered By - Nick
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.