Autofmt_xdate removes the x-axis labels of all subnets

I use autofmt_xdate to draw long labels along the x axis in a readable way. The problem is that when I want to combine different subheadings, the X axis marking of the other subplots disappears, which I don’t understand for the leftmost subheading in the figure below (two rows high). Is there a way to prevent autofmt_xdate from extinguishing other X axis labels? Or is there another way to rotate labels? As you can see, I experimented with xticks and β€œrotate”, but the results were not satisfactory because the labels were rotated around their center, which led to random marking.

Script that creates the graph below:

 from matplotlib import pyplot as plt from numpy import arange import numpy from matplotlib import rc rc("figure",figsize=(15,10)) #rc('figure.subplot',bottom=0.1,hspace=0.1) rc("legend",fontsize=16) fig = plt.figure() Test_Data = numpy.random.normal(size=20) fig = plt.figure() Dimension = (2,3) plt.subplot2grid(Dimension, (0,0),rowspan=2) plt.plot(Test_Data) plt.subplot2grid(Dimension, (0,1),colspan=2) for i,j in zip(Test_Data,arange(len(Test_Data))): plt.bar(i,j) plt.legend(arange(len(Test_Data))) plt.subplot2grid(Dimension, (1,1),colspan=2) xticks = [r"%s (%i)" % (a,b) for a,b in zip(Test_Data,Test_Data)] plt.xticks(arange(len(Test_Data)),xticks) fig.autofmt_xdate() plt.ylabel(r'$Some Latex Formula/Divided by some Latex Formula$',fontsize=14) plt.plot(Test_Data) #plt.setp(plt.xticks()[1],rotation=30) plt.tight_layout() #plt.show() 

Figure created by script

+8
python matplotlib
source share
1 answer

This is actually a function of the autofmt_xdate method. From the documentation of the autofmt_xdate method:

Date mark keys often overlap, so it’s useful to rotate them and align them to the right. In addition, a common use case is a lot of subheadings with common xaxes, where the x axis is date data. Most often, tags are often long, and this helps to rotate them on the lower subtitle and turn them off on other subplots , as well as turn off xlabels.

If you want to rotate only the hexadecimal characters of the lower right subtitle, use

 plt.setp(plt.xticks()[1], rotation=30, ha='right') # ha is the same as horizontalalignment 

This rotates the label marks 30 degrees, and the right one aligns them (the same result as when using autofmt_xdate ) for the lower right subheading, leaving the other two subheadings unchanged.

+6
source share

All Articles