将Matplotlib数据单位转换为归一化单位 [英] Convert matplotlib data units to normalized units
问题描述
有人知道如何将matplotlib数据单位转换为标准化单位吗?
Does anyone know how to convert matplotlib data units into normalized units?
我需要它的原因是我需要在另一个图的顶部创建一个子图. 以及默认语法:
The reason that I need it is that I need to create a subplot on top of another plot. And the default syntax:
plt.axes([0.1,0.1,0.3,0.3])
需要归一化的坐标,但是我想使用数据坐标:
requires normalized coordinates, but I want to use the data coordinates:
例如此代码:
plt.plot([0,2],[2,4]);
plt.axes([0.3,.3,0.4,.4])
产生此:
但我希望能够使用子图的数据坐标来定义子图的位置 ,例如[0.7,2.5,1.7,3.5].我试图摆弄axes.transData,axes.get_transform()之类的东西,但是没有成功找到合适的函数来完成工作
But I'd like to be able to define the location of the subplot using the data coordinates of it , something like [0.7,2.5,1.7,3.5]. I've tried to fiddle with axes.transData, axes.get_transform() and the like but didn't succeed to find the right function to do the job
推荐答案
这里是一种方法:
内部axes
打印在0.5, 2.5, 1.0, 0.3
上(在外部axes
坐标中)
inner axes
printed at 0.5, 2.5, 1.0, 0.3
(in outer axes
coords)
您基本上需要进行两种转换-一种从src-coords到display,另一种从display到dest-coord.从文档看来,似乎没有直接的方法:
http://matplotlib.org/users/transforms_tutorial.html
You basically need two transformations -- one from src-coords to display, and one from display to dest-coord. From the docs there seems to be no direct way:
http://matplotlib.org/users/transforms_tutorial.html
bb_data = Bbox.from_bounds(0.5, 2.5, 1.0, 0.3)
disp_coords = ax.transData.transform(bb_data)
fig_coords = fig.transFigure.inverted().transform(disp_coords)
ax
和fig
都带有变压器-显示坐标!
如果对它们调用inverted
,则会得到一个反方向的变压器.
ax
and fig
both carry transformer with them -- to display-coords!
If you call inverted
on them, you get an transformer for the inverse direction.
这是上面示例的完整代码:
Here's the full code for the above example:
import matplotlib.pyplot as plt
from matplotlib.transforms import Bbox
plt.plot([0,2], [2,4])
fig = plt.gcf()
ax = plt.gca()
bb_data = Bbox.from_bounds(0.5, 2.5, 1.0, 0.3)
disp_coords = ax.transData.transform(bb_data)
fig_coords = fig.transFigure.inverted().transform(disp_coords)
fig.add_axes(Bbox(fig_coords))
plt.show()
这篇关于将Matplotlib数据单位转换为归一化单位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!