如何将熊猫条形图xaxis格式化为百分比

时间:2020-04-09 10:01:59

标签: python pandas matplotlib

我正在绘制一个表示分布的熊猫系列。指数是销售变化的价值,而价值是它的概率密度。我想将此绘制为条形图,并将两个轴的格式都设置为百分比。这与PercentFormatter的y轴(以及绘制折线图时的两个轴)的预期效果相同,但对x轴却无效。有没有办法在条形图中的x轴上使用PercentFormatter

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.ticker as mtick

s = pd.Series([-0.5,0.5],index=[-0.5,0.5])

fig,ax = plt.subplots()
s.plot.bar(ax=ax)
ax.yaxis.set_major_formatter(mtick.PercentFormatter(1))
ax.xaxis.set_major_formatter(mtick.PercentFormatter(1))

fig,ax = plt.subplots()
s.plot(ax=ax)
ax.yaxis.set_major_formatter(mtick.PercentFormatter(1))
ax.xaxis.set_major_formatter(mtick.PercentFormatter(1))

bar plot with wrong x-axis

line plot with correct x-axis

0 个答案:

没有答案