- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to force Matplotlib to show the values on X-axis as integers?
To force matplotlib to show the values on X-axis as integers, we can take the following steps −
- Set the figure size and adjust the padding between and around the subplots.
- Create two lists, x and y, of data points.
- Plot x and y using plot() method.
- Make a new list for only integers tick on X-axis. Use math.floor() and math.ceil() to remove the decimals and include only integers in the list.
- Set x and y labels.
- Set the title of the figure.
- To display the figure, use show() method.
Example
import math from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True y = [0.17, 1.17, 2.98, 3.15, 4.11, 5.151] x = [1.0, 1.75, 2.90, 3.15, 4.50, 5.50] plt.plot(x, y) new_list = range(math.floor(min(x)), math.ceil(max(x))+1) plt.xticks(new_list) plt.xlabel("X-axis ") plt.ylabel("Y-axis ") plt.title("Only Integers on the X-axis") plt.show()
Output
- Related Articles
- How to force the Y axis to use only integers in Matplotlib?
- How to show date and time on the X-axis in Matplotlib?
- How to set X-axis values in Matplotlib Python?
- How to show all the X-axis tick values in Python Plotly?
- How to specify values on Y-axis in Python Matplotlib?
- Show the origin axis (x,y) in Matplotlib plot
- Updating the X-axis values using Matplotlib animation
- How to customize the X-axis in Matplotlib?
- How to plot data against specific dates on the X-axis using Matplotlib?
- Change values on matplotlib imshow() graph axis
- How to customize X-axis ticks in Matplotlib?
- Matplotlib – How to plot the FFT of signal with correct frequencies on the X-axis?
- How to add footnote under the X-axis using Matplotlib?
- How to change the range of the X-axis and Y-axis in Matplotlib?
- How to make Matplotlib show all X coordinates?

Advertisements