0

I have the following table:

Time Type Usage1 [%] Usage2 [%]
2021-07-09 09:00 DST LG1 60.0581 87.4926
2021-07-09 09:00 DST LG2 42.1409 40.57
2021-07-09 09:00 DST LG3 63.433 49.9326
2021-07-09 10:00 DST LG1 53.6577 86.6658
2021-07-09 10:00 DST LG2 36.384 41.7439
2021-07-09 10:00 DST LG3 54.5699 54.0306
2021-07-10 09:00 DST LG1 35.2818 75.8487
2021-07-10 09:00 DST LG2 34.101 37.7934
2021-07-10 09:00 DST LG3 50.4009 46.8263
2021-07-10 10:00 DST LG1 39.3575 78.3179
2021-07-10 10:00 DST LG2 50.3955 43.3913
2021-07-10 10:00 DST LG3 52.2898 51.8793
2021-07-11 09:00 DST LG1 36.8559 71.9565
2021-07-11 09:00 DST LG2 31.1939 35.8108
2021-07-11 09:00 DST LG3 44.6744 49.5196
2021-07-11 10:00 DST LG1 43.9611 74.5974
2021-07-11 10:00 DST LG2 39.075 36.9884
2021-07-11 10:00 DST LG3 41.0939 45.0962

I want the x-axis to be Time, and then plot a line in the graph for Usage1 and Usage2 for each Type. So in total, since there are 3 different Types, there should be a total of 6 lines. So far I've tried the following code but it plots a single line for each Usage:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

df = pd.read_excel (r'plot.xlsx')

plt.plot( 'Time', 'Usage1 [%]', data=df, marker='o', linewidth=2, label='Type')
plt.plot( 'Time', 'Usage2 [%]', data=df, marker='o', linewidth=2, label='Type')
plt.legend(loc='best')

1 Answer 1

1

You will need to reshape your data to look like this using pandas.melt:

                    Time  Type    variable    value
0  2021-07-09 09:00 DST   LG1   Usage1 [%]  60.0581
1  2021-07-09 09:00 DST   LG2   Usage1 [%]  42.1409
2  2021-07-09 09:00 DST   LG3   Usage1 [%]  63.4330
3  2021-07-09 10:00 DST   LG1   Usage1 [%]  53.6577
4  2021-07-09 10:00 DST   LG2   Usage1 [%]  36.3840

Then you can use seaborn.lineplot:

import seaborn as sns
df['Time'] = pd.to_datetime(df['Time'])
sns.lineplot(data=df.melt(id_vars=['Time', 'Type']), x='Time', hue='Type', style='variable', y='value')

output:

seaborn line plot

NB. to ensure consistent time handling, it is better to use the datetime format for your time column.

Sign up to request clarification or add additional context in comments.

4 Comments

is there anyway of doing it with matplotlib?
seaborn is using matplotlib, so you end up with a Axes object here.
For sure you can also do it manually by looping over the Type and Usage, but what would be the reason for not using seaborn? If there is a specific reason, please update your question
I don't have seaborn installed and I currently don't have permission to do it

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.