画像を撮って、何らかのプロセスを経て保存する必要があります。Figure を表示すると問題ないように見えますが、Figure を保存した後、保存した画像の周りに空白ができてしまいました。'tight'
メソッドのオプションを試しましたがsavefig
、うまくいきませんでした。コード:
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
fig = plt.figure(1)
img = mpimg.imread("image.jpg")
plt.imshow(img)
ax=fig.add_subplot(1, 1, 1)
extent = ax.get_window_extent().transformed(fig.dpi_scale_trans.inverted())
plt.savefig('1.png', bbox_inches=extent)
plt.axis('off')
plt.show()
Figure に NetworkX を使用して基本的なグラフを描画し、保存しようとしています。グラフがなくても機能することに気付きましたが、グラフを追加すると、保存された画像の周りに空白ができます。
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
import networkx as nx
G = nx.Graph()
G.add_node(1)
G.add_node(2)
G.add_node(3)
G.add_edge(1, 3)
G.add_edge(1, 2)
pos = {1:[100, 120], 2:[200, 300], 3:[50, 75]}
fig = plt.figure(1)
img = mpimg.imread("image.jpg")
plt.imshow(img)
ax=fig.add_subplot(1, 1, 1)
nx.draw(G, pos=pos)
extent = ax.get_window_extent().transformed(fig.dpi_scale_trans.inverted())
plt.savefig('1.png', bbox_inches = extent)
plt.axis('off')
plt.show()