How to add labels with categorical variables to a line chart in python?

79 Views Asked by At

I have a dataset containing UAV data. I want to create a graph showing the altitude of the drone and the flight phase. On that graph I want to label the phases of flight (so take off, cruising, landing, etc). I can easily make the graph showing altitude, but I have no idea how to add in the flight phases. Could anyone help? I would like the flight phase to be visible on the altitude line graph. This could be either through colour coding, or through labels which signify when the flight phase starts/changes.

My data looks something like this (edited to add more data for clarification):

time (s) phase altitude
1 take off 10
2 take off 20
50 cruise 500
51 cruise 500
200 landing 20
201 landing 10
202 take off 10
203 take off 20
250 cruise 500
251 cruise 500
300 landing 20
301 landing 10
1

There are 1 best solutions below

5
Pavel Kochkin On

If you have data like this:

import pandas as pd

data = pd.DataFrame({
    "time": [i for i in range(1, 201)],
    "phase": ["take off"]*50 + ["cruise"]*100 + ["landing"]*50,
    "altitude": [i*10 for i in range(1, 51)] + [500]*101 + [i*10 for i in range(49, 0, -1)],
})
data

Output:


time    phase   altitude
0   1   take off    10
1   2   take off    20
2   3   take off    30
3   4   take off    40
4   5   take off    50
... ... ... ...
195 196 landing     50
196 197 landing     40
197 198 landing     30
198 199 landing     20
199 200 landing     10

You can try this:

import seaborn as sns
sns.lineplot(x="time", y="altitude", data=data, hue="phase")

Output:

enter image description here