【发布时间】:2015-09-30 04:43:44
【问题描述】:
我有一个用熊猫创建的现有情节,如下所示:
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: using percentage on x axis
【问题讨论】:
-
您能否将您接受的答案更改为在 matplotlib 中本地实现的方法? stackoverflow.com/a/36319915/1840471
标签: python pandas matplotlib plot