简体   繁体   中英

How to plot the data on two y-axis in pandas or matplotlib?

I have a data frame which is a combination of FER (Facial Emotion Recognition) and Mood prediction

Now, the mood prediction dataset has two columns - Question and Prediction. The question represents three values [1 - Activation; 2 - Pleasance; 5- Stress] and the prediction column also has three values [0 - Low; 1 - Medium; 2 - High]. The index consists of timestamps.

I'd like to give a brief explanation about the screenshot 3 below. Let's consider the third row where the question value is 5 and the prediction value is 1. This indicates stress (5) of medium (1) level.

How can I plot the prediction of question values over time? I tried to do it but I am getting just one line for everything.

d = one.groupby('question')
dunk1 = d.get_group(1)
fig, ax1 = plt.subplots(figsize = (20,5))
x = one.index
y = one.prediction
ax1.plot(x,y,'r-')

Plot of my attempted code

图片

I am looking to get an output that looks something like the following:

图片

Screenshot of the dataset

数据集

You are plotting x and y from the original dataframe, not the grouped dataframe, you should be doing

d = one.groupby('question')
dunk1 = d.get_group(1)
fig, ax1 = plt.subplots(figsize = (20,5))
x = dunk1.index
y = dunk1.prediction
ax1.plot(x,y,'r-')

Or to plot all three question groups

d = one.groupby('question')
fig, ax = plt.subplots(figsize = (20,5))
for k in d.groups.keys():
    group = d.get_group(k)
    ax.plot(group.index, group.prediction)

But understand that this may not get you all the way to the result you want - there may be more filtering or sorting necessary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM