line_color = "k"
column_color_dict = {'OpenPlay': "royalblue",
'Penalty': "teal",
'DirectFreekick': "darkorchid",
'FromCorner': "magenta",
'SetPiece': "gold"
}
with plt.style.context("Solarize_Light2"):
plt.rcParams['font.family'] = 'Palatino Linotype' ##set global font
fig, ax = plt.subplots(figsize=(12, 8))
ax = g[column_color_dict.keys()].iloc[::-1].plot.barh(stacked=True, ax=ax, color=column_color_dict,
ec="silver", legend=None)
for spine in ["top", "right"]:
ax.spines[spine].set_visible(False)
ax.grid(False)
ax.set_position([0.08, 0.08, 0.82, 0.74]) ## make space for the title on top of the axes
for num, total in enumerate(g["total"].iloc[::-1]):
ax.text(total+2, num, int(total), va='center', ha='left', fontsize=15)
## labels, titles and subtitles
ax.set(ylabel=" ", xlabel="Goals")
ax.xaxis.label.set(fontsize=12, fontweight='bold', color=line_color)
for label in ax.get_yticklabels():
label.set(color=line_color, fontsize=12)
for t in ax.xaxis.get_ticklabels():
t.set_color(line_color)
## titles, subtitles, and custom legend (Peter McKeever style)
fig.text(x=0.08, y=0.92, s="How are Europe's top strikers scoring their goals?",
ha='left', fontsize=20, fontweight='bold', color=line_color,
path_effects=[pe.Stroke(linewidth=1.1, foreground='0.1'),
pe.Normal()])
fig.text(x=0.08, y=0.883, s="Top 5 Leagues | 2014-2018",
ha='left', fontsize=16, fontweight='bold', color=line_color,
path_effects=[pe.Stroke(linewidth=1.1, foreground='0.1'),
pe.Normal()])
fig_text(x=0.08, y=0.872, ha='left',
fontsize=14, fontweight='bold',
color=line_color,
s='<Open Play> | <Penalty> | <Direct Freekick> | <Corner> | <Other Set-pieces>',
highlight_textprops=[{"color": "royalblue"},
{"color": "teal"},
{"color": "darkorchid"},
{"color": "magenta"},
{"color": "gold"}]
)
fig.savefig("shooter-bar-chart", dpi=180) ##save image