-2

I have already checked stackoverflow for any similar question but there is no answer to this.

Recently I started learning data visualization in python. This is the data frame I worked upon: Immigration to Canada (virtual data)

This is the code:

import matplotlib as mpl
import matplotlib.pyplot as plt
import pandas as pd

df = pd.read_excel('icdf.xlsx')
df.plot(kind="line")
plt.title('Immigration to Canada (virtual data)')
plt.xlabel('Years')
plt.ylabel('Number of immigrants')
plt.show()

Now, on running the above code I get this plot: Line Plot for the data frame

Why does the x-axis has 9 values viz. (1980.0, 1980.5, 1981.0, 1981.5, 1982.0,...) whereas according to the data frame it is expected to have 5 values viz. (1980, 1981, 1982,...)?

  • The axis values do not represent your actual data so but the looks of things you have plotted all 5 data points per line graph. you can set your ticks using `plt.xticks` – Jetman Dec 13 '18 at 09:08
  • Yes, got it from the answers too. Thanks @Jetman – codeXashutosh Dec 13 '18 at 12:22

1 Answers1

0

There are many solutions here. The problem seems to be just with the tick marks not the way the data is uploaded. Here are some of the best answers from that post:

import matplotlib.ticker as ticker

tick_spacing = 1
ax.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing))

and

plt.xticks(range(1980, 1985, 1)) #you can use min() and max()+1 to grab those from the dataframe and choose the step size according to your requirement
rLevv
  • 498
  • 3
  • 12
  • plt.xticks() did the trick but the code above that, was something I haven't tried yet. Will check the docs on that. Thanks. – codeXashutosh Dec 13 '18 at 12:27