Slartibartfast
Slartibartfast

Reputation: 1190

How to round to 1 floating point in matplotlib

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.

Upvotes: 1

Views: 681

Answers (2)

Scott Boston
Scott Boston

Reputation: 153460

You can try using labels:

ax.bar_label(ax.containers[0], labels=df['A'].round(1))

Upvotes: 1

Rabinzel
Rabinzel

Reputation: 7903

Try this:

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

Upvotes: 2

Related Questions