Matplotlib艺术家可以在放大时保持相同大小吗? [英] Matplotlib artists to stay the same size when zoomed in?

查看:56
本文介绍了Matplotlib艺术家可以在放大时保持相同大小吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用matplotlib在图形上绘制一些艺术家(特别是几个矩形).我想以某种方式锚定这些,以便无论使用什么缩放级别,它们都保持相同的大小.我用过google进行搜索,并且阅读了大多数艺术家的文档,但没有运气找到需要固定矩形大小的功能.

I'm using matplotlib to draw some artists (a couple rectangles, specifically) on a graph. I'd like to anchor these somehow so that they remain the same size no matter what zoom level is used. I've used google to search and I've read through most of the artist documentation and have had no luck finding what function I need to anchor the rectangle size.

我希望得到详细说明如何执行此功能的答案,但如果您能让我大致知道如何执行此操作,或者甚至给我一些关键字以使我的 google 搜索更有效,我将不胜感激:)

I'd love an answer detailing how to perform this functionality, but if you could just let me know roughly how to do this, or even throw me some keywords to make my google search more effective, I'd really appreciate it :)

谢谢!

推荐答案

只需将 transform=ax.transAxes 关键字应用于 PolygonRectangle实例.如果将修补程序锚定到图形而不是轴上更有意义,则也可以使用 transFigure .此处是有关转换的教程.

Simply apply the transform=ax.transAxes keyword to the Polygon or Rectangle instance. You could also use transFigure if it makes more sense to anchor the patch to the figure instead of the axis. Here is the tutorial on transforms.

这是一些示例代码:

from matplotlib import pyplot as plt
from matplotlib.patches import Polygon
import numpy as np
x = np.linspace(0,5,100)
y = np.sin(x)

plt.plot(x,y)
ax = plt.gca()

polygon = Polygon([[.1,.1],[.3,.2],[.2,.3]], True, transform=ax.transAxes)
ax.add_patch(polygon)

plt.show()

如果不想使用轴坐标系放置多边形,而是希望使用数据坐标系放置多边形,则可以使用变换在定位之前静态转换数据.最好的例子在这里:

If you do not want to place your polygon using axis coordinate system but rather want it positioned using data coordinate system, then you can use the transforms to statically convert the data before positioning. Best exemplified here:

from matplotlib import pyplot as plt
from matplotlib.patches import Polygon
import numpy as np

x = np.linspace(0,5,100)
y = np.sin(x)

plt.plot(x,y)
ax = plt.gca()

dta_pts = [[.5,-.75],[1.5,-.6],[1,-.4]]

# coordinates converters:
#ax_to_display = ax.transAxes.transform
display_to_ax = ax.transAxes.inverted().transform
data_to_display = ax.transData.transform
#display_to_data = ax.transData.inverted().transform

ax_pts = display_to_ax(data_to_display(dta_pts))

# this triangle will move with the plot
ax.add_patch(Polygon(dta_pts, True)) 
# this triangle will stay put relative to the axes bounds
ax.add_patch(Polygon(ax_pts, True, transform=ax.transAxes))

plt.show()

这篇关于Matplotlib艺术家可以在放大时保持相同大小吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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