1

I'm having a common issue but other advised solutions seem not to work including this

The labels[Years] on the X axis are not displaying when the kind used is Line. I'll appreciate someone runs the code and help figure it out. Thank you in advance.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
data = pd.read_csv('https://raw.githubusercontent.com/princeinzion/NigeriaGDPtoPopulation/master/API_NGA_DS2_en_csv_v2_10185307.csv', skiprows=4)

df = data.loc[[620, 1168], '1999':'2017']

df = df.T

dfp = df.pct_change()

dfp = dfp.reset_index()
dfp.columns = ['Years', 'Population', 'GDP']
dfp

fig = plt.figure(figsize=(20,10))

ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)

dfp.plot(kind='line',x='Years',y='GDP', color='red', ax=ax1)
dfp.plot(kind='line',x='Years',y='Population', color='blue', ax=ax2)

plt.show()

Below is the result I get. The Years do not display on the X axis. Line Chart

Zion Oyemade
  • 433
  • 6
  • 17

1 Answers1

3

Convert year to a datetime data type like this:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
data = pd.read_csv('https://raw.githubusercontent.com/princeinzion/NigeriaGDPtoPopulation/master/API_NGA_DS2_en_csv_v2_10185307.csv', skiprows=4)

df = data.loc[[620, 1168], '1999':'2017']

df = df.T

dfp = df.pct_change()

dfp = dfp.reset_index()
dfp.columns = ['Years', 'Population', 'GDP']
dfp['Years'] = pd.to_datetime(dfp['Years'])
dfp

fig = plt.figure(figsize=(20,10))

ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)

dfp.plot(kind='line',x='Years',y='GDP', color='red', ax=ax1)
dfp.plot(kind='line',x='Years',y='Population', color='blue', ax=ax2)

plt.show()

Output:

enter image description here

Scott Boston
  • 147,308
  • 15
  • 139
  • 187
  • Your advise worked not entirely. The chart is no longer showing the complete range of years. Any suggestions please – Zion Oyemade Nov 05 '18 at 15:43