简体   繁体   English

创建从数据帧行中提取的多条线图

[英]Create plot of multiple lines taken from rows of dataframe

I would like to create a graph that plots multiple lines onto one graph.我想创建一个图表,将多条线绘制到一个图表上。

Here is an example dataframe (my actual dataframe is much larger):这是一个示例数据框(我的实际数据框要大得多):

df = pd.DataFrame({'first': [1, 2, 3], 'second': [1, 1, 5], 'Third' : [8,7,9], 'Person' : ['Ally', 'Bob', 'Jim']})

The lines I want plotted are rowwise ie a line for Ally, a line for Jim and a line for Bob我想要绘制的线条是按行排列的,即 Ally 的一条线,Jim 的一条线和 Bob 的一条线

You can use the built-in plotting functions, as soon as the DataFrame has the right shape.只要 DataFrame 具有正确的形状,您就可以使用内置的绘图函数。 The right shape in this case would be Person names as columns and the former columns as index.在这种情况下,正确的形状是人名作为列,前列作为索引。 So all you have to do is to set Person as the index and transpose:所以你所要做的就是将Person设置为索引并转置:

df.set_index("Person").T.plot()

在此处输入图片说明

First you should set your name as the index then retrieve the values for each index:首先,您应该将您的名称设置为索引,然后检索每个索引的值:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({'first': [1, 2, 3], 'second': [1, 1, 5], 'Third' : [8,7,9], 'Person' : ['Ally', 'Bob', 'Jim']})
df = df.set_index('Person')

for person in df.index:
    val = df.loc[person].values
    plt.plot(val, label = person)
plt.legend()
plt.show()

在此处输入图片说明

As for how you want to handle the first second third I let you judge by yourself至于你想如何处理第一第二第三我让你自己判断

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

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