How to round to 1 floating point in matplotlib

Question:

I have the following code to plot the bar graph.

import pandas.util.testing as testing
from pandas import DataFrame as df
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
# import matplotlib.pyplot.bar_label as bar_label
import warnings
warnings.filterwarnings("ignore")
df = testing.makeTimeDataFrame(freq='MS')
ax = df.A.plot(kind='bar')
ax.bar_label(ax.containers[0]);

It produces this plot:

enter image description here

How can round it to 1 floating point. Using a format such as this "{:.1f}".format(45.34531). I am unsure of how to apply it. Without it, the plot is illegible.

Asked By: Slartibartfast

||

Answers:

Try this:

ax.bar_label(ax.containers[0], fmt='%.1f') 
Answered By: Rabinzel

You can try using labels:

ax.bar_label(ax.containers[0], labels=df['A'].round(1))
Answered By: Scott Boston
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.