1

I am seeking a better understanding of python plotting routines in pandas/matplotlib that cause xticklabels to disappear when formatting is attempted. To demonstrate, here is some basic code to recreate the problem:

#Create dummy variables x and y in a dataframe and plot in a scatter plot with the xticklabels rotated 35 degrees
data={'x':[1111,2222,3333,4444], 'y':[1211,1322,1260,5555]};
df=pd.DataFrame(data=data);
ax1=df.plot('x', 'y', kind='scatter', c='lightblue', s=50, edgecolors='black')
#Create dummy variables x and y in a dataframe and plot in a scatter plot with the xticklabels rotated 35 degrees
data={'x':[1111,2222,3333,4444], 'y':[1211,1322,1260,5555]};
df=pd.DataFrame(data=data);
ax1=df.plot('x', 'y', kind='scatter', c='lightblue', s=50, edgecolors='black')

This generates a generic plot with labeled x- and y- axes and x- and y- ticklabels. (generic x-y scatter plot with default axis and tickmark labels)

Now, if I want to rotate the xticklabels and horizontally align them to the right, that is where I run into the disappearing xticklabels:

#I would like to change the angle of rotation of the xticklabels of the ax1 handle.
labels=ax1.get_xticklabels()
print(labels)
ax1.set_xticklabels(labels, rotation=40)
ax1.set_xticklabels(labels, ha="right")

Now the xticklabels have disappeared. I've added a line to print the xticklabels because most examples perform the set_xticklabels when custom labels are being affixed to the plot. I just want to use what was already plotted. I think that the problem may lie in the contents of the 'labels' variable that I populated with get_xticklabels, but I am having trouble connecting the dots here. Any help understanding this will be greatly appreciated!

brosenheim
  • 172
  • 1
  • 14
  • 1
    Having said that, there is no need to retrieve the labels per se. Just tell matplotlib that is has to [rotate them](https://stackoverflow.com/a/56139690/8881141) – Mr. T Dec 01 '20 at 17:17
  • 1
    ^^^ Note that `dataframe.plot` also has a `rot` parameter. Though the above approach is probably easiest since you also want to change the horizontal alignment. – BigBen Dec 01 '20 at 17:20
  • Your code does not cause the xticks to disappear on my machine. – Guimoute Dec 01 '20 at 18:25

1 Answers1

0

If you don't need pandas, you could just do this.

import matplotlib.pyplot as plt

data = {'x': [1111, 2222, 3333, 4444], 'y': [1211, 1322, 1260, 5555]};
fig, ax = plt.subplots()
ax.scatter(data['x'], data['y'], ec='k', fc='lightblue', s=50)
plt.xticks(rotation=45, ha='right')
plt.show()
M. Small
  • 138
  • 5
  • Object-orientated version: `ax.xaxis.set_tick_params(rotation=45, ha="right")` and `fig.show()`. – Guimoute Dec 01 '20 at 18:27
  • `ValueError: keyword ha is not recognized; valid keywords are ['size', 'width', 'color'...` and `fig.show()` collapses immediately in my environment. – Mr. T Dec 01 '20 at 19:31
  • This works also on my machine - thank you. But it doesn't help me understand the issue, so I had a question about your answer. How does the plt.xticks call know what axes to adjust with the properties in the call? If I had subplots, for instance, would it adjust every x-axis? Similarly, in @BigBen's approach above, the rot command in pandas does not make it clear what axis (x or y) will be adjusted by the rotation angle. in the pyplot version, xticks accesses x-axis properties and yticks accesses y-axis properties - this is not made clear with the rot command in pandas. Thank you. – brosenheim Dec 02 '20 at 13:34