Shannon
Shannon

Reputation: 1205

How to obtain xticks with alternating distances?

I have a bar plot where the x-axis has the 2-letter labels of countries. I have to choose a very small font, so the x-ticks do not overlap with each other. Is there any way to assign a distance between x-ticks and the x-axis, so I can choose a larger font without labels being overlapped?

Currently:

enter image description here

Desired example:

axis:  ----------------------
ticks: c1  c3  ...
         c2  c4   ...

Upvotes: 5

Views: 980

Answers (1)

JohanC
JohanC

Reputation: 80339

An approach is to create minor ticks at the alternating positions and give them a larger tick length. A MultipleLocator of 2 for the major ticks puts them every 2. Adding a MultipleLocator of 1 for the minor ticks fills in the gaps, as major ticks automatically suppress minor ticks in overlapping positions. The color of the ticks can be made lighter to obtain more contrast between the ticks and the labels.

The same approach would work when the plot would be generated via seaborn or pandas, as long as an explicit list of labels can be provided.

from matplotlib import pyplot as plt
from matplotlib import ticker
import numpy as np

letters = list('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
N = 80
names = [letters[i // 26] + letters[i % 26] for i in range(N)]
values = np.random.binomial(100, 0.1, N)

cmap = plt.cm.get_cmap('rainbow')
colors = [cmap(i / N) for i in range(N)]
plt.bar(names, values, color=colors)
ax = plt.gca()
ax.xaxis.set_major_locator(ticker.MultipleLocator(2))
ax.xaxis.set_minor_locator(ticker.MultipleLocator(1))
ax.xaxis.set_minor_formatter(ticker.IndexFormatter(names))
ax.tick_params(axis='x', which='minor', length=15)
ax.tick_params(axis='x', which='both', color='lightgrey')
ax.autoscale(enable=True, axis='x', tight=True)

plt.show()

example plot

PS: @MadPhysicist's idea from the comments to add newlines is even simpler. It looks a bit different:

from matplotlib import pyplot as plt
import numpy as np

letters = list('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
N = 80
names = [('\n' if i % 2 == 1 else '') + letters[i // 26] + letters[i % 26] for i in range(N)]
values = np.random.binomial(100, 0.1, N)

cmap = plt.cm.get_cmap('rainbow')
colors = [cmap(i / N) for i in range(N)]
plt.bar(names, values, color=colors)
plt.gca().autoscale(enable=True, axis='x', tight=True)
plt.show()

plot using newlines

Upvotes: 6

Related Questions