我有一个已经用Pandas创建的图:
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轴上使用百分比