0

(I am sort new to python so the class hierarchy of python still isn't quite clear for me)

So I have ulti.py which helps me to plot things.

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
class Util:    
    def plot(self, i, data):            
        plt.plot(data[0],data[1])
        plt.title("test" + str(i))
        plt.show()
        plt.savefig("test" + str(i) + '.png')

And then when i do

from util import Util

uti1 = Util()
uti2 = Util()
print (uti1.plot(1,[[0,1,2],[0,1,2]]))
print (uti2.plot(2,[[0,1,2],[2,1,0]]))

from another file, to my surprise that the plot from uti2 instance also have the plot from uti1 instance overlapped together!

So my question is that is this the same for importing every module in python? I later find out i can add plt.figure(i) to evade this problem. But how can I avoid it from a good design of the classes? Or is it python just designed this way.

Lance Ruo Zhang
  • 401
  • 4
  • 12
  • Here's a relevant discussion: http://stackoverflow.com/questions/5993206/is-it-possible-to-have-multiple-pyplot-windows-or-am-i-limited-to-subplots (note a link to http://stackoverflow.com/questions/2397791/how-can-i-show-figures-separately-in-matplotlib/2399978#2399978!) – DYZ Jan 20 '17 at 03:34
  • 1
    This is where the matplotlib object-oriented approach helps a lot (i.e. use `ax.plot`, `ax.set_title`, etc., instead of `plt.plot`, `plt.title`). See http://matplotlib.org/faq/usage_faq.html#coding-styles – tmdavison Jan 20 '17 at 11:09

0 Answers0