Matt M
Matt M

Reputation: 309

Seaborn / MatplotLib Axis and Data Values formatting: Hundreds, Thousands, and Millions

I have a problem, which, as far as I know hasn't been solved yet.

I need formatting of my axes and data points for my Seaborn / Matplotlib graphs to be dynamic. An example of what I'd like to achieve is below (done via Keynote, I used a log scale to make the point clearer).

What would be the best way to go about it? I saw answers that format either as Ks or Ms, but never as both. Am I missing something?

enter image description here

Right now I am using the FuncFormatter option

if options.get('ctype') == 'number':

    df = df.loc[df[ycat2] >= 1].copy()
    plt.figure(figsize=(14,7.5))
    plot = sns.barplot(xcat1, ycat2, data=df, color='#00c6ff', saturation=1, ci=None)

    sns.despine(top = True, right = True, left=True)
    #plot.set_title('Instagram - Engagement Rate', fontweight='bold',y=1.04, loc = 'left', fontsize=12)

    plot.yaxis.grid(True)
    plot.yaxis.get_major_ticks()[0].label1.set_visible(False)
    plot.yaxis.set_major_formatter(FuncFormatter(lambda y, _: '{:,}'.format(int(y))))
    plot.set_xlabel('')
    plot.set_ylabel('')
    plot.tick_params(axis="x", labelsize=13)
    plot.tick_params(axis="y", labelsize=13)

    for i, bar in enumerate(plot.patches):

        h = bar.get_height()

        plot.text(
            i, 
            h,
            '{:,}'.format(int(h)),
            ha='center', 
            va='bottom',
            fontweight='heavy',
            fontsize=12.5)


    return plot.figure

Upvotes: 1

Views: 1465

Answers (1)

Diziet Asahi
Diziet Asahi

Reputation: 40697

Matplotlib has an Engineering formatter for specifically this purpose. You can use it to format the axis (using set_major_formatter()) or to format any number, using EngFormatter.format_eng()

from matplotlib.ticker import EngFormatter
fmt = EngFormatter(places=0)

y = np.arange(1,10)
data = np.exp(3*y)
fig, ax = plt.subplots()
ax.set_xscale('log')
bars = ax.barh(y=y, width=data)
ax.xaxis.set_major_formatter(fmt)

for b in bars:
    w = b.get_width()
    ax.text(w, b.get_y()+0.5*b.get_height(),
            fmt.format_eng(w),
            ha='left', va='center')

enter image description here

Upvotes: 2

Related Questions