我有一个现有的用熊猫创建的图,像这样:
df['myvar'].plot(kind='bar')
y轴是浮点格式我想把y轴改为百分比。我找到的所有解都用了ax。xyz语法,我只能把代码放在下面的线上面创建的情节(我不能添加ax=ax到上面的行。)
如何在不改变上面的直线的情况下将y轴格式化为百分比?
以下是我找到的解决方案,但需要我重新定义情节:
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.ticker as mtick
data = [8,12,15,17,18,18.5]
perc = np.linspace(0,100,len(data))
fig = plt.figure(1, (7,4))
ax = fig.add_subplot(1,1,1)
ax.plot(perc, data)
fmt = '%.0f%%' # Format you want the ticks, e.g. '40%'
xticks = mtick.FormatStrFormatter(fmt)
ax.xaxis.set_major_formatter(xticks)
plt.show()
链接到上述解决方案:Pyplot:使用x轴上的百分比