Issue
The following code plots a horizontal bar chart in a decreasing order. I would like to change the colors of the bars, so they fade out as the values decrease. In this case California will stay as it is, but Minnesota will be very light blue, almost transparent. I know that I can manually hardcode the values in a list of colors, but is there a better way to achieve this?
x_state = df_top_states["Percent"].nlargest(10).sort_values(ascending=True).index
y_percent = df_top_states["Percent"].nlargest(10).sort_values(ascending=True).values
plt_size = plt.figure(figsize=(9,6))
plt.barh(x_state, y_percent)
plt.title("Top 10 States with the most number of accidents (2016 - 2021)", fontsize=16)
plt.ylabel("State", fontsize=13)
plt.yticks(size=13)
plt.xlabel("% of Total Accidents", fontsize=13)
plt.xticks(size=13)
plt.tight_layout()
plt.show()
Solution
You could create a list of colors with decreasing alpha from the list of percentages. Here is some example code:
import matplotlib.pyplot as plt
from matplotlib.colors import to_rgba
import seaborn as sns # to set the ' darkgrid' style
import pandas as pd
import numpy as np
sns.set_style('darkgrid')
# first, create some suitable test data
df_top_states = pd.DataFrame({"Percent": np.random.rand(20)**3},
index=["".join(np.random.choice([*'abcdef'], np.random.randint(3, 9))) for _ in range(20)])
df_top_states["Percent"] = df_top_states["Percent"] / df_top_states["Percent"].sum() * 100
df_largest10 = df_top_states["Percent"].nlargest(10).sort_values(ascending=True)
x_state = df_largest10.index
y_percent = df_largest10.values
max_perc = y_percent.max()
fig = plt.figure(figsize=(9, 6))
plt.barh(x_state, y_percent, color=[to_rgba('dodgerblue', alpha=perc / max_perc) for perc in y_percent])
plt.title("Top 10 States with the most number of accidents (2016 - 2021)", fontsize=16)
plt.ylabel("State", fontsize=13)
plt.yticks(size=13)
plt.xlabel("% of Total Accidents", fontsize=13)
plt.xticks(size=13)
plt.margins(y=0.02) # less vertical margin
plt.tight_layout()
plt.show()
PS: Note that plt.figure(...)
returns a matplotlib figure, not some kind of size element.
Answered By - JohanC
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.