在绘制数据框中的数据时需要帮助

时间:2020-11-04 10:47:07

标签: python pandas dataframe matplotlib

我有以下数据框;

    Region Name Year    Internet usage %
0   Northern Africa 2000    0.6
1   Northern Africa 2005    9.6
2   Northern Africa 2010    24.6
3   Northern Africa 2014    35.3
4   Northern Africa 2015    38.9
5   Northern Africa 2016    41.7
6   Northern Africa 2017    45.5
7   Sub-Saharan Africa  2000    0.5
8   Sub-Saharan Africa  2005    2.1
9   Sub-Saharan Africa  2010    6.5
10  Sub-Saharan Africa  2014    14.3
11  Sub-Saharan Africa  2015    17.7
12  Sub-Saharan Africa  2016    19.5
13  Sub-Saharan Africa  2017    21.8
14  Eastern Africa  2000    0.2
15  Eastern Africa  2005    1.3
16  Eastern Africa  2010    4.5
17  Eastern Africa  2014    9.8
18  Eastern Africa  2015    13.3
19  Eastern Africa  2016    15.0
20  Eastern Africa  2017    17.6
21  Middle Africa   2000    0.1
22  Middle Africa   2005    0.7
23  Middle Africa   2010    2.1
24  Middle Africa   2014    7.0
25  Middle Africa   2015    8.7
26  Middle Africa   2016    10.5
27  Middle Africa   2017    12.2
28  Southern Africa 2000    4.9
29  Southern Africa 2005    7.0
30  Southern Africa 2010    22.0
31  Southern Africa 2014    45.9
32  Southern Africa 2015    48.9
33  Southern Africa 2016    51.2
34  Southern Africa 2017    53.4
35  Western Africa  2000    0.1
36  Western Africa  2005    2.5
37  Western Africa  2010    7.7
38  Western Africa  2014    16.7
39  Western Africa  2015    21.1
40  Western Africa  2016    22.9
41  Western Africa  2017    25.2
​

我想绘制此数据,其中X轴为“年”,Y轴为“互联网使用百分比”,并且不同区域以不同的颜色显示,这将是线图。

当我尝试绘图时,我弄错了绘图。有人可以帮我吗?

预先感谢

3 个答案:

答案 0 :(得分:0)

使用seaborn

sns.lineplot(data=df, x='Year',y='Internet usage %', hue='Region Name')

enter image description here

答案 1 :(得分:0)

这是我到目前为止开发的代码;

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

df = pd.read_csv('Internet Usage over the years.csv', engine='python')

# Remove the first row as it is not required
df = df.iloc[1:]

# Rename the columns
df.columns = ['Region Code', 'Region Name', 'Year', 'Misc 1', 'Internet usage %', 'Misc 2', 'Misc 3']

df = df[(df['Region Name'] == 'Northern Africa') | (df['Region Name'] == 'Sub-Saharan Africa') | (df['Region Name'] == 'Eastern Africa') | (df['Region Name'] == 'Middle Africa') | (df['Region Name'] == 'Southern Africa') | (df['Region Name'] == 'Western Africa')]
df = df.reset_index()

# Remove unwanted columns from the dataframe
cols = [0, 1, 4, 6, 7]
df.drop(df.columns[cols], inplace=True, axis=1)

convert_dict = {'Year': int, 
                'Internet usage %': float
               } 

df = df.astype(convert_dict)

df.plot(x ='Year', y='Internet usage %', kind = 'line') 

plt.xlabel('Year')
plt.ylabel('% of individuals using the internet')

plt.legend(loc='upper left', fontsize=9, frameon=True, framealpha=1)

plt.title('Internet usage % in different regions of Afria over the years')
plt.show()

我已经附上了结果图enter image description here

答案 2 :(得分:0)

因此,以下三种解决方案可能适合您。

  1. 使用熊猫支点
df_plot = df.pivot(index='Year', columns='Region', values='Internet usage %')
df_plot.plot()
  1. 使用熊猫分组方式
fig, ax = plt.subplots()

for region, grp in df.groupby(['Region']):
    ax = grp.plot(ax=ax, kind='line', x='Year', y='Internet usage %', c=region, label=region)

plt.show()
  1. 使用seaborn

请参阅@Diziet Asahi的答案。

我个人更喜欢第一个解决方案,但这当然取决于您。