在Matplotlib中添加从另一个库生成的图像作为插图 [英] Adding image generated from another library as inset in matplotlib

查看:48
本文介绍了在Matplotlib中添加从另一个库生成的图像作为插图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 vedo 库生成了一个网络图形,我试图将其作为插图添加到在 matplotlib

I've generated a network figure using vedo library and I'm trying to add this as an inset to a figure generated in 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, 0, 0, 0, 0, 0, 0, 0, 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], [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"
)

插图中的图像分辨率太低.有关如何添加插图而不失分辨率的建议将非常有帮助.

There resolution of the image in the inset is too low. Suggestions on how to add the inset without loss of resolution will be really helpful.

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

The answer posted below works for adding a 2D network, but I am still looking for ways that will be useful for adding a 3D network in the inset.

推荐答案

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

I am not familiar with vedo but the general procedure would be to create an inset_axis and plot the image with imshow. However, your code is using networkx which has matplotlib bindings and you can directly do this without vedo

为3D绘图编辑的代码

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, 0, 0, 0, 0, 0, 0, 0, 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], [1, 2, 3],
    'go-',
    label='line 1',
    linewidth=2
 )

from mpl_toolkits.mplot3d import (Axes3D)
from matplotlib.transforms import Bbox
rect = [.6, 0, .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()

这篇关于在Matplotlib中添加从另一个库生成的图像作为插图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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