Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to rotate xticks on one axis of figure in matplotlib without "getting" the labels as a list

suppose i have the following code which creates one matplotlib figure with two axes, the second of which has x-axis labels as dates:

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import datetime as dt    

x1 = np.arange(0,30)
x2 = pd.date_range('1/1/2016', periods=30, freq='D')
y1 = np.random.randn(30)
y2 = np.random.randn(30)

%matplotlib inline
fig, ax = plt.subplots(1,2, figsize=(18,5))
ax[0].scatter(x1,y1)
ax[1].scatter(x2,y2)

displaying this in an ipython notebook will show the x axis labels of the graph on the right as running into one another. i would like to rotate the labels to improve visibility. all of the documentation and online searching seems to suggest one of the following 2 options (both after the last line above):

#1

plt.setp(ax[1].xaxis.get_majorticklabels(),rotation=90,horizontalalignment='right')

or #2

plt.xticks(rotation=90)

either of these will work but will also print a list of labels (which for some reason is different in the first example than in the second)

how do i accomplish the rotation/display without also outputting some array?

like image 740
laszlopanaflex Avatar asked Dec 15 '22 03:12

laszlopanaflex


1 Answers

i was able to use this approach. not sure if it is the most elegant way, but it works without outputting an array

for tick in ax[1].get_xticklabels():
    tick.set_rotation(90)
like image 120
laszlopanaflex Avatar answered Jan 29 '23 12:01

laszlopanaflex