熊猫为多类列绘制CDF

时间:2019-12-02 11:02:02

标签: python pandas cdf

我正在使用python empyrical-dist软件包来绘制关于行进模式(多类)的速度分布CDF

data.head()
+---+---------+----------+----------+-------+--------------+------------+
|   | trip_id | distance | duration | speed | acceleration | travelmode |
+---+---------+----------+----------+-------+--------------+------------+
| 0 |  303637 | 5.92     | 0.51     | 3.20  | 0.00173      | metro      |
| 1 |  303638 | 3.54     | 0.22     | 4.44  | 0.00557      | bus        |
| 2 |  303642 | 4.96     | 0.20     | 6.84  | 0.00944      | car        |
| 3 |  303662 | 6.53     | 0.97     | 1.86  | 0.00053      | foot       |
| 4 |  303663 | 40.23    | 0.94     | 11.85 | 0.00349      | car        |
+---+---------+----------+----------+-------+--------------+------------+

现在为CDF中的每种模式绘制speed列中travelmode列的内容。所以,

from empiricaldist import Cdf

def decorate_cdf(title, x, y):
    """Labels the axes.

    title: string
    """
    plt.xlabel(x)
    plt.ylabel(y)
    plt.title(title)

for name, group in data.groupby('travelmode'):
    Cdf.from_seq(group.speed).plot()

title, x, y = 'Speed by mode','speed (km/h)', 'CDF'
decorate_cdf(title,x,y)

enter image description here

然后如何为每个图添加图例,以便我可以确定哪个图适用于哪种模式?

2 个答案:

答案 0 :(得分:1)

使用matplotlib的pyplot.legend命令:

plt.legend(data.groupby('travelmode').groups.keys())

答案 1 :(得分:0)

您只需将参数“ label =”添加到与Cdf相关联的plot方法中,就像这样:

Cdf.from_seq(group.speed).plot(label = 'metro') 

或根据您的情况传递列表,而不是“地铁”