【发布时间】:2017-02-06 16:33:05
【问题描述】:
有谁知道如何更改 X 轴刻度和刻度以显示如下图所示的百分位数分布?此图片来自 MATLAB,但我想使用 Python(通过 Matplotlib 或 Seaborn)来生成。
从@paulh 的指针来看,我现在离我更近了。这段代码
import matplotlib
matplotlib.use('Agg')
import numpy as np
import matplotlib.pyplot as plt
import probscale
import seaborn as sns
clear_bkgd = {'axes.facecolor':'none', 'figure.facecolor':'none'}
sns.set(style='ticks', context='notebook', palette="muted", rc=clear_bkgd)
fig, ax = plt.subplots(figsize=(8, 4))
x = [30, 60, 80, 90, 95, 97, 98, 98.5, 98.9, 99.1, 99.2, 99.3, 99.4]
y = np.arange(0, 12.1, 1)
ax.set_xlim(40, 99.5)
ax.set_xscale('prob')
ax.plot(x, y)
sns.despine(fig=fig)
生成以下图(注意重新分布的 X 轴)
我发现它比标准量表更有用:
我联系了原始图表的作者,他们给了我一些指示。它实际上是一个对数比例图,x 轴反转,值为 [100-val],手动标记 x 轴刻度。下面的代码使用与此处其他图表相同的示例数据重新创建原始图像。
import matplotlib
matplotlib.use('Agg')
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
clear_bkgd = {'axes.facecolor':'none', 'figure.facecolor':'none'}
sns.set(style='ticks', context='notebook', palette="muted", rc=clear_bkgd)
x = [30, 60, 80, 90, 95, 97, 98, 98.5, 98.9, 99.1, 99.2, 99.3, 99.4]
y = np.arange(0, 12.1, 1)
# Number of intervals to display.
# Later calculations add 2 to this number to pad it to align with the reversed axis
num_intervals = 3
x_values = 1.0 - 1.0/10**np.arange(0,num_intervals+2)
# Start with hard-coded lengths for 0,90,99
# Rest of array generated to display correct number of decimal places as precision increases
lengths = [1,2,2] + [int(v)+1 for v in list(np.arange(3,num_intervals+2))]
# Build the label string by trimming on the calculated lengths and appending %
labels = [str(100*v)[0:l] + "%" for v,l in zip(x_values, lengths)]
fig, ax = plt.subplots(figsize=(8, 4))
ax.set_xscale('log')
plt.gca().invert_xaxis()
# Labels have to be reversed because axis is reversed
ax.xaxis.set_ticklabels( labels[::-1] )
ax.plot([100.0 - v for v in x], y)
ax.grid(True, linewidth=0.5, zorder=5)
ax.grid(True, which='minor', linewidth=0.5, linestyle=':')
sns.despine(fig=fig)
plt.savefig("test.png", dpi=300, format='png')
这是结果图:
【问题讨论】:
-
您是否自己编写过任何代码或付出任何努力?如果是,请在此处发布。
-
我完全不明白为什么这个问题被关闭为太宽泛。虽然它缺乏一个好的问题描述,但从图表中看问题本身就很明显了。如果有办法生成这种图表,肯定只需要几行代码,所以答案既不会太长,也不会期望有太多可能的答案。
-
@Chris Osterwood 请提供生成这种图形的 matlab 命令,并以文本形式提供清晰的问题描述,而不仅仅是张贴图片。您可以通过将它们发布为评论来做到这一点,这样更有经验的用户可以将它们合并到问题中。
-
我想你想在我的库上使用:phobson.github.io/mpl-probscale
-
@PaulH - 非常感谢!我已经使用 mpl-probscale 用代码编辑了我的问题,它更接近我想要的。
标签: matplotlib seaborn