使用matplotlib在X轴上的百分位数 [英] Percentiles on X axis with matplotlib

查看:44
本文介绍了使用matplotlib在X轴上的百分位数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要绘制的浮点数排序数组.我想在 x 轴上打印百分位数.我知道如何获取百分位值.

I have a sorted numpy array of floats that I want to plot. I would like to print percentiles on the x axis. I know how to get the percentile values.

这里是我所拥有的,但是我不知道如何处理我的百分位值数组:

Here is what I have, but I don't know what to do with my array of percentile values:

import numpy as np
import pylab as P
import matplotlib as mp

d = np.loadtxt("report.stats.sorted")
pct = mp.mlab.prctile(d, p=(0.0, 10.0, 25.0, 50.0, 75.0, 90.0, 100.0))

P.plot(d)
P.show()

该图显示了一条很好的曲线,但 x 轴显示了条目数的刻度.我想要的是在适当位置显示 [0.0, 25.0, 50.0, 75.0, 100.0] 的刻度.抱歉,如果不清楚.

The plot shows a nice curve but the x axis is showing ticks with the number of entries. What I want is ticks showing [0.0, 25.0, 50.0, 75.0, 100.0] at the appropriate location. Sorry if it's not clear.

推荐答案

如果您只想更改xticklabel以显示百分位等级,则可以将刻度的位置设置为绘制数组长度的百分比,并将标签设置为百分等级:

If you simply want to change the xticklabels to show the percentile ranks, you can set the location of the ticks as the percentage of the length of the plotted array, and set the labels as the percentile ranks:

from matplotlib import mlab
import matplotlib.pyplot as plt
import numpy as np

d = np.sort(np.random.randint(0,1000,1000)).cumsum()

# Percentile values
p = np.array([0.0, 25.0, 50.0, 75.0, 100.0])

perc = mlab.prctile(d, p=p)

plt.plot(d)
# Place red dots on the percentiles
plt.plot((len(d)-1) * p/100., perc, 'ro')

# Set tick locations and labels
plt.xticks((len(d)-1) * p/100., map(str, p))

plt.show()

这篇关于使用matplotlib在X轴上的百分位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆