簡體   English   中英

添加從另一個庫生成的圖像作為 matplotlib 中的插圖

[英]Adding image generated from another library as inset in 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, 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"
)

插圖中的圖像分辨率太低。 關於如何在不損失分辨率的情況下添加插圖的建議將非常有幫助。

編輯:下面發布的答案適用於添加 2D 網絡,但我仍在尋找有助於在插圖中添加 3D 網絡的方法。

我不熟悉vedo ,但一般程序是使用 imshow 創建一個inset_axisimshow圖像。 但是,您的代碼使用的是具有matplotlib綁定的networkx ,您可以直接執行此操作而無需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()

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM