How to use plt.xticks() in this case?

Screen Link:
https://app.dataquest.io/m/143/multiple-plots/5/adding-data

My Code:

import matplotlib.pyplot as plt
%matplotlib inline
fig=plt.figure()
ax1=fig.add_subplot(2,1,1)
ax2=fig.add_subplot(2,1,2)
ax1.plot(unrate.loc[0:11,"DATE"],unrate.loc[0:11,"VALUE"])
ax2.plot(unrate.loc[12:23,"DATE"],unrate.loc[12:23,"VALUE"])
plt.xticks(rotation=90)

What I expected to happen:
I expected that both the plots will get the rotation of their x-axis labels with 90 degrees.

What actually happened:
Only the second plot ax2 got it’s x labels rotated with 90 degrees.

Can we actually use sticks in the case when we are building multiple blocks? If yes, how can we change the rotation of the x-label using sticks for all the plots in the figure (Container)?

1 Like

Try using:

ax1.set_xticklabels(unrate.loc[0:11,"DATE"], rotation=90)
ax2.set_xticklabels(unrate.loc[12:23,"DATE"], rotation=90)

1 Like

Goodmorning,
yes I used it thank you for help. But after this figsize parameter is required too.

You can also add in figure size as an argument when you create the figure:

import matplotlib.pyplot as plt
%matplotlib inline
fig=plt.figure(figsize=(width, height))
ax1=fig.add_subplot(2,1,1)
ax2=fig.add_subplot(2,1,2)
ax1.plot(unrate.loc[0:11,“DATE”],unrate.loc[0:11,“VALUE”])
ax2.plot(unrate.loc[12:23,“DATE”],unrate.loc[12:23,“VALUE”])
plt.xticks(rotation=90)

2 Likes

So use the figsize parameter. Just like @cam.mcbroom showed you.

1 Like

One way of rotating axes ticks is using ax.set_xticklabels(rotation = 90).
Documentation link :-
https://matplotlib.org/2.0.0/api/_as_gen/matplotlib.axes.Axes.set_xticklabels.html

1 Like

Got it, Thanks a lot guys.

1 Like