如何在Matplotlib中交错或偏移x轴标签? [英] How do I stagger or offset x-axis labels in Matplotlib?

查看:46
本文介绍了如何在Matplotlib中交错或偏移x轴标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种简单的方法可以使x轴标签与附件中的图像相似.

I was wondering if there is an easy way to offset x-axis labels in a way similar to the attached image.

推荐答案

您可以循环遍历 x 轴刻度并增加每隔一个刻度的间距,使它们低于其他刻度.一个最小的例子是:

You can loop through your x axis ticks and increase the pad for every other tick so that they are lower than the other ticks. A minimal example would be:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

ax.plot([1,2,3,4,5])

ax.set_xticks([1,2,3,4,5])
ax.set_xticklabels(["A","B","C","D","E",])

# [1::2] means start from the second element in the list and get every other element
for tick in ax.xaxis.get_major_ticks()[1::2]:
    tick.set_pad(15)

plt.show()

这篇关于如何在Matplotlib中交错或偏移x轴标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆