创建一个绘图,将其保存到文件,然后加载文件并在matplotlib中的新绘图旁边绘图

时间:2017-11-26 18:01:43

标签: python matplotlib

我想在matplotlib中创建一个条形图:

fig, ax = plt.subplots()
oldbar = ax.bar(x=ind, height=y, width=width)

然后我想把这个条形图腌制成文件(字典或轴 - 我不确定哪个是正确的):

pickle.dump(oldbar, file('oldbar.pkl', 'w'))  

然后我想重新加载这个文件,然后将旧条形图放在一个新的条形图旁边,这样我就可以在一个轴上进行比较:

fig, ax = plt.subplots()
newbar = ax.bar(x=ind, height=y, width=width)
oldbar = pickle.load(file('oldbar.pkl'))

# I realise the line below doesn't work
ax.bar(oldbar) 

plt.show()

理想情况下,我希望将它们呈现如下。关于我如何解决这个问题的任何建议?

enter image description here

2 个答案:

答案 0 :(得分:2)

你会腌制这个人物,而不是其中的艺术家。

import matplotlib.pyplot as plt
import numpy as np
import pickle

ind = np.linspace(1,5,5)
y   = np.linspace(9,1,5)
width = 0.3

dic = {"ind":ind, "y":y, "width":width}

pickle.dump(dic, file('olddata.pkl', 'w'))  

### new data
ind2 = np.linspace(1,5,5)
y2   = np.linspace(8,2,5)
width2 = 0.3

olddic = pickle.load(file('olddata.pkl'))

fig, ax = plt.subplots()
ax.bar(x=olddic["ind"], height=olddic["y"], width=olddic["width"])
ax.bar(x=ind2+olddic["width"], height=y2, width=width2)
ax.set_xlabel("x label")
plt.show()

然而,在这里腌制数据本身可能更有意义。

var chat_interval = setInterval(function()
{
    $.post("chat_update.php",  function(data) { $("#rect_comments_text").append(data);}  );
}, 4000);

答案 1 :(得分:1)

也许这会有所帮助:

import pickle as pkl
import matplotlib.pyplot as plt
import numpy as np


class Data_set(object):
    def __init__(self, x=[], y=[], name='data', pklfile=None,
                 figure=None, axes=None):
        """
        """
        if pklfile is None:
            self.x = np.asarray(x)
            self.y = np.asarray(y)
            self.name = str(name)
        else:
            self.unpickle(pklfile)
        self.fig = figure
        self.ax = axes
        self.bar = None

    def plot(self, width=0, offset=0, figure=None, axes=None):

        if self.fig is None:
            if figure is None:
                self.fig = plt.figure()
                self.ax = self.fig.subplots(1, 1)
            else:
                self.fig = figure
                if axes is None:
                    self.ax = self.fig.subplots(1, 1)
                else:
                    self.ax = axes

        # maybe there's no need to keep track of self.fig, .ax and .bar,
        # but just in case...
        if figure is not None:
            fig_to_use = figure
            if axes is not None:
                ax_to_use = axes
            else:
                ax_to_use = fig_to_use.subplots(1, 1)
        else:
            fig_to_use = self.fig
            ax_to_use = self.ax
        if not width:
            width = (self.x[1]-self.x[0]) / 2.
        self.bar = ax_to_use.bar(x=self.x+offset, height=self.y, width=width)
        return fig_to_use, ax_to_use, self.bar

    def pickle(self, filename='', ext='.pkl'):
        if filename == '':
            filename = self.name
        with open(filename+ext, 'w') as output_file:
            pkl.dump((self.name, self.x, self.y), output_file)

    def unpickle(self, filename='', ext='.pkl'):
        if filename == '':
            filename = self.name
        with open(filename + ext, 'r') as input_file:
            # the name should really come from the filename, but then the
            # above would be confusing? 
            self.name, self.x, self.y = pkl.load(input_file)


class Data_set_manager(object):
    def __init__(self, datasets={}):
        self.datasets = datasets

    def add_dataset(self, data_set):
        self.datasets[data_set.name] = data_set

    def add_dataset_from_file(self, filename, ext='.pkl'):
        self.datasets[filename] = Data_set(name=filename)
        self.datasets[filename].unpickle(filename=filename, ext=ext)

    def compare(self, width=0, offset=0, *args):
        self.fig = plt.figure()
        self.ax = self.fig.subplots(1, 1)
        if len(args) == 0:
            args = self.datasets.keys()
            args.sort()
        n = len(args)
        if n == 0:
            return None, None
        if width == 0:
            min_dx = None
            for dataset in self.datasets.values():
                sorted_x = dataset.x.copy()
                sorted_x.sort()
                try:
                    new_min_dx = np.min(dataset.x[1:] - dataset.x[:-1])
                except ValueError:
                    # zero-size array to reduction operation minimum which
                    # has no identity (empty array)
                    new_min_dx = None
                if new_min_dx < min_dx or min_dx is None:
                    min_dx = new_min_dx
            if min_dx is None:
                min_dx = 1.
            width = float(min_dx) / (n + 1)
            offset = float(min_dx) / (n + 1)
        offsets = offset*np.arange(n)
        if n % 2 == 0:
            offsets -= offsets[n/2] - offset/2.
        else:
            offsets -= offsets[n/2]
        i = 0
        for name in args:
            self.datasets.get(name, Data_set()).plot(width=width,
                                                     offset=offsets[i],
                                                     figure=self.fig,
                                                     axes=self.ax)
            i += 1
        self.ax.legend(args)
        return self.fig, self.ax


if __name__ == "__main__":
    # test saving/loading
    name = 'test'
    to_pickle = Data_set(x=np.arange(10),
                         y=np.random.rand(10),
                         name=name)
    to_pickle.pickle()
    unpickled = Data_set(pklfile=name)
    print unpickled.name == to_pickle.name

    # test comparison
    blorg = Data_set_manager({})
    x_step = 1.
    n_bars = 4  # also try an odd number
    for n in range(n_bars):
        blorg.add_dataset(Data_set(x=x_step * np.arange(n_bars),
                                   y=np.random.rand(n_bars),
                                   name='teste' + str(n)))
    fig, ax = blorg.compare()
    fig.show()

它应该适用于偶数和奇数条: even number of bars odd number of bars

只要您记录了您使用过的名称(提示:查看保存它们的文件夹),您就可以重新加载数据并将其与新数据进行比较。

可以进行更多检查(确保文件存在,x轴是可以在尝试之前减去的东西等),并且它也可以使用一些文档和正确的测试 - 但这应该匆忙做