简体   繁体   English

从数据框中绘制多条线

[英]Plot multiple lines from dataframe

I have the following table:我有下表:

Time时间 Type类型 Usage1 [%]用量1 [%] Usage2 [%]用量2 [%]
2021-07-09 09:00 DST 2021-07-09 09:00 夏令时 LG1 LG1 60.0581 60.0581 87.4926 87.4926
2021-07-09 09:00 DST 2021-07-09 09:00 夏令时 LG2 LG2 42.1409 42.1409 40.57 40.57
2021-07-09 09:00 DST 2021-07-09 09:00 夏令时 LG3 LG3 63.433 63.433 49.9326 49.9326
2021-07-09 10:00 DST 2021-07-09 10:00 夏令时 LG1 LG1 53.6577 53.6577 86.6658 86.6658
2021-07-09 10:00 DST 2021-07-09 10:00 夏令时 LG2 LG2 36.384 36.384 41.7439 41.7439
2021-07-09 10:00 DST 2021-07-09 10:00 夏令时 LG3 LG3 54.5699 54.5699 54.0306 54.0306
2021-07-10 09:00 DST 2021-07-10 09:00 夏令时 LG1 LG1 35.2818 35.2818 75.8487 75.8487
2021-07-10 09:00 DST 2021-07-10 09:00 夏令时 LG2 LG2 34.101 34.101 37.7934 37.7934
2021-07-10 09:00 DST 2021-07-10 09:00 夏令时 LG3 LG3 50.4009 50.4009 46.8263 46.8263
2021-07-10 10:00 DST 2021-07-10 10:00 夏令时 LG1 LG1 39.3575 39.3575 78.3179 78.3179
2021-07-10 10:00 DST 2021-07-10 10:00 夏令时 LG2 LG2 50.3955 50.3955 43.3913 43.3913
2021-07-10 10:00 DST 2021-07-10 10:00 夏令时 LG3 LG3 52.2898 52.2898 51.8793 51.8793
2021-07-11 09:00 DST 2021-07-11 09:00 夏令时 LG1 LG1 36.8559 36.8559 71.9565 71.9565
2021-07-11 09:00 DST 2021-07-11 09:00 夏令时 LG2 LG2 31.1939 31.1939 35.8108 35.8108
2021-07-11 09:00 DST 2021-07-11 09:00 夏令时 LG3 LG3 44.6744 44.6744 49.5196 49.5196
2021-07-11 10:00 DST 2021-07-11 10:00 夏令时 LG1 LG1 43.9611 43.9611 74.5974 74.5974
2021-07-11 10:00 DST 2021-07-11 10:00 夏令时 LG2 LG2 39.075 39.075 36.9884 36.9884
2021-07-11 10:00 DST 2021-07-11 10:00 夏令时 LG3 LG3 41.0939 41.0939 45.0962 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.我希望 x 轴是时间,然后在图中为每个类型的 Usage1 和 Usage2 绘制一条线。 So in total, since there are 3 different Types, there should be a total of 6 lines.所以总的来说,因为有3种不同的类型,所以总共应该有6行。 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')

You will need to reshape your data to look like this using pandas.melt :您需要使用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 :然后你可以使用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 线图

NB.注意。 to ensure consistent time handling, it is better to use the datetime format for your time column.为确保一致的时间处理,最好为您的时间列使用日期时间格式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从数据框中绘制多条线 - How to plot multiple lines from a dataframe Plot 线图来自 Pandas dataframe(多线) - Plot line graph from Pandas dataframe (with multiple lines) 创建从数据帧行中提取的多条线图 - Create plot of multiple lines taken from rows of dataframe 从熊猫数据框中绘制线 - Plot lines from pandas dataframe 从一个数据框中绘制多条线并添加辅助轴以绘制不同的数据框 - Pandas - Plotting multiple lines from one dataframe and adding a secondary axis to plot a different dataframe - Pandas 如何 plot 多行在一个 plotly 图表中来自同一列的同一列 pandas Z6A8064B5DF47945550705553? - How to plot multiple lines in one plotly chart from same column from the same pandas dataframe? Plotly:如何在来自同一 Pandas 数据帧的不同列的一个绘图图表中绘制多条线? - Plotly: How to plot multiple lines in one plotly chart from different columns from the same pandas dataframe? Python plot dataframe 有多条线和两个不同的 styles - Python plot dataframe with multiple lines and two different styles 我如何 plot 多行使用 dataframe 上的值计数 - how do i plot multiple lines using value counts on a dataframe 在 plot 中创建多行,按较大 dataframe 的选定列分组 - Creating multiple lines in plot grouped by selected column of larger dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM