How can I move tick labels by a few pixels?
In my case, I want to move the This is class #N-labels on the X-axis to the right by a few pixels.
I am aware of the horizontalalign
/ha
values right
, center
, and left
; but I want to "improve" the looks of the right
alignment.
Take the following example which will produce the plot shown below:
import pandas as pd
import numpy as np
categories = ['This is class #{}'.format(n) for n in range(10)]
data = {
'Value': [categories[np.random.randint(10)] for _ in range(100)],
'id': [1000+i for i in range(100)]
}
df = pd.DataFrame(data)
ax = df.Value.value_counts().sort_index().plot(kind='bar', rot=45)
plt.xticks(ha='right')
Results in:
I think, subjectively, that the plot would look better if the labels were translated to the right so that the tick were placed over the "#". In other words, a "middle ground" between the right
and center
alignment options.
Side notes:
I'm using pandas, but I believe that is irrelevant to the issue, as it's using matplotlib to do its plotting anyway.
The plt.xticks()
method is used for simplicity, I could just as well use ax.set_xticklabels()
, but I don't need to rewrite the label texts, and AFAIK there is no shortcut to set the horizontal alignment without also copying the existing labels into with ax.set_xticklabels(labels, **more_options)
, as ha
is not a valid key in matplotlib 2's ax.xaxis.set_tick_params()
-method.
I'm aware of pandas' Series.hist()
-method, but I think the Series.value_counts().plot(kind='bar')
looks prettier when I have few categories and want the number of bars to be the same as number of categories.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…