- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我写了一段代码来制作不规则的光栅图(即其中光栅矩形的大小是可变的)。下面是一个最小的可重现示例。
问题是在我的示例中循环 block 非常慢(每个图都有很多矩形并且有很多图要制作)。我试图将坐标转换为元组列表,但这引发了错误。
是否有可能获取 patches.Rectangle 以返回一个补丁列表,而不是一个,这样我就可以摆脱补丁上的循环并加快代码速度?
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import numpy as np
import matplotlib.colorbar as cbar
fig,ax=plt.subplots(1)
rng=6
plt.ylim(0,rng)
plt.xlim(0,rng)
N = 30
x = np.random.rand(N)*rng
y = np.random.rand(N)*rng
s = np.random.rand(N)
colors=np.random.rand(N)
normal = plt.Normalize(0,1) # my numbers from 0-1
cmap=plt.cm.RdYlBu_r
c=cmap(colors)
for i in range(N):
val=0.5
rect=patches.Rectangle((x[i],y[i]),s[i],s[i],
edgecolor='black',
linewidth = 1,
facecolor = c[i],
)
ax.add_patch(rect)
cax, _ = cbar.make_axes(ax)
cb2 = cbar.ColorbarBase(cax, cmap=cmap,norm=normal)
plt.savefig("test.png")
输出:
最佳答案
一句话总结:使用PolyCollection
。
使用集合来绘制许多形状肯定比绘制单个矩形更有效。 other answer建议使用 PatchCollection
。更高效的方法是使用 PolyCollection
。
原因有两个:
我对有关颜色定义(最好让集合为您完成)和颜色栏(使用集合,而不是独立的颜色栏)的代码做了一些修改
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import numpy as np
from matplotlib.collections import PatchCollection, PolyCollection
import matplotlib.transforms as mtrans
补丁集合:
def patchcoll(N, show=False):
fig,ax=plt.subplots()
rng=6
plt.ylim(0,rng+1)
plt.xlim(0,rng+1)
x = np.random.rand(N)*rng
y = np.random.rand(N)*rng
s = np.random.rand(N)
c = np.random.rand(N)
norm = plt.Normalize(0,1) # my numbers from 0-1
cmap=plt.cm.RdYlBu_r
pat = []
for i in range(N):
rect=patches.Rectangle((x[i],y[i]),s[i],s[i])
pat.append(rect)
col = PatchCollection(pat, cmap=cmap, norm=norm)
col.set_array(c)
col.set_edgecolor('k')
col.set_linewidth(1.)
ax.add_collection(col)
fig.colorbar(col)
if show:
plt.show()
else:
fig.canvas.draw()
plt.close()
聚合集合:
def polycoll(N, show=False):
fig,ax=plt.subplots()
rng=6
plt.ylim(0,rng)
plt.xlim(0,rng)
x = np.random.rand(N)*rng
y = np.random.rand(N)*rng
s = np.random.rand(N)
c = np.random.rand(N)
norm = plt.Normalize(0,1) # my numbers from 0-1
cmap=plt.cm.RdYlBu_r
offsets = np.c_[x,y]
verts = list(zip([0,1,1,0,0], [0,0,1,1,0]))
col = PolyCollection([verts], sizes=s, offsets=offsets,
transOffset=mtrans.IdentityTransform(),
offset_position="data", cmap=cmap, norm=norm)
col.set_array(c)
col.set_edgecolor('k')
col.set_linewidth(1.)
ax.add_collection(col)
fig.colorbar(col)
if show:
plt.show()
else:
fig.canvas.draw()
plt.close()
单个矩形:
def rectangles(N, show=False):
fig,ax=plt.subplots()
rng=6
plt.ylim(0,rng)
plt.xlim(0,rng)
x = np.random.rand(N)*rng
y = np.random.rand(N)*rng
s = np.random.rand(N)
c = np.random.rand(N)
norm = plt.Normalize(0,1) # my numbers from 0-1
cmap=plt.cm.RdYlBu_r
for i in range(N):
rect=patches.Rectangle((x[i],y[i]),s[i],s[i],
facecolor=cmap(norm(c[i])), edgecolor="k", linewidth=1)
ax.add_patch(rect)
sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
sm.set_array([])
fig.colorbar(sm)
if show:
plt.show()
else:
fig.canvas.draw()
plt.close()
全部运行:
patchcoll(30, show=True)
polycoll(30,show=True)
rectangles(30,show=True)
对于 N=1000
我得到
%timeit(rectangles(1000))
757 ms ± 4.26 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%timeit(patchcoll(1000))
184 ms ± 462 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
%timeit(polycoll(1000))
58.3 ms ± 146 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
所以在这种情况下,使用 PatchCollection
比单个矩形的效率高 3 倍,而使用 PolyCollection
的效率比 PatchCollection 高 3 倍
。
使用上述 3 种不同方法创建具有 N
个矩形的图形所需时间的概述:
关于python - 使用 patches.Rectangle 绘制不规则光栅图时避免缓慢循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54463269/
有谁知道这种情况发生或应该发生的情况,粗略地浏览一下文档是没有帮助的。 最佳答案 只有在矩形不相交的情况下才会发生这种情况。例如 new Rectangle(0, 0, 10, 10).interse
我看到 System.Drawing.Rectangle 类有两组属性: X、Y、宽度、高度 左、上、右、下 Width 和Right 之间的区别很明显。但我不明白 Left 和 Top 属性背后的原
给定一个元组列表(包括 x, y, width, height 这四个值以二维坐标定义一个矩形)。目标是在原始列表中检查一个矩形是否在另一个矩形内部(如果是,则只取较小的,较大的丢弃) 最佳答案 如果
我去的论坛上有人说我不应该使用 Rectangle.intersects 进行碰撞检测,而应该使用这个算法: boolean rectangleIntersects(float rect1x, flo
Rectangle 类扩展了 ClosedShape 类。当我尝试创建 Rectangle 的实例时,出现编译器错误: constructor Rectangle in class Rectangle
以下是intersects(Rectangle r) awt Rectangle 的方法 源代码。 我添加一些以 //* 开头的评论与我的问题相关: 由于代码验证 rw (例如)为正,则 rw rw+
我正在使用 Dr. Java,语言是 java ...我是初学者* 只是一个基本的矩形 问题: 创建一个构造 Rectangle 对象 (java.awt.Rectangle) 的程序。该对象应具有
有没有办法知道 libgdx 中两个 Rectangle 之间的交集 Rectangle 面积,就像 C# 中的 Rectangle http://msdn.microsoft.com/en-us/l
所以我正在做一个Java作业,我必须创建一个矩形类,该类在一个绘制重叠矩形的程序中使用,并且在矩形重叠的地方,用新的颜色绘制一个新的矩形。我添加了硬件描述的链接,因为我认为让您查看它比我试图解释它更容
我有一个网格,用户可以在上面绘制矩形(实际上是房子里的房间)。由于房间不能相互交叉,因此我尝试在绘制第二个房间之前检查是否存在冲突。到目前为止,我设法检查第二个矩形的“目标点”是否在第一个矩形内部(=
我不明白为什么这个程序不起作用。我是 C++ 的新手,在学习了三年 Java 后转行。我认为 Java 中的错误消息毫无意义,但我在 C++ 中得到的错误只是直截了当的胡言乱语。这是我真正能理解的。
在 OnPaint 方法中绘图时,我一直偏离 1 个像素。我不明白为什么。 不过,我不确定是不是我算不上! 我已经回到绘图板,在 1 个面板中使用 1 个标签,因为我确定我正确地计算了这些,我有大量的
找到适合空白空间的面积最大的矩形的最有效算法是什么? 假设屏幕看起来像这样('#' 代表填充区域): .................... ..............###### ##.....
我正在查看一个包含两个矩形的控件:一个在另一个矩形内。我希望用户能够拖动内部矩形,调整它的大小,并在可能的情况下在外部矩形的范围内旋转它。这些值应该是可绑定(bind)的,以便我可以在更新时将这些值存
两者有什么区别?使用其中任何一种时,我都没有发现我的项目有任何差异,但我不知道这两种方法有什么用,这让我很困扰。 请指教! 最佳答案 布局矩形是控件的外观视觉尺寸。它与实际框架不同,因为在控件的视觉不
你可以有一个父类(super class) Shape,Square 和 Rectangle 是两个子类,但是你可以有 Square 子类 Rectangle,因为 Square 是一个四边相等的特殊
我正在阅读以下代码,并开始想知道 Rectangle.prototype = Object.create(Shape.prototype) 和 Rectangle.prototype = Shape.
我看到了两种不同的模式和解释。来自 DailyJS 和许多其他人的一篇:矩形.prototype = new Shape(); 然后是 Crockford 的 here 这意味着只是 矩形.proto
Sorry, the original image cannot be uploaded due to some security reasons. The following is a sch
以下代码是教程的一部分。我已经无数次根据教程检查代码,虽然它在视频上有效,但我的程序有以下错误: 1>------ Build started: Project: simpleclass, Confi
我是一名优秀的程序员,十分优秀!