Matplotlib轴文本坐标不一致? [英] Matplotlib axis text coordinates inconsistency?

查看:65
本文介绍了Matplotlib轴文本坐标不一致?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一段代码来自动对齐不同数量的子图的 x 轴标签.当我开始手动设置标签位置时遇到麻烦时,我检查以确保可以从一组坐标转换为另一组坐标而不更改任何内容,并使用如下代码段:

I'm working on a piece of code to automatically align x-axis labels for a variable number of subplots. When I started having trouble setting label positions manually, I checked to be sure I could just transform from one set of coordinates to the other without changing anything, with a code snippet like this:

# xaxes is a list of Axes objects
textCoords = [ax.xaxis.get_label().get_position() for ax in xaxes]
newCoords = [ax.transAxes.inverted().transform(ax.xaxis.get_label().\
    get_transform().transform(c)) for ax,c in zip(xaxes,textCoords)]
for ax,c in zip(xaxes,newCoords): ax.xaxis.set_label_coords(*c)

理论上,这段代码不会改变任何坐标;它只是获取每个标签的坐标,使用 Text 对象的内部存储变换将其映射到 Axes 坐标,然后设置位置.但是运行此代码会完全删除我的标签,并且进行一些实验表明它们脱离了图表的底部边缘.

In theory, this code doesn't change any coordinates; it just gets the coordinates of each label, maps it to Axes coordinates using the Text object's internally-stored transform, and then sets the position. Yet running this code removes my labels entirely, and a little experimentation shows that they go off the bottom edge of the plot.

我是不是误解了这里涉及的转换?

Have I just misunderstood the transforms involved here?

推荐答案

您正确地理解了转换,但是要注意在显示图形之前使用显示坐标.

You're understanding the transforms correctly, but there's a caveat to using display coordinates before the plot has been displayed.

简短的答案是,在上述代码段之前调用 plt.draw()可以解决您的直接问题.

The short answer is that putting in a call to plt.draw() before your code snippet above will fix your immediate problem.

您正在尝试通过显示坐标链接不同的轴显示系统.但是,在第一次绘制绘图之前,渲染器还没有完全初始化,显示坐标没有太大意义.

You're trying to link the different axes display system through display coordinates. However, before the plot has been drawn the first time, the renderer isn't fully initialized yet, and the display coordinates don't have much meaning.

您能详细说明您要做什么吗?可能有一种更简单的方法.

Can you elaborate a bit more on what you're trying to do? There may be an easier way.

或者,如果要避免多余的 draw ,则可以在绘制图形之前通过图形坐标链接事物.(无论如何,它们的定义都很好.)

Alternatively, if you want to avoid the extra draw, you can link things through figure coordinates before the plot has been drawn. (They're well defined regardless.)

这篇关于Matplotlib轴文本坐标不一致?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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