Here’s a function that generates a barh graph and displays a bar label
import pandas as pd
import seaborn as sns
sns.set_theme(style='white')
def plot_barh(s, unit='', title='', f=0):
ax1 = s.plot(kind='barh', figsize=(6, 3),
width=.8, title=title)
ax1.bar_label(ax1.containers[0], padding=10, color='black',
fontsize=10, fmt='{:.' + str(f) + 'f} ' + unit)
ax1.set_xticks([])
sns.despine(bottom=True, left=True)
Plot sample data s
, in a graph like this
s = pd.Series({'John': 7, 'Amy': 4, 'Elizabeth': 4, 'James': 4, 'Roy': 2})
plot_barh(s)
Decimals and units can also be represented
plot_barh(s, unit='point',f=1)
fmt='{:.' + str(f) + 'f} ' + unit
code in plot_barh
func make this, but I’m struggling to find a more elegant way to express the argument of fmt
.
I’ve tried f-string
or the format
function, but I get an error.
Am I using something wrong?
Or are f-string
or format
functions not supported?
and if not, why not?