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

1
  • 3
    Your year column is a string. You need to convert it to an int. Commented Nov 5, 2018 at 14:44

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Your advise worked not entirely. The chart is no longer showing the complete range of years. Any suggestions please

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.