首页 > 解决方案 > Python Seaborn 线图

问题描述

我是 Python 新手,对线图有疑问。

我有一个数据集,我想将其显示为 Seaborn 线图。在这个数据集中,我有 3 个类别,它们应该在 Y 轴上。我没有 X 轴的数据,但我想使用索引。

不幸的是,我没有做对。我想像 Excel 图片一样使用它。

列的长度也不同。

在此处输入图像描述

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

df = pd.read_csv("Testdata.csv", delimiter= ";")
df
     Double       Single      Triple
0   50.579652   24.498143   60.954680
1   53.313919   24.497490   60.494626
2   54.174343   24.490651   60.052566
3   56.622435   24.485605   59.622501
4   59.656155   26.201791   59.199581
...     ...         ...        ...
410    NaN         NaN     75.478118
411    NaN         NaN     73.780804
412    NaN         NaN     72.716096
413    NaN         NaN     72.468472
414    NaN         NaN     71.179819

我怎么做?

我感谢您的帮助。

标签: pythonpandasseaborn

解决方案


首先融化你的列,然后使用色调参数绘制每一行:

fig, ax = pyplot.subplots(figsize=(10, 10))
ax =seaborn.lineplot(
    data= df.melt(id_vars='index').rename(columns=str.title),
    x= 'index',
    y= 'value',
    hue='varaible'
)

推荐阅读