Matplotlib - show only one shape

So, I have some graphs that my code generates. I want to save them all, but I only want to display them. I can do the save part just fine, but getting only one to display is a pain.

He gathered in circles, trying to achieve this. The closest I got was to clear all but one digit, but they all displayed when I tried to show only one. Having started to think that this is something very simple, I miss it, otherwise it is impossible.

Does anyone know how to achieve this?

Edit: added sample code. I apologize for not having done this.

fig1 = plt.figure(1) plt.plot([0, 1, 2, 3, 4], [0, 1, 2, 3, 4], label="Test", color='g') plt.plot([0, 1, 2, 3, 4], [0, 1, 4, 9, 16], label="Other Test", color='r') plt.grid(True) fig1.savefig('Foo1.png') fig2 = plt.figure(2) plt.plot([0, 1, 2, 3, 4], [0, 5, 1, 9, 2], label="Test 2", color='g') plt.plot([0, 1, 2, 3, 4], [0, 10, 50, 0, 10], label="Other Test 2", color='r') plt.grid(True) fig2.savefig('Foo2.png') plt.show() 
+6
source share
1 answer

You can close each shape immediately after saving with plt.close() . Just remember to make a closed expression after the last digit.

Your code will look like this:

 fig1 = plt.figure(1) plt.plot([0, 1, 2, 3, 4], [0, 1, 2, 3, 4], label="Test", color='g') plt.plot([0, 1, 2, 3, 4], [0, 1, 4, 9, 16], label="Other Test", color='r') plt.grid(True) fig1.savefig('Foo1.png') # add plt.close() after you've saved the figure plt.close() fig2 = plt.figure(2) plt.plot([0, 1, 2, 3, 4], [0, 5, 1, 9, 2], label="Test 2", color='g') plt.plot([0, 1, 2, 3, 4], [0, 10, 50, 0, 10], label="Other Test 2", color='r') plt.grid(True) fig2.savefig('Foo2.png') plt.show() 
+10
source

Source: https://habr.com/ru/post/986781/


All Articles