Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

328
Views
x axis gets transformed to floats

I am trying to plot my data grouped by year, and for each year, i want to count the number of users. Below, i just transformed the date column from float to integer.

This is my plotenter image description here

If you see the x-axis, my year ticker seems to have become a float and the each ticker is 0.5 tick apart.

How do i make this purely an integer?


Changing the groupby has the same result: enter image description here


ticks are still 2 spaces apart after converting the year column to a string format

df['year'] = df['year'].astype(str)

: enter image description here

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

The expectation that using integer data will lead a matplotlib axis to only show integers is not justified. At the end, each axis is a numeric float axis.

The ticks and labels are determined by locators and formatters. And matplotlib does not know that you want to plot only integers.

Some possible solutions:

Tell the default locator to use integers

The default locator is a AutoLocator, which accepts an attribute integer. So you may set this attribute to True:

ax.locator_params(integer=True)

Example:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

data = pd.DataFrame({"year" : [2010,2011,2012,2013,2014],
                     "count" :[1000,2200,3890,5600,8000] })

ax = data.plot(x="year",y="count")
ax.locator_params(integer=True)

plt.show()

Using a fixed locator

You may just tick only the years present in the dataframe by using ax.set_ticks().

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

data = pd.DataFrame({"year" : [2010,2011,2012,2013,2014],
                     "count" :[1000,2200,3890,5600,8000] })

data.plot(x="year",y="count")
plt.gca().set_xticks(data["year"].unique())
plt.show()

Convert year to date

You may convert the year column to a date. For dates much nicer ticklabeling takes place automatically.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

data = pd.DataFrame({"year" : [2010,2011,2012,2013,2014],
                     "count" :[1000,2200,3890,5600,8000] })

data["year"] = pd.to_datetime(data["year"].astype(str), format="%Y")
ax = data.plot(x="year",y="count")

plt.show()

In all cases you would get something like this:

enter image description here

over 4 years ago · Santiago Trujillo Report

0

import matplotlib.pyplot as plt

# Use min and max to get the range of years to use in axis ticks
year_min = df['year'].min()
year_max = df['year'].max()

df['year'] = df['year'].astype(str) # Prevents conversion to float

plt.xticks(range(year_min, year_max, 1)) # Sets plot ticks to years within range

Hope this helps!

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!