如何正确使用for循环从python中的csv绘制多行?

Nig*_*ner 5 python plot for-loop matplotlib

我正在学习 Python(基本问题即将到来),我有很多来自一些分析的数据,作为 CSV 文件,如下所示:

示例数据表

我正在尝试重新创建下面的图(目前我在 Excel 中做这样的事情,随着数据量的增加和可视化的复杂性,我想让事情变得更有效率。

我正在尝试重新创建的情节

我尝试使用 For 循环的概念为该列中的每个不同的“SectionName”绘制一条线,但显然我遗漏了一些东西。

#read CSV
df=pd.read_csv('/Users/F/Documents/Data/LinesLoopTest.csv')

#Make Variables from dataframe columns
Value = df['Value']
Position = df['Xposition']
Section = df['SectionName']

#Setup Figure
fig = plt.figure(figsize=(6,3))
ax1 = fig.add_subplot(1,1,1)
ax1.set_title('Sections')
ax1.set_xlabel("XPosition")
ax1.set_ylabel("Value")

#plot lines by SectionName
for name in ['A', 'B', 'C']:
    plt.plot(Position, Value)

plt.show()
Run Code Online (Sandbox Code Playgroud)

我意识到这是一个简单的问题,到目前为止我还没有找到一个解释,我可以真正理解这个过程,以至于我可以重新创建它,然后在它的基础上进行构建。

She*_*ore 3

您可以 在 上使用groupbySectionName然后绘制组

fig, ax = plt.subplots()

for section, group in df.groupby('SectionName'):
    group.plot(x='Xposition', y='Value', ax=ax, label=section)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述