Matplotlib打开两个图表......我只想要一个

时间:2016-03-19 22:51:50

标签: python matplotlib plot

所以我正在制作一个绘图剧本。我将使用包含大量数据的文本文件...就像一万个坐标的顺序。所以我想初始化一个创建绘图的对象然后我可以多次显示该绘图而无需一次又一次地重新加载所有数据。

我正在制作两种类型的图表......位置(x,y,z)和能量与时间(t,KE)。

一切都在我的代码中有效,但是当我在课堂上调用其中一个图形(即位置或能量,但不是两者)时,它会显示两者。

有什么想法吗?

代码如下:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

def make_plot(x, y, z, title):
    '''
    returns a plt object that can be saved to show later
    x, y, z are lists of floats, but the 0th index is a string: the title of that axes.
    title is a string
    '''
    fig = plt.figure()
    #If there is no z list, the graph should only be 2d (energy vs time)
    if z != None:
        plot = fig.add_subplot(111, projection='3d')
        plot.set_zlabel(z[0])
        plot.plot(x[1:], y[1:], z[1:], c='r', marker='o')
        plot.set_xlabel(x[0])
        plot.set_ylabel(y[0])
        plot.set_zlabel(z[0])
    else:
        plt.plot(x[1:], y[1:], c='r', marker='o')
        plt.xlabel(x[0])
        plt.ylabel(y[0])
    plt.title(title)
    return plt  

class Plot(object):
    '''
    Makes every plot its own object.
    '''
    def __init__(self, x, y, z, title):
        self.p = make_plot(x, y, z, title)
    def show_plot(self):
        self.p.show()

class Graph(object):
    '''
    Reads from a file that has 5 columns: x, y, z, t, KE
    There are two graphs I want to make; a position graph (x, y, z)
    and a Kinetic energy vs Time graph (t, KE)
    '''
    def __init__(self, filename, xlabel='X', ylabel='Y', zlabel='Z', tlabel='t', elabel='KE', title='Title'):
        self.title = title
        f = open(filename, 'r')
        data = f.readlines()
        f.close()
        #first index of the lists are what I am going to name the axes
        self.xList, self.yList, self.zList, self.tList, self.eList = [xlabel], [ylabel], [zlabel], [tlabel], [elabel]
        lines = []
        for element in data:
            lines.append(element.split())
        for element in lines:
            #In case I decide later to add comments in my file with the points
            #I added this try statement so it won't add a comment to my point lists
            try:
                self.xList.append(float(element[0]))
                self.yList.append(float(element[1]))
                self.zList.append(float(element[2]))
                self.tList.append(float(element[3]))
                self.eList.append(float(element[4]))
            except:
                pass
        #I want to make a position plot and an energy plot that is loaded
        #and ready to show whenever.
        self.position = Plot(self.xList, self.yList, self.zList, self.title)
        self.energy = Plot(self.tList, self.eList, None, self.title)
    def plot_position(self):
        self.position.show_plot()
    def plot_energy(self):
        self.energy.show_plot()

a = Graph(filename='table.txt')
a.plot_position()
#I only want it to graph the position graph here, but it graphs the position and energy graphs.

0 个答案:

没有答案