- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试在 networkx
边缘上绘制轮廓 (linestyle=":"
)。我似乎无法弄清楚如何对 matplotlib
patch
对象执行此操作? 现在有人知道如何操作这些补丁
对象来绘制这些“边缘”的轮廓吗?如果这不可能,有人知道如何获取线数据以使用ax.plot(x,y,linestyle=":")
分别做这个?
import networkx as nx
import numpy as np
from collections import *
# Graph data
G = {'y1': OrderedDict([('y2', OrderedDict([('weight', 0.8688325076457851)])), (1, OrderedDict([('weight', 0.13116749235421485)]))]), 'y2': OrderedDict([('y3', OrderedDict([('weight', 0.29660515972204304)])), ('y4', OrderedDict([('weight', 0.703394840277957)]))]), 'y3': OrderedDict([(4, OrderedDict([('weight', 0.2858185316736193)])), ('y5', OrderedDict([('weight', 0.7141814683263807)]))]), 4: OrderedDict(), 'input': OrderedDict([('y1', OrderedDict([('weight', 1.0)]))]), 'y4': OrderedDict([(3, OrderedDict([('weight', 0.27847763084646443)])), (5, OrderedDict([('weight', 0.7215223691535356)]))]), 3: OrderedDict(), 5: OrderedDict(), 'y5': OrderedDict([(6, OrderedDict([('weight', 0.5733512797415756)])), (2, OrderedDict([('weight', 0.4266487202584244)]))]), 6: OrderedDict(), 1: OrderedDict(), 2: OrderedDict()}
G = nx.from_dict_of_dicts(G)
G_scaffold = {'input': OrderedDict([('y1', OrderedDict())]), 'y1': OrderedDict([('y2', OrderedDict()), (1, OrderedDict())]), 'y2': OrderedDict([('y3', OrderedDict()), ('y4', OrderedDict())]), 1: OrderedDict(), 'y3': OrderedDict([(4, OrderedDict()), ('y5', OrderedDict())]), 'y4': OrderedDict([(3, OrderedDict()), (5, OrderedDict())]), 4: OrderedDict(), 'y5': OrderedDict([(6, OrderedDict()), (2, OrderedDict())]), 3: OrderedDict(), 5: OrderedDict(), 6: OrderedDict(), 2: OrderedDict()}
G_scaffold = nx.from_dict_of_dicts(G_scaffold)
G_sem = {'y1': OrderedDict([('y2', OrderedDict([('weight', 0.046032370518141796)])), (1, OrderedDict([('weight', 0.046032370518141796)]))]), 'y2': OrderedDict([('y3', OrderedDict([('weight', 0.08764771571290508)])), ('y4', OrderedDict([('weight', 0.08764771571290508)]))]), 'y3': OrderedDict([(4, OrderedDict([('weight', 0.06045928834718992)])), ('y5', OrderedDict([('weight', 0.06045928834718992)]))]), 4: OrderedDict(), 'input': OrderedDict([('y1', OrderedDict([('weight', 0.0)]))]), 'y4': OrderedDict([(3, OrderedDict([('weight', 0.12254141747735424)])), (5, OrderedDict([('weight', 0.12254141747735425)]))]), 3: OrderedDict(), 5: OrderedDict(), 'y5': OrderedDict([(6, OrderedDict([('weight', 0.11700701511079069)])), (2, OrderedDict([('weight', 0.11700701511079069)]))]), 6: OrderedDict(), 1: OrderedDict(), 2: OrderedDict()}
G_sem = nx.from_dict_of_dicts(G_sem)
# Edge info
edge_input = ('input', 'y1')
weights_sem = np.array([G_sem[u][v]['weight']for u,v in G_sem.edges()]) * 256
# Layout
pos = nx.nx_agraph.graphviz_layout(G_scaffold, prog="dot", root="input")
# Plotting graph
pad = 10
with plt.style.context("ggplot"):
fig, ax = plt.subplots(figsize=(8,8))
linecollection = nx.draw_networkx_edges(G_sem, pos, alpha=0.5, edges=G_sem.edges(), arrowstyle="-", edge_color="#000000", width=weights_sem)
x = np.stack(pos.values())[:,0]
y = np.stack(pos.values())[:,1]
ax.set(xlim=(x.min()-pad,x.max()+pad), ylim=(y.min()-pad, y.max()+pad))
for path, lw in zip(linecollection.get_paths(), linecollection.get_linewidths()):
x = path.vertices[:,0]
y = path.vertices[:,1]
w = lw/4
theta = np.arctan2(y[-1] - y[0], x[-1] - x[0])
# ax.plot(x, y, color="blue", linestyle=":")
ax.plot((x-np.sin(theta)*w), y+np.cos(theta)*w, color="blue", linestyle=":")
ax.plot((x+np.sin(theta)*w), y-np.cos(theta)*w, color="blue", linestyle=":")
经过几次思想实验,我意识到我需要计算角度然后相应地调整垫子:
例如,如果线是完全垂直的(在 90 或 -90),则 y 坐标根本不会移动,而 x 坐标会移动。角度为 0 或 180 的线会发生相反的情况。
然而,它仍然有点偏离。
我怀疑这是相关的: matplotlib - Expand the line with specified width in data unit?
我不认为linewidth
直接转换为数据空间
或者,如果可以将这些线集合转换为矩形对象,那也是可能的。
最佳答案
将一定宽度的线围成另一条线的问题是,线是用数据坐标定义的,而线宽是物理单位,即点。这通常是可取的,因为它允许线宽独立于数据范围、缩放级别等。它还确保线的末端始终垂直于线,与轴方向无关。
因此线条的轮廓始终处于混合坐标系中,最终的外观在使用渲染器绘制实际线条之前并不确定。因此,对于考虑(可能变化的)坐标的解决方案,需要确定图形当前状态的轮廓。
一种选择是使用新的艺术家,它将现有的 LineCollection
作为输入,并根据线条在像素空间中的当前位置创建新的变换。
下面我选择了一个PatchCollection
。从一个矩形开始,我们可以缩放和旋转它,然后将它平移到原始直线的位置。
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection, PatchCollection
import matplotlib.transforms as mtrans
class OutlineCollection(PatchCollection):
def __init__(self, linecollection, ax=None, **kwargs):
self.ax = ax or plt.gca()
self.lc = linecollection
assert np.all(np.array(self.lc.get_segments()).shape[1:] == np.array((2,2)))
rect = plt.Rectangle((-.5, -.5), width=1, height=1)
super().__init__((rect,), **kwargs)
self.set_transform(mtrans.IdentityTransform())
self.set_offsets(np.zeros((len(self.lc.get_segments()),2)))
self.ax.add_collection(self)
def draw(self, renderer):
segs = self.lc.get_segments()
n = len(segs)
factor = 72/self.ax.figure.dpi
lws = self.lc.get_linewidth()
if len(lws) <= 1:
lws = lws*np.ones(n)
transforms = []
for i, (lw, seg) in enumerate(zip(lws, segs)):
X = self.lc.get_transform().transform(seg)
mean = X.mean(axis=0)
angle = np.arctan2(*np.squeeze(np.diff(X, axis=0))[::-1])
length = np.sqrt(np.sum(np.diff(X, axis=0)**2))
trans = mtrans.Affine2D().scale(length,lw/factor).rotate(angle).translate(*mean)
transforms.append(trans.get_matrix())
self._transforms = transforms
super().draw(renderer)
请注意实际变换是如何仅在draw
时计算的。这确保它们考虑了像素空间中的实际位置。
用法可能看起来像
verts = np.array([[[5,10],[5,5]], [[5,5],[8,2]], [[5,5],[1,4]], [[1,4],[2,0]]])
plt.rcParams["axes.xmargin"] = 0.1
fig, (ax1, ax2) = plt.subplots(ncols=2, sharex=True, sharey=True)
lc1 = LineCollection(verts, color="k", alpha=0.5, linewidth=20)
ax1.add_collection(lc1)
olc1 = OutlineCollection(lc1, ax=ax1, linewidth=2,
linestyle=":", edgecolor="black", facecolor="none")
lc2 = LineCollection(verts, color="k", alpha=0.3, linewidth=(10,20,40,15))
ax2.add_collection(lc2)
olc2 = OutlineCollection(lc2, ax=ax2, linewidth=3,
linestyle="--", edgecolors=["r", "b", "gold", "indigo"],
facecolor="none")
for ax in (ax1,ax2):
ax.autoscale()
plt.show()
现在的想法当然是使用问题中的 linecollection
对象而不是上面的 lc1
对象。这应该很容易在代码中替换。
关于python - 如何在 Python 中的 Matplotlib 线上绘制外边缘的轮廓?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55911075/
我有一串数字。我需要检查边缘上的数字是否对称,这意味着它们以 2 为模时具有相同的余数。 我已经编写了一个有效的代码,但我对此感到不安,在一些失败之后我想出了这个代码: int PaliPair(ch
我要删除黑色蒙版和人体图像之间的白色边框 图像输入示例: 厚度为1的图像输出: 厚度为2的图像输出: 我尝试了一些在这里找到的具有模糊和阈值的游戏 我还使用此代码查找和绘制轮廓 thickne
这应该是一个在用户悬停时打开的飞出菜单。 这在 Chrome 中工作得很好,我已经尝试了多种方法(使用 CSS :hover,(mousenter)和(mouseleave ) 问题是 - 使用 Ed
我有一个 Apache 服务器 v2.4.43 为我的网站提供服务,我使用一个简单的 .htpasswd,我使用指令“AuthUserFile ”在 .htaccess 中调用它来进行身份验证。 现在
如何仅使用剪辑路径创建波浪边缘? 波浪应该是这样的图像: 最佳答案 您应该通过波函数及其频率生成此剪辑路径。 我用过 cos()在 PHP 中。您可以找到如下链接: https://repl.it/@
我有这张图: digraph G { 1 [label="car"]; 2 [label="x"]; 3 [label="car"]; 4 [label="y"]; 5 [label="cdr"];
我想强制点仅显示节点之间的垂直或水平边缘。 我在帖子 Family tree layout with Dot/GraphViz 中发现了类似的请求,但我不处理树,所以我希望有一个解决方案,无需插入额外
我已经设法让两个直方图重叠,但如果你仔细观察,条形开始倾斜并且没有完全重叠。 我已经调整了线宽和宽度,并没有改善。 我的目标是让所有的条形排列在彼此的顶部,黑边没有歪斜。 有什么办法解决这个问题 这是
我已经设法让两个直方图重叠,但如果你仔细观察,条形开始倾斜并且没有完全重叠。 我已经调整了线宽和宽度,并没有改善。 我的目标是让所有的条形排列在彼此的顶部,黑边没有歪斜。 有什么办法解决这个问题 这是
我一直在与 latex table 搏斗太久了。我需要一个具有居中标题的表格,以及包含可能环绕的文本的正文单元格。由于环绕要求,我使用 p{xxx} 而不是 l 来指定单元格宽度。这导致的问题是单元格
有没有办法平滑转换(平移和旋转)的 BufferedImage 的锯齿状边缘? 测试图像的放大 View : (请注意,这不是将要使用的实际 BufferedImage,仅用于此处演示)。 已使用双线
我在 my page 有一个旋转木马.我的父元素和子元素的边缘都是 flex 的,但是当您按“下一个”或“上一个”并滑动旋转木马时,子元素在移动时不会保持 flex 。我怎样才能避免这种情况? .ho
我在 .boxwrap 和 .lsmlbox + .rsmlbox 之间的嵌套 div 中添加了不必要的空格,这使得无法将 .smlbox + .rsmlbox 与 .box 对齐我相信这是一个比预期
我正在使用 Apple 的 ARKit 制作 AR 应用程序,我想制作盒子的线框轮廓。代码中描述并显示在图片中的那个在盒子的每个面上绘制斜边。它还隐藏了盒子另一侧的角。 Scene Kit 中有没有办
下图仅包含阈值处理后的黑色和白色像素。我在此图像的顶部绘制了一个灰色的旋转矩形。现在我想计算这个旋转矩形内的黑色像素数,但不包括白色矩形矩形外的黑色像素(即白色矩形内的像素数)。 做到这一点的最佳方法
你们如何处理边际 CSS?我所说的边缘是指需要斜体或粗体的单个单词或短语。只用 声明一个“大胆”的类似乎很愚蠢 Bold { font-weight: bold; } 或者斜体,要么! Italic
好的。我知道有几十个类似的线程。我几乎检查了每一个,但找不到可行的解决方案。我试过负边距、最小高度、最大高度、绝对页脚、正文背景颜色,甚至将溢出:隐藏到页面中的所有主要容器。然后将 clearfix
我网站的一部分使用代码使用倾斜边缘: -webkit-clip-path: 多边形(0% 0%, 100% 0%, 81% 100%, 0% 100%) 这在 Chrome 中运行良好,但不适用于任何
我一直在创建这个网站,并且进展顺利。然后我试着给它添加一个模态,但一切都有点走下坡路。问题是在添加模式后,Chrome 中的图像对齐方式发生了变化。它在 Edge 中运行良好。现在我知道这可能是因为
如果我在 Matlab 中使用 image() 命令制作一个 4 x 4 像素的图像,它会将刻度线居中放置在像素的中间。我希望刻度线以像素的左下角为中心。有什么方法可以做到这一点吗? 最佳答案 您可以
我是一名优秀的程序员,十分优秀!