累积分布图python [英] cumulative distribution plots python
问题描述
我正在使用python做一个项目,其中有两个数据数组.我们称它们为 pc 和 pnc .我需要在同一张图上绘制这两者的累积分布.对于 pc ,它应该小于图,即在(x,y)处, pc 中的y点的值必须小于x.对于 pnc ,它必须大于图,即在(x,y), pnc 中的y点的值必须大于x.
I am doing a project using python where I have two arrays of data. Let's call them pc and pnc. I am required to plot a cumulative distribution of both of these on the same graph. For pc it is supposed to be a less than plot i.e. at (x,y), y points in pc must have value less than x. For pnc it is to be a more than plot i.e. at (x,y), y points in pnc must have value more than x.
我尝试使用直方图功能-pyplot.hist
.有没有更好,更轻松的方式来做我想做的事?而且,它必须在x轴上以对数比例绘制.
I have tried using histogram function - pyplot.hist
. Is there a better and easier way to do what i want? Also, it has to be plotted on a logarithmic scale on the x-axis.
推荐答案
您已经关闭.您不应该将plt.hist用作numpy.histogram,它既可以提供值又可以提供bin,而您可以轻松地绘制累积量:
You were close. You should not use plt.hist as numpy.histogram, that gives you both the values and the bins, than you can plot the cumulative with ease:
import numpy as np
import matplotlib.pyplot as plt
# some fake data
data = np.random.randn(1000)
# evaluate the histogram
values, base = np.histogram(data, bins=40)
#evaluate the cumulative
cumulative = np.cumsum(values)
# plot the cumulative function
plt.plot(base[:-1], cumulative, c='blue')
#plot the survival function
plt.plot(base[:-1], len(data)-cumulative, c='green')
plt.show()
这篇关于累积分布图python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!