• Home
  • rotate plot in matplotlib figure by 90 degree – Python

rotate plot in matplotlib figure by 90 degree – Python

To rotate a plot in Matplotlib by 90 degrees, you can use the rotate_degrees parameter in the set_rotation method of the xaxis or yaxis object of the plot.

Here is an example of how to rotate the x-axis tick labels of a plot by 90 degrees:

import matplotlib.pyplot as plt

# Create a plot
plt.plot([1, 2, 3, 4])

# Rotate the x-axis tick labels by 90 degrees
plt.xticks(rotation=90)

# Show the plot
plt.show()

You can also use the rotation parameter in the set_tick_params method to rotate the tick labels:

import matplotlib.pyplot as plt

# Create a plot
plt.plot([1, 2, 3, 4])

# Rotate the x-axis tick labels by 90 degrees
plt.gca().xaxis.set_tick_params(rotation=90)

# Show the plot
plt.show()

You can use a similar approach to rotate the y-axis tick labels by 90 degrees. Simply use the yaxis object instead of the xaxis object in the above examples.

Note that you can use a negative value for the rotation parameter to rotate the tick labels in the opposite direction. For example, a rotation of -90 degrees will rotate the tick labels by 270 degrees.