I have the following dataframe;
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
I want to plot this data, where X axis has the 'Year', Y axis 'Internet usage %' and the different regions are displayed in different colors and this will be a line plot.
When I tried plotting it I got the plot wrong. Can someone help me with this.
Thanks in advance
This is the code I have developed so far;
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()
I have attached the resulting plot enter image description here
So here are three solutions that might work for you.
df_plot = df.pivot(index='Year', columns='Region', values='Internet usage %')
df_plot.plot()
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()
Please see @Diziet Asahi's answer.
Personally, I prefer the first solution, but this is of course up to you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.