0

I am working with the python empyrical-dist package to plot a CDF of speed distribution with respect to traval mode (multi-class).

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        |
+---+---------+----------+----------+-------+--------------+------------+

now what to plot the CDF of speed column for each mode in travelmode. So,

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

How do I then add legend to each plot so I can tell which plot is for what mode?

2 Answers 2

1

Use matplotlib's pyplot.legend command:

plt.legend(data.groupby('travelmode').groups.keys())
Sign up to request clarification or add additional context in comments.

2 Comments

Could you please shade more light to your answer?
If you add plt.legend(data.groupby('travelmode').groups.keys()) to your decorate_cdf(title, x, y) function, you should get a legend with the group names so you know which plot if for what mode.
0

You can simply add the argument "label = " to the plot method associated with Cdf, like this:

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

or pass a list in your case instead of 'metro'

1 Comment

Welcome to StackOverflow. When including code in a question, answer, or comment, you should use backticks (` `) or code blocks to format it so it is easier to read. See more information here: stackoverflow.com/editing-help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.