Matplotlib: plot multiple plots using the same drawing, without overlapping

I have a class that I use to create stories and then save them in a file. Here is a simplified version:

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

class Test():
    def __init__(self, x, y, filename):

        fig = plt.figure(1)
        ax = fig.add_subplot(111)

        ax.plot(x, y, 'D', color='red')

        ax.set_xbound(-5,5)
        ax.set_ybound(-5,5)

        plt.savefig('%s.png' % filename)


test1 = Test(1,2, 'test1')
test2 = Test(2,4, 'test2')

Here are the results:

test1 bwo2S.png

test2 aKu3N.png

The problem is that the image of test2 also has a point from test1. Graphs are generated dynamically in a loop, so I cannot specify the number of a digit.

I could make a counter and pass it to the class constructor, but I was wondering if there is a more elegant way to do this. I tried to delete the test1 object but did nothing.

+3
source share
1 answer

clf, , . , pyplot.clf .

, , pyplot.figure num - , .

+9

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


All Articles