Matplotlib:将字符串添加为自定义X标记,但还保留现有的(数字)刻度标签吗? matplotlib.pyplot.annotate的替代品? [英] Matplotlib: Add strings as custom x-ticks but also keep existing (numeric) tick labels? Alternatives to matplotlib.pyplot.annotate?

查看:253
本文介绍了Matplotlib:将字符串添加为自定义X标记,但还保留现有的(数字)刻度标签吗? matplotlib.pyplot.annotate的替代品?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试生成图形,但在注释时遇到了一些问题.

I am trying to produce a graph and I am having some issues annotating it.

我的图形在x轴上有一个对数刻度,显示时间.我想要做的是将现有的(但不可预测的)数字刻度标签保持在100个单位,1000个单位,10000个单位等,但还要在x轴上添加自定义刻度标签,以使其在哪里更人性化"可读的"时间间隔出现-例如,我希望能够标记一周",一个月","6个月"等.

My graph has a log scale on the x-axis, showing time. What I want to be able to do is keep the existing (but not predictable) numeric tick labels at 100 units, 1000 units, 10000 units, etc but also add custom tick labels to the x-axis that make it clear where more "human readable" time intervals occur---for instance I want to be able to label 'one week', 'one month', '6 months', etc.

我可以使用matplotlib.pyplot.annotate()标记点,但是它并没有真正做到我想要的.我真的不希望文本和箭头位于图表的顶部,我只想添加一些额外的自定义刻度线.有什么想法吗?

I can use matplotlib.pyplot.annotate() to mark the points but it doesn't really do what I want. I don't really want text and arrows on top of my graph, I just want to add a few extra custom tick marks. Any ideas?

推荐答案

如果您真的想添加额外的刻度,则可以使用axis.xaxis.get_majorticklocs()获取现有的刻度,添加要添加的内容,然后使用axis.xaxis.set_ticks(<your updated array>).

If you really want to add extra ticks, you can get the existing ones using axis.xaxis.get_majorticklocs(), add whatever you want to add, and then set the ticks using axis.xaxis.set_ticks(<your updated array>).

一种替代方法是使用axvline 添加垂直线 .优点是您不必担心将自定义刻度添加到现有数组中,而需要手动注释行.

An alternative would be to add vertical lines using axvline. The advantage is that you don't have to worry about inserting your custom tick into the existing array, but you'll have to annotate the lines manually.

另一种替代方法是添加带有自定义刻度的链接轴.

Yet another alternative would be to add a linked axis with your custom ticks.

这篇关于Matplotlib:将字符串添加为自定义X标记,但还保留现有的(数字)刻度标签吗? matplotlib.pyplot.annotate的替代品?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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