简体   繁体   English

在一张图中绘制多个时间序列列

[英]Plot many time-series columns in one graph

I have a big data.frame with roughly 100 columns and try to plot all the time-series in one graph.我有一个大约有 100 列的大 data.frame,并尝试在一个图中绘制所有时间序列。 Is there an easy way to deal with it, without specifying every y-axis manually?有没有一种简单的方法来处理它,而无需手动指定每个 y 轴?

This would be a simple example with these time-series: 02K W, 03K W, and 04K W :这将是以下时间序列的简单示例: 02K W、03K W 和 04K W

import pandas as pd
import matplotlib.pyplot as plt

df1 = pd.DataFrame({
    'Date':['2021-01-01', '2021-01-02', '2021-01-03', '2021-01-04', '2021-01-05'],
    'index':[0, 1, 2, 3, 4], 
    '02K W':[3.5, 0.1, 3, 'nan', 0.2], 
    '03K W':[4.2, 5.2, 2.5, 3.0, 0.6], 
    '04K W':[1.5, 2.6, 8.2, 4.2, 5.3]}) 

df1['Date'] = pd.to_datetime(df1['Date'])
df1 = df1.set_index('index')

So far, I manually specify all y-axis to plot the individual time-series.到目前为止,我手动指定了所有 y 轴来绘制单个时间序列。

plt.plot(df1['Date'], df1['02K W'])
plt.plot(df1['Date'], df1['03K W'])
plt.plot(df1['Date'], df1['04K W'])

Is there a more elegant way to specify the relevant columns for the plot?有没有更优雅的方法来指定绘图的相关列? Thank you very much for your suggestions :)非常感谢您的建议:)

Is there a more elegant way to specify the relevant columns for the plot?有没有更优雅的方法来指定绘图的相关列?

Use DataFrame.plot with Date as the index and filter by the desired columns :使用DataFrame.plotDate作为索引并按所需columns过滤:

columns = ['02K W', '03K W', '04K W']
df1.set_index('Date')[columns].plot()

Note that you have a string 'nan' in your sample data.请注意,您的示例数据中有一个字符串'nan' If this is true in your real data, you should convert it to a real np.nan , eg, with pd.to_numeric or DataFrame.replace .如果这在您的真实数据中是正确的,您应该将其转换为真实的np.nan ,例如,使用pd.to_numericDataFrame.replace

import pandas as pd import matplotlib.pyplot as plt df1 = pd.DataFrame({ 'Date':['2021-01-01', '2021-01-02', '2021-01-03', '2021-01-04', '2021-01-05'], 'index':[0, 1, 2, 3, 4], '02K W':[3.5, 0.1, 3, 'nan', 0.2], '03K W':[4.2, 5.2, 2.5, 3.0, 0.6], '04K W':[1.5, 2.6, 8.2, 4.2, 5.3]}) df1['Date'] = pd.to_datetime(df1['Date']) df1 = df1.set_index('index') for col in df1.colums[1:]: plt.plot(df1['Date'], df1[col])

You can melt your columns and use seaborn.lineplot :你可以melt你的列并使用seaborn.lineplot

import seaborn as sns

sns.lineplot(data=df1.replace('nan', float('nan')).melt(id_vars=['Date']),
             x='Date', y='value', hue='variable'
            )

output:输出:

线图

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

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