- 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 rotate tick labels in a subplot in Matplotlib?
To rotate tick labels in a subplot, we can use set_xticklabels() or set_yticklabels() with rotation argument in the method.
Create a list of numbers (x) that can be used to tick the axes.
Get the axis using subplot() that helps to add a subplot to the current figure.
Set ticks on the X and Y axes using set_xticks and set_yticks methods, respectively, and the list x (from step 1).
Set tick labels with label lists (["one", "two", "three", "four"]) and rotation=45 using set_xticklabels() and set_yticklabels().
To add space between axes and tick labels, we can use tick_params() method with pad argument that helps to add space. The argument direction(in) helps to put the ticks inside the axes. And, apply axis(both) parameters on both the axes.
To show the figure, use plt.show() method.
Example
import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = [1, 2, 3, 4] ax1 = plt.subplot() ax1.set_xticks(x) ax1.set_yticks(x) ax1.set_xticklabels(["one", "two", "three", "four"], rotation=45) ax1.set_yticklabels(["one", "two", "three", "four"], rotation=45) ax1.tick_params(axis="both", direction="in", pad=15) plt.show()
Output
- Related Articles
- How to make longer subplot tick marks in Matplotlib?
- How to rotate X-axis tick labels in Pandas bar plot?
- Hiding major tick labels while showing minor tick labels in Matplotlib
- Rotate tick labels for Seaborn barplot in Matplotib
- Centering x-tick labels between tick marks in Matplotlib
- How to change the separation between tick labels and axis labels in Matplotlib?
- How to decrease the density of tick labels in subplots in Matplotlib?
- How to show tick labels on top of a matplotlib plot?
- Rotate xtick labels in Seaborn boxplot using Matplotlib
- Getting empty tick labels before showing a plot in Matplotlib
- How to show minor tick labels on a log-scale with Matplotlib?
- Change grid interval and specify tick labels in Matplotlib
- Show tick labels when sharing an axis in Matplotlib
- How can I rotate xtick labels through 90 degrees in Matplotlib?
- Hide axis values but keep axis tick labels in matplotlib
