How to obtain xticks with alternating distances?

不问归期 提交于 2020-06-27 16:17:55

问题


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:

Desired example:

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

回答1:


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()

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()



来源:https://stackoverflow.com/questions/61354147/how-to-obtain-xticks-with-alternating-distances

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!