简体   繁体   English

matplotlib获取轴相对刻度位置

[英]matplotlib get axis-relative tick positions

I know that I can get the positions of my y-ticks by ax.get_yticks() (btw., is that the best/correct way to get them?). 我知道我可以通过ax.get_yticks()获取我的y-ticks的位置(顺便说一句,这是获取它们的最佳/正确方法吗?)。 But I need the tick positions relative to the axis limits (ie, between 0 and 1). 但是我需要相对于轴限制的刻度位置(即0到1之间)。 What is the best way to get that? 最好的方法是什么? I tried ax.get_yticks(transform=ax.transAxes) , which does not work. 我尝试了ax.get_yticks(transform=ax.transAxes) ,它不起作用。

This transformation can be easily done by hand: 可以轻松地手动完成此转换:

y_min, y_max = ax.get_ylim()
ticks = [(tick - y_min)/(y_max - y_min) for tick in ax.get_yticks()]

UPDATE UPDATE

To get it working with log scales, we need to employ matplotlib.transforms . 要使其与对数刻度一起使用,我们需要使用matplotlib.transforms But transforms can only work with (x,y) points, so we need first to complement y-ticks with some x coordinates (for example, zeros): 但是变换只能与(x,y)点一起使用,因此我们首先需要用一些x坐标(例如零)来补充y-ticks:

crd = np.vstack((np.zeros_like(ax.get_yticks()), ax.get_yticks())).T

Then we can transform data coodinates to display and then to axes and take y column of it: 然后,我们可以转换数据共坐标以显示,然后转换为轴并取其y列:

ticks = ax.transAxes.inverted().transform(ax.transData.transform(crd))[:,1]

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

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