I need to take an image and save it after some process. The shape looks good when I display it, but after saving the shape, I got free space around the saved image. I tried the 'tight' option for the savefig method, it didn't work either. The code:
import matplotlib.image as mpimg import matplotlib.pyplot as plt fig = plt.figure(1) img = mpimg.imread(path) 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()
I am trying to draw a basic graph using NetworkX in the drawing and save it. I realized that without a graph, this works, but when I add a graph, I get a space around the saved image;
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("C:\\images\\1.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()
python matplotlib
Ahmet Tuฤrul Bayrak Aug 07 2018-12-12T00: 00Z
source share