我有与熊猫这样创建的现有情节:格式Y轴为百分之
df['myvar'].plot(kind='bar')
y轴是浮点格式,我想y轴改为百分比。所有我发现使用ax.xyz语法和我只能将高于线下的代码解决方案的创建情节(我不能添加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
当您交互式地平移/缩放图形时,这会产生不希望的效果 – hitzg
比尝试使用'matplotlib.ticker'函数格式化程序容易百万倍! – Jarad