- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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 make xticks evenly spaced despite their values? (Matplotlib)
To make xticks evenly spaced despite their values, we can use set_ticks() and set_ticklabels() methods.
Steps
- Set the figure size and adjust the padding between and around the subplots.
- Create x and y data points using numpy.
- Create a figure and a set of subplots using subplots() method.
- Plot x and y data points on axis 1.
- Set xticks using xaxis.set_ticks() method.
- Plot x and y data points on axis 2.
- Set xticks and ticklabels using xaxis.set_ticks() and xaxis.set_ticklabels() method.
- To display the figure, use show() method.
Example
import numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.array([1, 1.5, 3, 5, 6]) y = np.power(2, x) fig, (ax1, ax2) = plt.subplots(2, 1) ax1.plot(x, y) ax1.xaxis.set_ticks(x) ax2.plot(x, y) ax2.xaxis.set_ticks(range(len(x))) ax2.xaxis.set_ticklabels(x) plt.show()
Output
- Related Articles
- Return evenly spaced values within a given interval in Numpy
- Return evenly spaced values within a given interval and step size in Numpy
- Matplotlib – How to set xticks and yticks with imshow plot?
- How to change xticks font size in a matplotlib plot?
- Create an evenly spaced color stops with CSS Radial Gradients
- Return evenly spaced numbers over a log scale in Numpy
- Return numbers spaced evenly on a geometric progression in Numpy
- Return evenly spaced numbers over a specified interval in Numpy
- How to plot a Pandas multi-index dataFrame with all xticks (Matplotlib)?
- Program to find out the sum of evenly spaced elements in an array in Python
- Return evenly spaced numbers on a log scale and set the base in Numpy
- Return numbers spaced evenly on a geometric progression but with negative inputs in Numpy
- Return numbers spaced evenly on a geometric progression but with complex inputs in Numpy
- Return evenly spaced numbers on a log scale and set the number of samples to generate in Numpy
- Return evenly spaced numbers on a geometric progression and set the number of samples to generate in Numpy

Advertisements