When I first started using Matplotlib in Python, one of the challenges I faced was keeping my chart labels readable, especially when plotting large datasets. The tick labels on the x-axis or y-axis would often overlap, making the chart look messy and hard to interpret.
If you’ve ever created a bar chart or a line plot with long category names, you’ve probably run into this issue too. The good news is that Matplotlib provides simple and effective ways to rotate tick labels to make your plots look cleaner and more professional.
In this tutorial, I’ll show you how to rotate tick labels by 45 and 90 degrees in Matplotlib using different methods. I’ll also share a few practical tips from my experience as a Python developer with over a decade of data visualization work.
Why Rotate Tick Labels in Matplotlib?
Before we jump into the how, let’s understand why rotating tick labels is useful. When your tick labels (the text along the axes) are long or numerous, they can overlap. This makes your chart difficult to read and interpret.
By rotating tick labels, especially at 45° or 90°, you can:
- Improve readability.
- Save space on the chart.
- Make your visualizations look more polished and professional.
I often use this technique when visualizing data such as U.S. state names, company names, or months, where the labels are longer than usual.
Method 1 – Rotate Tick Labels Using plt.xticks() in Python
The easiest and most common way to rotate tick labels in Matplotlib is by using the plt.xticks() function. This method works great for simple plots and is perfect if you’re just getting started.
Here’s a step-by-step example.
Example: Rotate X-Axis Labels by 45 Degrees
Below is a Python example where I’ll plot the average temperatures of five major U.S. cities.
import matplotlib.pyplot as plt
# Data for average temperatures (in °F)
cities = ["New York", "Los Angeles", "Chicago", "Houston", "Phoenix"]
temperatures = [55.3, 65.4, 52.1, 70.1, 75.2]
# Create a simple bar chart
plt.bar(cities, temperatures, color='skyblue')
# Add title and labels
plt.title("Average Annual Temperature in Major U.S. Cities", fontsize=14)
plt.xlabel("City", fontsize=12)
plt.ylabel("Temperature (°F)", fontsize=12)
# Rotate x-axis tick labels by 45 degrees
plt.xticks(rotation=45)
# Display the plot
plt.tight_layout()
plt.show()You can refer to the screenshot below to see the output.

In the code above, the line plt.xticks(rotation=45) rotates all x-axis tick labels by 45 degrees. This small change makes the labels more readable, especially when dealing with longer names like “Los Angeles” or “New York.”
Example: Rotate X-Axis Labels by 90 Degrees
If you want the labels to be completely vertical, you can rotate them by 90 degrees using the same function.
plt.xticks(rotation=90)This is particularly useful when you have many categories or very long names. It ensures that every label is visible without overlapping.
Method 2 – Rotate Tick Labels Using ax.tick_params() in Python
When working with object-oriented Matplotlib, you’ll often use the Axes object (ax) instead of calling functions directly from plt. In such cases, you can rotate the tick labels using ax.tick_params().
Here’s how I do it in my projects.
Example: Rotate X-Axis Labels by 45 Degrees Using tick_params()
import matplotlib.pyplot as plt
# Create sample data
months = ["January", "February", "March", "April", "May", "June"]
sales = [15000, 18000, 22000, 21000, 25000, 27000]
# Create a figure and axis
fig, ax = plt.subplots()
# Plot the data
ax.plot(months, sales, marker='o', color='green')
# Set labels and title
ax.set_title("Monthly Sales Data (USA)", fontsize=14)
ax.set_xlabel("Month", fontsize=12)
ax.set_ylabel("Sales ($)", fontsize=12)
# Rotate tick labels by 45 degrees
ax.tick_params(axis='x', rotation=45)
# Display the plot
plt.tight_layout()
plt.show()You can refer to the screenshot below to see the output.

In this example, I used ax.tick_params(axis=’x’, rotation=45) to rotate the x-axis labels. This method is more flexible because it allows you to control both the x and y axis labels independently.
If you’re building multiple subplots or customizing your chart heavily, this approach is more professional and scalable.
Method 3 – Rotate Tick Labels Using set_xticklabels() in Python
Another way to rotate tick labels in Matplotlib is through the set_xticklabels() method. This method gives you direct control over the tick labels, allowing you to set their rotation, color, font size, and alignment.
Here’s an example.
Example: Rotate X-Axis Labels by 90 Degrees Using set_xticklabels()
import matplotlib.pyplot as plt
# Data for a population chart
states = ["California", "Texas", "Florida", "New York", "Pennsylvania"]
population = [39.2, 30.3, 22.2, 19.8, 13.0] # in millions
fig, ax = plt.subplots()
ax.bar(states, population, color='orange')
# Add labels and title
ax.set_title("Top 5 Most Populous U.S. States", fontsize=14)
ax.set_xlabel("State", fontsize=12)
ax.set_ylabel("Population (Millions)", fontsize=12)
# Rotate labels by 90 degrees
ax.set_xticklabels(states, rotation=90)
plt.tight_layout()
plt.show()You can refer to the screenshot below to see the output.

Here, I used ax.set_xticklabels(states, rotation=90) to rotate the labels vertically. This makes the bar chart easy to read, even with long state names like “California” and “Pennsylvania.”
Note: When using set_xticklabels(), it’s best to ensure that your tick positions align with your data points. You can do this by setting both ax.set_xticks() and ax.set_xticklabels() together if needed.
Method 4 – Rotate Both X and Y Tick Labels in Python
Sometimes, you might need to rotate both the x-axis and the y-axis labels for better readability. This is common in heatmaps or correlation matrices.
Here’s a quick example.
import matplotlib.pyplot as plt
import numpy as np
# Create a random correlation matrix
data = np.random.rand(5, 5)
fig, ax = plt.subplots()
heatmap = ax.imshow(data, cmap='coolwarm')
# Add labels
labels = ["A", "B", "C", "D", "E"]
ax.set_xticks(np.arange(len(labels)))
ax.set_yticks(np.arange(len(labels)))
ax.set_xticklabels(labels)
ax.set_yticklabels(labels)
# Rotate both x and y tick labels
plt.setp(ax.get_xticklabels(), rotation=45, ha="right", rotation_mode="anchor")
plt.setp(ax.get_yticklabels(), rotation=45, ha="right", rotation_mode="anchor")
# Add title
ax.set_title("Heatmap Example with Rotated Tick Labels", fontsize=14)
plt.tight_layout()
plt.show()You can refer to the screenshot below to see the output.

In this example, I used plt.setp() to rotate both x and y tick labels by 45 degrees. The ha=”right” and rotation_mode=”anchor” parameters help align the text neatly. This approach is perfect for visualizations that involve grids or matrices.
Bonus Tip – Adjust Alignment for Better Readability
When you rotate tick labels, it’s often helpful to adjust their alignment. You can use the ha (horizontal alignment) and va (vertical alignment) parameters to fine-tune how the labels appear.
For example:
plt.xticks(rotation=45, ha='right')This ensures the labels don’t overlap with the ticks and look visually balanced.
Common Mistakes to Avoid
After years of working with Matplotlib, I’ve noticed a few common mistakes developers make when rotating tick labels:
- Forgetting plt.tight_layout() – Without it, labels might get cut off.
- Using rotation without alignment – Always use
haorvafor neatness. - Not updating tick positions – When using set_xticklabels(), ensure tick positions match your data.
- Over-rotating labels – Sometimes, 30° or 60° is more readable than 90°.
Keeping these small details in mind can make a big difference in your final visualization.
Conclusion
Rotating tick labels in Matplotlib with Python is a small change that can dramatically improve your chart’s readability and presentation. Whether you’re creating business dashboards, academic reports, or data visualizations for the U.S. market, properly aligned labels make your insights stand out.
From my experience, I recommend starting with plt.xticks(rotation=45) for quick fixes and moving to the ax.tick_params() method for more control in complex projects.
You may also like to read:
- Change Inner and Outer Background Colors in Matplotlib
- Transparent Plot Backgrounds & Legend Styling in Matplotlib
- Change Background Color of Matplotlib Subplot Based on Value
- Rotate Tick Labels on X and Y Axes in Python Matplotlib

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.