Search code examples
pythonpandasplotlineboxplot

Delete an artist from a figure


I have a boxplot created with pandas (with all the lines saved in dict):

tup = ....boxplot(column = [plot_it], by = 'C', ax = ax, return_type = 'both')

How can I remove all the artists (lines added by the boxplot) from the figure,so that I can reuse those lines?

I tried to copy.deepcopy() the lines and use the copies in a new figure but that failed -> "Can not put single artist in more than one figure."

fig, ax = plt.subplots()
data = tup[0].lines['whiskers']
line = copy.deepcopy(data[0])
ax.add_line(line)

P.S.: I solved the problem at least it does what I expcet it to do but it's quite nasty:

    fig, ax = plt.subplots()
    tup = self.stat.alls.boxplot(column = [plot_it], by = 'C', ax = ax, return_type = 'both')#,ax = ax
    data = tup[0].lines['whiskers']
    line = data[0]
    xo = []
    xu = []
    yo = []
    yu = []
    x_med = []
    y_med = []
    for j in range(200):
        x_med.append(tup[0].lines['medians'][j].get_data()[0][0])
        y_med.append(tup[0].lines['medians'][j].get_data()[1][0])
    for j in range(0,400):
        if j%2 == 0:
            xo.append(data[j].get_data()[0][0])
            yo.append(data[j].get_data()[1][0])
        else:
            xu.append(data[j].get_data()[0][0])
            yu.append(data[j].get_data()[1][0])
    lineo = lines.Line2D(xo,yo)
    lineu = lines.Line2D(xu,yu)
    line_med = lines.Line2D(x_med, y_med)
    self.fig, ax = plt.subplots()
    ax.set_title(self.log_id)
    plt.xlabel('Cycle')
    plt.ylabel(plot_it + ' [' + Si[plot_it] + ']')
    
    plt.plot(lineo.get_data()[0], lineo.get_data()[1])
    plt.plot(lineu.get_data()[0], lineu.get_data()[1])
    plt.plot(line_med.get_data()[0], line_med.get_data()[1])
    ax.set_xticks(np.arange(0,max(self.stat.alls['C']),20))

Solution

  • I solved the problem at least it does what I expcet it to do but it's quite nasty:

    fig, ax = plt.subplots()
    tup = self.stat.alls.boxplot(column = [plot_it], by = 'C', ax = ax, return_type = 'both')#,ax = ax
    data = tup[0].lines['whiskers']
    line = data[0]
    xo = []
    xu = []
    yo = []
    yu = []
    x_med = []
    y_med = []
    for j in range(200):
        x_med.append(tup[0].lines['medians'][j].get_data()[0][0])
        y_med.append(tup[0].lines['medians'][j].get_data()[1][0])
    for j in range(0,400):
        if j%2 == 0:
            xo.append(data[j].get_data()[0][0])
            yo.append(data[j].get_data()[1][0])
        else:
            xu.append(data[j].get_data()[0][0])
            yu.append(data[j].get_data()[1][0])
    lineo = lines.Line2D(xo,yo)
    lineu = lines.Line2D(xu,yu)
    line_med = lines.Line2D(x_med, y_med)
    self.fig, ax = plt.subplots()
    ax.set_title(self.log_id)
    plt.xlabel('Cycle')
    plt.ylabel(plot_it + ' [' + Si[plot_it] + ']')
    
    plt.plot(lineo.get_data()[0], lineo.get_data()[1])
    plt.plot(lineu.get_data()[0], lineu.get_data()[1])
    plt.plot(line_med.get_data()[0], line_med.get_data()[1])
    ax.set_xticks(np.arange(0,max(self.stat.alls['C']),20))