Home > Software design >  Rotate Matplotlib x-axis label text
Rotate Matplotlib x-axis label text

Time:05-18

I have a plot that looks like this (this is the famous Wine dataset):

enter image description here

As you can see, the x-axis labels overlap and thus I need to be rotated.

NB! I am not interested in rotating the x-ticks (as explained here), but the label text, i.e. alcohol, malic_acid, etc.

The logic of creating the plot is the following: I create a grid using axd = fig.subplot_mosaic(...) and then for the bottom plots I set the labels with axd[...].set_xlabel("something"). Would be great if set_xlabel would take a rotation parameter, but unfortunately that is not the case.

CodePudding user response:

Based on the documentation set_xlabel accepts text arguments, of which rotation is one.

The example I used to test this is shown below, though .

import matplotlib.pyplot as plt
import numpy as np

plt.plot()
plt.gca().set_xlabel('Test', rotation='vertical')
  • Related