简体   繁体   English

如何在matplotlib中添加其他格式的刻度线标签

[英]How to add additional tick labels, formatted differently, in matplotlib

I'm having real problems plotting a 2nd line of tick labels on x-axis directly underneath the original ones. 我在将x轴的第二行刻度标签绘制在原始标签的正下方时遇到了实际问题。 I'm using seaborn so I need to add these extra labels after the plot is rendered. 我使用的是seaborn,因此在绘制情节后需要添加这些额外的标签。

Below is what I'm trying to achieve but I'd like the gap between the 2 rows of tick labels to be a bit bigger and make the 2nd row bold and a different colour. 以下是我要实现的目标,但我希望两行刻度标签之间的间隙更大一些,并使第二行的粗体和颜色不同。

在此处输入图片说明

My attempts involve hacking the existing tick labels and appending the new strings underneath separated by newline: 我的尝试涉及破解现有的刻度标签,并在新的字符串后面添加新行:

            # n_labels is list that has the new labels I wish to plot directly
            # underneath the 1st row
            locs = ax.get_xticks().tolist()
            labels = [x.get_text() for x in ax.get_xticklabels()]
            nl = ['\n'.join([l, n_labels[i]]) for i, l in enumerate(labels)]
            ax.set_xticks(locs)
            ax.set_xticklabels(nl)

Any ideas? 有任何想法吗? Thank you! 谢谢!

One possibility would be to create a second x-axis on top of the first, and adjust the position and xtickslabels of the second x-axis. 一种可能性是在第一个x轴的顶部创建第二个x轴,并调整第二个x轴的位置和xtickslabels。 Check this example in the documentation as a starting point. 从文档中检查此示例作为起点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM