Reputation: 518
Is it possible to format the tick labels in a pandas.DataFrame.plot()
without importing the matplotlib.ticker
library?
I've come to realise that pandas
has many native functions I'm unaware of and I like to use them where possible — if only to simplify my code. And yes, pandas
leans on matplotlib
anyway, but I want to know to what extent I can style plots without directly invoking matplotlib
functions.
For example, can I change the tick labels on this chart to percentages without matplotlib.ticker.Percentformatter()
?
import pandas as pd
import numpy as np
import matplotlib as plt
df = pd.DataFrame(columns=["value"], data=np.random.rand(5))
ax = df.plot.barh()
ax.xaxis.set_major_formatter(plt.ticker.PercentFormatter(1))
plt.show()
Upvotes: 0
Views: 1878
Reputation: 138
You can set a custom function as a string formatter with set_major_formatter
.
For example:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = pd.DataFrame(columns=["value"], data=np.random.rand(5))
ax = df.plot.barh()
func = lambda x, pos: f"{int(x*100)}%"
ax.xaxis.set_major_formatter(func)
plt.show()
Upvotes: 1