首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在python中绘制熊猫系列的CDF

在python中绘制熊猫系列的CDF
EN

Stack Overflow用户
提问于 2014-08-30 07:05:47
回答 6查看 92.9K关注 0票数 60

有没有办法做到这一点?我似乎不是一种简单的方法来连接pandas系列和绘制CDF。

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2014-10-16 07:57:58

我相信您正在寻找的功能是在Series对象的hist方法中,它包装了matplotlib中的hist()函数

以下是相关文档

代码语言:javascript
运行
复制
In [10]: import matplotlib.pyplot as plt

In [11]: plt.hist?
...
Plot a histogram.

Compute and draw the histogram of *x*. The return value is a
tuple (*n*, *bins*, *patches*) or ([*n0*, *n1*, ...], *bins*,
[*patches0*, *patches1*,...]) if the input contains multiple
data.
...
cumulative : boolean, optional, default : False
    If `True`, then a histogram is computed where each bin gives the
    counts in that bin plus all bins for smaller values. The last bin
    gives the total number of datapoints.  If `normed` is also `True`
    then the histogram is normalized such that the last bin equals 1.
    If `cumulative` evaluates to less than 0 (e.g., -1), the direction
    of accumulation is reversed.  In this case, if `normed` is also
    `True`, then the histogram is normalized such that the first bin
    equals 1.

...

例如

代码语言:javascript
运行
复制
In [12]: import pandas as pd

In [13]: import numpy as np

In [14]: ser = pd.Series(np.random.normal(size=1000))

In [15]: ser.hist(cumulative=True, density=1, bins=100)
Out[15]: <matplotlib.axes.AxesSubplot at 0x11469a590>

In [16]: plt.show()
票数 87
EN

Stack Overflow用户

发布于 2015-08-13 00:57:35

CDF或累积分布函数图基本上是一个在X轴上具有排序值,在Y轴上具有累积分布的图形。因此,我将创建一个新的序列,将排序后的值作为索引,将累积分布作为值。

首先创建一个示例系列:

代码语言:javascript
运行
复制
import pandas as pd
import numpy as np
ser = pd.Series(np.random.normal(size=100))

对序列进行排序:

代码语言:javascript
运行
复制
ser = ser.sort_values()

现在,在继续之前,再次追加最后一个(也是最大的)值。为了获得无偏的CDF,这一步对于小样本尤其重要:

代码语言:javascript
运行
复制
ser[len(ser)] = ser.iloc[-1]

创建一个新系列,将排序后的值作为索引,将累积分布作为值:

代码语言:javascript
运行
复制
cum_dist = np.linspace(0.,1.,len(ser))
ser_cdf = pd.Series(cum_dist, index=ser)

最后,将函数绘制为以下步骤:

代码语言:javascript
运行
复制
ser_cdf.plot(drawstyle='steps')
票数 15
EN

Stack Overflow用户

发布于 2016-09-22 07:52:51

这是最简单的方法。

代码语言:javascript
运行
复制
import pandas as pd
df = pd.Series([i for i in range(100)])
df.hist( cumulative = True )

Image of cumulative histogram

票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25577352

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档