Question: How do I highlight one bar in a bar graph in Python? I have a Dataframe with two columns : Year (years from 1994-2020) and Fatalities (number of car crash fatalities per year).
I'm trying to plot a bar graph and highlight the year 2020.
Code I have tried
fig, ax = plt.subplots()
colors = []
if fatality_94_20[fatality_94_20['Year']==2020]:
colors = 'orange'
else:
colors = 'cornflowerblue'
with sns.axes_style("whitegrid"):
ax = ax.bar(fatality_94_20['Year'], fatality_94_20['Fatalities'], color = 'colors')
plt.title('Total Car Crash Fatalities \n per Year', x = .50, y = 1.00, size = 18)
plt.xlabel('Years', size = 16)
plt.ylabel('Fatalities', size = 16)
plt.xticks(rotation = 90, size = 14)
plt.yticks(size = 14)
plt.show()
I am getting the error: ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Here is a screenshot of the graph without the highlighted bar. :
When I do just the code:
fatality_94_20[fatality_94_20['Year']==2020]
I do get the correct data. Thanks