从 Pandas 数据框中绘制折线图(多条线)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50642098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:37:51  来源:igfitidea点击:

Plot line graph from Pandas dataframe (with multiple lines)

pythonpandasdataframegraphline

提问by Ryan

I'm currently working on the below dataframe.

我目前正在处理以下数据框。

Dataframe

数据框

To summarize the contents, there is an "age" column, which relates to an age group (i.e. 16-25, 26-32) - and then 8 class % values, these are percentage values which tell me what percentage of a certain age group are in that specific social class. So in this example, there are 10.81%(rounded) of the people in age group with the ID of 1 that are also in the social class with an ID of 1. For the same age group, there are 22.34% in the social class with an ID of 2, and so on, so forth. Each row totals to 100%.

总结一下内容,有一个“年龄”列,它与年龄组(即 16-25、26-32)有关 - 然后是 8 个班级百分比值,这些百分比值告诉我某个年龄的百分比群体属于那个特定的社会阶层。所以在这个例子中,有10.81%(四舍五入)年龄组中ID为1的人也属于ID为1的社会阶层。对于同一个年龄组,社会阶层中有22.34% ID 为 2,依此类推。每行总计为 100%。

I am looking to create a line graph, which has one line representing each age group. So this graph should have a total of 5 lines.

我希望创建一个折线图,其中有一条线代表每个年龄段。所以这张图总共应该有5条线。

The X-Axis should represent the Social classes (so ranging 1 through 8), and the Y-Axis should represent the percentage of people in that class.

X 轴应代表社会类别(范围为 1 到 8),Y 轴应代表该类别的人数百分比。

I'm looking for the graph in this format to make it clear to see for each distinct age group, the patterns in how many people are in each social class, and how this changes as you get older.

我正在寻找这种格式的图表,以便清楚地了解每个不同的年龄组、每个社会阶层中有多少人的模式,以及随着年龄的增长而变化的情况。

Any help with this would be appreciated, I'm not even sure where to start? I've tried some examples online but nothing seems to work. Even a starter would be great.

对此的任何帮助将不胜感激,我什至不确定从哪里开始?我在网上尝试了一些示例,但似乎没有任何效果。即使是首发也会很棒。

Thanks.

谢谢。

采纳答案by user59271

You can do it in two lines. Firstly you could simply transpose your dataset so that it's in a shape that you want to plot it:

你可以用两行来完成。首先,您可以简单地转置您的数据集,使其处于您想要绘制的形状中:

df_plot = df.set_index('age').T

this produces(numbers are randomly generated and differ from the ones you've provided):

这会产生(数字是随机生成的,与您提供的数字不同):

age          1       2       3       4       5
class1  0.5377  0.2147  0.4837  0.8682  0.3429
class2  0.8350  0.0544  0.4314  0.6592  0.6475
class3  0.9382  0.0283  0.7152  0.0962  0.3012
class4  0.7277  0.1523  0.3124  0.0077  0.4039
class5  0.7580  0.4149  0.1352  0.5068  0.2955
class6  0.3243  0.3346  0.2820  0.8481  0.9782
class7  0.2298  0.0522  0.7307  0.9851  0.8681
class8  0.3283  0.0562  0.9052  0.6320  0.6140

Then produce a plot by calling the inbuilt plot function:

然后通过调用内置的绘图函数生成一个绘图:

df_plot.plot(figsize=(10,6), xticks=range(0, 8)).legend(title='age', bbox_to_anchor=(1, 1))

this results in: enter image description here

这导致: 在此处输入图片说明

回答by Peter Leimbigler

You might be interested in a stacked area plot. This should work on your DataFrame, named df:

您可能对堆积面积图感兴趣。这应该适用于您的 DataFrame,名为df

df.drop(columns='age').plot(kind='area', stacked=True)

One issue is that legend items will show up in reverse order compared to the vertical ordering of the plot areas. To fix this, you can manually reverse the legend handles and labels:

一个问题是,与绘图区域的垂直顺序相比,图例项将以相反的顺序显示。要解决此问题,您可以手动反转图例手柄和标签:

ax = plt.gca()
leg_handles, leg_labels = ax.get_legend_handles_labels()
ax.legend(leg_handles[::-1], leg_labels[::-1])

Here's some example data (post text, not images, so we can easily copy-paste and experiment :)):

这是一些示例数据(发布文本,而不是图像,因此我们可以轻松复制粘贴和实验:)):

df = pd.DataFrame({'age': [1, 2, 3], 
                   'Class1': [22, 14, 26], 
                   'Class2': [14, 15, 14], 
                   'Class3': [64, 71, 60]
                  })

Output: Stackplot demo

输出: 堆栈图演示

To reverse the vertical order in the plot so that Class 1 ends up at the top, sort the columns (axis=1) in descending order before plotting:

要反转图中的垂直顺序,使 Class 1 在顶部结束,请在axis=1绘图前按降序对列 ( ) 进行排序:

df.drop(columns='age').sort_index(axis=1, ascending=False)plot(kind='area', stacked=True)

回答by Dav2357

A possible solution to create the line graph as you requested could be (using a dummy dataset):

根据您的要求创建折线图的可能解决方案是(使用虚拟数据集):

import matplotlib.pyplot as plt
import pandas as pd

df=pd.DataFrame({"age":[1,2,3,4,5],"class1":[0.1,0.2,0.3,0.3,0.6],"class2":[0.4,0.1,0.2,0.3,0.6],"class3":[0.1,0.7,0.8,0.3,0.5]})
df=df.set_index("age")
for i in range(len(df)):
    plt.plot([k for k in df.columns],[df[y].iloc[i] for y in df.columns])
plt.legend(df.index,loc="upper left")
plt.show()

Output: enter image description hereProbably not the most pythonic way though.

输出: 在此处输入图片说明虽然可能不是最 Pythonic 的方式。