-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,...)?

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
  • 468
  • 3
  • 12