Matplotlib xticks rotation

Matplotlib xticks rotation

Matplotlib is a powerful Data Visualization library in Python that provides various plotting functions to create insightful and appealing charts and graphs. One essential aspect of creating visualizations is controlling the appearance and customization options. In this article, we will explore the xticks rotation feature of Matplotlib, which allows us to control the rotation of the x-axis tick labels.

Understanding xticks rotation

Tick labels in Matplotlib are the values or markers that appear along the axes of a plot to represent specific data points. The x-axis represents the horizontal axis, and the y-axis represents the vertical axis.

Sometimes, when we have long tick labels on the x-axis, they might overlap each other or become difficult to read. The xticks rotation feature enables us to rotate the x-axis tick labels at a desired angle, making them easier to interpret.

Using xticks rotation in Matplotlib

To utilize the xticks rotation feature, we use the xticks() function from the matplotlib.pyplot module. This function has a rotation parameter that allows us to set the rotation angle of the x-axis tick labels.

The general syntax for modifying xticks rotation is as follows:

import matplotlib.pyplot as plt

plt.xticks(rotation=angle)

Here, angle represents the rotation angle in degrees. By specifying a positive value, we rotate the tick labels clockwise, and by using a negative value, we rotate them counterclockwise. Let’s now dive into some specific examples to understand the usage of xticks rotation in Matplotlib.

Example 1: Basic xticks rotation

import matplotlib.pyplot as plt

# Sample data
x = [1, 2, 3, 4, 5]
y = [10, 20, 15, 18, 25]

# Plotting
plt.plot(x, y)

# Setting xticks rotation
plt.xticks(rotation=45)

# Display the chart
plt.show()

Output:

Matplotlib xticks rotation

In this example, we rotate the x-axis tick labels by an angle of 45 degrees. The resulting plot shows the x-axis tick labels at a diagonal angle.

Example 2: No rotation

import matplotlib.pyplot as plt

# Sample data
x = [1, 2, 3, 4, 5]
y = [10, 20, 15, 18, 25]

# Plotting
plt.plot(x, y)

# Setting xticks rotation to 0 (no rotation)
plt.xticks(rotation=0)

# Display the chart
plt.show()

Output:

Matplotlib xticks rotation

The x-axis tick labels remain in the default horizontal orientation with no rotation applied.

Example 3: Custom rotation angle

import matplotlib.pyplot as plt

# Sample data
x = [1, 2, 3, 4, 5]
y = [10, 20, 15, 18, 25]

# Plotting
plt.plot(x, y)

# Setting custom xticks rotation
plt.xticks(rotation=-30)

# Display the chart
plt.show()

Output:

Matplotlib xticks rotation

In this example, we rotate the x-axis tick labels by an angle of -30 degrees, resulting in a counterclockwise rotation.

Example 4: Large rotation angle

import matplotlib.pyplot as plt

# Sample data
x = [1, 2, 3, 4, 5]
y = [10, 20, 15, 18, 25]

# Plotting
plt.plot(x, y)

# Setting large xticks rotation
plt.xticks(rotation=90)

# Display the chart
plt.show()

Output:

Matplotlib xticks rotation

In this example, we set the rotation angle to 90 degrees, resulting in a vertical orientation of the x-axis tick labels.

Example 5: Multiple rotations

import matplotlib.pyplot as plt

# Sample data
x = [1, 2, 3, 4, 5]
y = [10, 20, 15, 18, 25]

# Plotting
plt.plot(x, y)

# Setting multiple xticks rotations
plt.xticks(rotation=(45, -30, 90))

# Display the chart
plt.show()

In this example, we provide multiple rotation angles as a tuple. The x-axis tick labels rotate according to the provided sequence.

Example 6: Subplot with xticks rotation

import matplotlib.pyplot as plt

# Sample data
x = [1, 2, 3, 4, 5]
y = [10, 20, 15, 18, 25]

# Creating subplots
fig, axs = plt.subplots(2)

# Plotting on each subplot
axs[0].plot(x, y)
axs[1].bar(x, y)

# Setting xticks rotation on each subplot
axs[0].set_xticks(rotation=30)
axs[1].set_xticks(rotation=45)

# Display the subplots
plt.show()

In this example, we demonstrate xticks rotation on subplots. Each subplot has its own rotation angle for the x-axis tick labels.

Example 7: Using string tick labels

import matplotlib.pyplot as plt

# Sample data
x = ['A', 'B', 'C', 'D', 'E']
y = [10, 20, 15, 18, 25]

# Plotting
plt.plot(x, y)

# Setting xticks rotation
plt.xticks(rotation=60)

# Display the chart
plt.show()

Output:

Matplotlib xticks rotation

Here, we use string labels on the x-axis. The x-axis tick labels are rotated by an angle of 60 degrees.

Example 8: Customize the font size

import matplotlib.pyplot as plt

# Sample data
x = [1, 2, 3, 4, 5]
y = [10, 20, 15, 18, 25]

# Plotting
plt.plot(x, y)

# Setting xticks rotation and font size
plt.xticks(rotation=45, fontsize=10)

# Display the chart
plt.show()

Output:

Matplotlib xticks rotation

In this example, not only do we rotate the x-axis tick labels by 45 degrees, but we also customize the font size to 10.

Example 9: Using scientific notation

import matplotlib.pyplot as plt

# Sample data
x = [1000, 2000, 3000, 4000, 5000]
y = [10, 20, 15, 18, 25]

# Plotting
plt.plot(x, y)

# Setting xticks rotation and scientific notation
plt.xticks(rotation=90, style='sci', scilimits=(0,0))

# Display the chart
plt.show()

In this example, we rotate the x-axis tick labels by 90 degrees and display them in scientific notation.

Example 10: Modifying tick positions

import matplotlib.pyplot as plt

# Sample data
x = [1, 2, 3, 4, 5]
y = [10, 20, 15, 18, 25]

# Plotting
plt.plot(x, y)

# Setting custom xticks and rotation
plt.xticks([1, 2, 3, 4, 5], ['A', 'B', 'C', 'D', 'E'], rotation=45)

# Display the chart
plt.show()

Output:

Matplotlib xticks rotation

In this example, we provide custom tick positions and tick labels on the x-axis. The labels are then rotated by 45 degrees.

Matplotlib xticks rotation Conclusion

Controlling the rotation of x-axis tick labels in Matplotlib using the xticks rotation feature is a powerful customization option. With various rotation angles and additional options like font size, string labels, and scientific notation, we can enhance the readability and aesthetics of our data visualizations. Incorporate xticks rotation into your Matplotlib

Like(0)