微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

添加从另一个库生成的图像作为 matplotlib 中的插图

如何解决添加从另一个库生成的图像作为 matplotlib 中的插图

我已经使用 vedo生成一个网络图,我正在尝试将其作为插图添加matplotlib生成的图

import networkx as nx
import matplotlib.pyplot as plt

from vedo import *
from matplotlib.offsetBox import Offsetimage,AnnotationBBox


G = nx.gnm_random_graph(n=10,m=15,seed=1)
nxpos = nx.spring_layout(G,dim=3,seed=1)

nxpts = [nxpos[pt] for pt in sorted(nxpos)]
nx_lines = [(nxpts[i],nxpts[j]) for i,j in G.edges()]

pts = Points(nxpts,r=12)
edg = Lines(nx_lines).lw(2)

# node values
values = [[1,0],[30,80,10,79,70,60,75,78,65,10],[1,.30,.10,.79,.70,.60,.75,.78,.65,.90]]
time = [0.0,0.1,0.2]  # in seconds

vplt = Plotter(N=1)
pts1 = pts.cmap('Blues',values[0])
vplt.show(
    pts1,edg,axes=False,bg='white',at=0,interactive=False,zoom=1.5
).screenshot("network.png")

ax = plt.subplot(111)
ax.plot(
    [1,2,3],'go-',label='line 1',linewidth=2
 )

arr_img = vplt.screenshot(returnNumpy=True,scale=1)
im = Offsetimage(arr_img,zoom=0.25)
ab = AnnotationBBox(im,(1,0),xycoords='axes fraction',Box_alignment=(1.1,-0.1),frameon=False)
ax.add_artist(ab)
plt.show()
ax.figure.savefig(
    "output.svg",transparent=True,dpi=600,bBox_inches="tight"
)

插图中的图像分辨率太低。关于如何在不损失分辨率的情况下添加插图的建议将非常有帮助。

编辑: 下面发布的答案适用于添加 2D 网络,但我仍在寻找有助于在插图中添加 3D 网络的方法

解决方法

我不熟悉 vedo,但一般过程是创建一个 inset_axis 并使用 imshow 绘制图像。但是,您的代码使用具有 networkx 绑定的 matplotlib,您可以直接执行此操作而无需 vedo

编辑:为 3d 绘图编辑的代码

enter image description here

import networkx as nx
import matplotlib.pyplot as plt

G = nx.gnm_random_graph(n=10,m=15,seed=1)
nxpos = nx.spring_layout(G,dim=3,seed=1)

nxpts = [nxpos[pt] for pt in sorted(nxpos)]
nx_lines = [(nxpts[i],nxpts[j]) for i,j in G.edges()]

# node values
values = [[1,0],[30,80,10,79,70,60,75,78,65,10],[1,.30,.10,.79,.70,.60,.75,.78,.65,.90]]
time = [0.0,0.1,0.2]  # in seconds


fig,ax = plt.subplots()
ax.plot(
    [1,2,3],'go-',label='line 1',linewidth=2
 )

from mpl_toolkits.mplot3d import (Axes3D)
from matplotlib.transforms import Bbox
rect = [.6,.5,.5]
bbox = Bbox.from_bounds(*rect)
inax = fig.add_axes(bbox,projection = '3d')
# inax = add_inset_axes(,#                       ax_target = ax,#                       fig = fig,projection = '3d')

# inax.axis('off')


# set angle
angle = 25
inax.view_init(10,angle)

# hide axes,make transparent
# inax.set_facecolor('none')
# inax.grid('off')
import numpy as np

# plot 3d
seen = set()
for i,j in G.edges():
    x = np.stack((nxpos[i],nxpos[j]))
    inax.plot(*x.T,color = 'k')
    if i not in seen:
        inax.scatter(*x[0],color = 'skyblue')
        seen.add(i)
    if j not in seen:
        inax.scatter(*x[1],color = "skyblue")
        seen.add(j)

fig.show()

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。