Python Pyplot:在 x 轴上使用百分比
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26294360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pyplot: using percentage on x axis
提问by Alexis Eggermont
I have a line chart based on a simple list of numbers. By default the x-axis is just the an increment of 1 for each value plotted. I would like to be a percentage instead but can't figure out how. So instead of having an x-axis from 0 to 5, it would go from 0% to 100% (but keeping reasonably spaced tick marks. Code below. Thanks!
我有一个基于简单数字列表的折线图。默认情况下,对于绘制的每个值,x 轴只是 1 的增量。我想成为一个百分比,但不知道如何。因此,x 轴不是从 0 到 5,而是从 0% 到 100%(但保持合理间隔的刻度线。下面的代码。谢谢!
from matplotlib import pyplot as plt
from mpl_toolkits.axes_grid.axislines import Subplot
data=[8,12,15,17,18,18.5]
fig=plt.figure(1,(7,4))
ax=Subplot(fig,111)
fig.add_subplot(ax)
plt.plot(data)
采纳答案by Mad Physicist
This is a few months late, but I have created PR#6251with matplotlib to add a new PercentFormatterclass. With this class you can do as follows to set the axis:
这已经晚了几个月,但我已经用 matplotlib创建了PR#6251来添加一个新PercentFormatter类。使用这个类,您可以按如下方式设置轴:
import matplotlib.ticker as mtick
# Actual plotting code omitted
ax.xaxis.set_major_formatter(mtick.PercentFormatter(5.0))
This will display values from 0 to 5 on a scale of 0% to 100%. The formatter is similar in concept to what @Ffisegydd suggests doing except that it can take any arbitrary existing ticks into account.
这将在 0% 到 100% 的范围内显示从 0 到 5 的值。格式化程序在概念上与@Ffisegydd 建议的类似,只是它可以考虑任何任意现有的刻度。
PercentFormatter()accepts three arguments, max, decimals, and symbol. maxallows you to set the value that corresponds to 100% on the axis (in your example, 5).
PercentFormatter()接受三个参数max,decimals、 和symbol。max允许您设置对应于轴上 100% 的值(在您的示例中,5)。
The other two parameters allow you to set the number of digits after the decimal point and the symbol. They default to Noneand '%', respectively. decimals=Nonewill automatically set the number of decimal points based on how much of the axes you are showing.
另外两个参数允许您设置小数点后的位数和符号。它们分别默认为None和'%'。decimals=None将根据您显示的轴数量自动设置小数点数。
Note that this formatter will use whatever ticks would normally be generated if you just plotted your data. It does not modify anything besides the strings that are output to the tick marks.
请注意,如果您只是绘制数据,此格式化程序将使用通常会生成的任何刻度。除了输出到刻度线的字符串之外,它不会修改任何内容。
Update
更新
PercentFormatterwas accepted into Matplotlib in version 2.1.0.
PercentFormatter在 2.1.0 版中被 Matplotlib 接受。
回答by Ffisegydd
The code below will give you a simplified x-axis which is percentage based, it assumes that each of your values are spaces equally between 0% and 100%.
下面的代码将为您提供一个基于百分比的简化 x 轴,它假定您的每个值都是介于 0% 和 100% 之间的空格。
It creates a percarray which holds evenly-spaced percentages that can be used to plot with. It then adjusts the formatting for the x-axis so it includes a percentage sign using matplotlib.ticker.FormatStrFormatter. Unfortunately this uses the old-style string formatting, as opposed to the new style, the old style docs can be found here.
它创建一个perc数组,其中包含可用于绘图的均匀间隔百分比。然后它会调整 x 轴的格式,以便使用matplotlib.ticker.FormatStrFormatter. 不幸的是,这使用了旧式字符串格式,而不是新式,旧式文档可以在这里找到。
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()



