0

I am trying to manually curate a dataset by going through each set and choosing whether or not to keep it or reject it. To do this, I want to plot a dataset, use the click module (https://click.palletsprojects.com/en/7.x/) to choose whether to keep it, then plot the next dataset and repeat. Currently, I am able to plot each set and choose whether or not to save it, but the problem is each graph remains above the next one. I need to go through thousands of datasets so it isn't viable to have them all plotted simultaneously.

for q in np.arange(0,len(x)):

    Thing = Data[x[q]-100:x[q]+400]
  
    Thing2 = Data2[x[q]-100:x[q]+400]
        
    plt.plot(Thing)
    plt.plot(Thing2)
    plt.show()

    if click.confirm('Do you want to save?', default=True):
            List.append(x[q])

I have seen other people recommending pyplot.ion or the matplotlib animations, but neither of them seem to be working, perhaps due to the interaction with the "Click" input box appearing. All I want to do is close each plot before the next one is made, but it is proving impossible!

Thanks in advance.

SAPJ
  • 3
  • 1
  • You can use the Object Orientated approach of `matplotlib`. Then you can clear the graph and draw a new one over it. For information about the OOP, see [this](https://matplotlib.org/matplotblog/posts/pyplot-vs-object-oriented-interface/) or [this](https://matplotlib.org/stable/tutorials/introductory/lifecycle.html) tutorial. And for clearing a figure check [this](https://stackoverflow.com/questions/8213522/when-to-use-cla-clf-or-close-for-clearing-a-plot-in-matplotlib) post or [here](https://www.pythonpool.com/clear-plot-matplotlib/). – Thymen May 06 '21 at 19:02
  • Unfortunately this hasn't resolved the issue, even with OOP - it is still plotting the graph, opening the input box, then just creating a new graph beneath it. – SAPJ May 07 '21 at 13:07

1 Answers1

0

A solution would have to clear the current figure and draw a new figure on the cleared canvas. There are multiple approaches that would work, which are pretty well described in

Since you are showing two graphs, I am first providing a solution that works on a single graph. Then, I am going to show a solution that works with multiple rows or columns.


Solution

Single

The following solution and test code was used to update a single graph, and combine the data that you want to save.

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


def single(data, n_splits):
    """ Split up a dataset in n splits, and then combine the parts that you want to save.  """
    fig, ax = plt.subplots()
    results = []

    for thing in np.hsplit(data, n_splits):
        ax.cla()
        ax.plot(thing)
        fig.show()
        plt.pause(0.0001)

        if click.confirm('Do you want to save?', default=True):
            results.append(thing)

    plt.close(fig=fig)
    return np.hstack(results) if results else []

Example code

if __name__ == '__main__':
    # Single data example
    data = np.random.randn(1000) * 10
    result = single(data, n_splits=2)

    fig, ax = plt.subplots()
    ax.plot(result)
    fig.show()
    plt.pause(5)

Double

The following code is used to show two different plots at the same time, using subplots. Alternatively you can generate two figures and separately plot the different graphs.

def double(data, n_splits):
    """ Split up a dataset in n splits, and then combine the parts that you want to save.  """
    fig, axs = plt.subplots(nrows=2, squeeze=True)
    results = []

    for thing_1, thing_2 in np.hsplit(data, n_splits):

        for ax, thing in zip(axs, [thing_1, thing_2]):
            ax.cla()
            ax.plot(thing)

        fig.show()
        plt.pause(0.0001)

        if click.confirm('Do you want to save?', default=True):
            results.append([thing_1, thing_2])

    plt.close(fig=fig)
    return np.hstack(results) if results else []

Example code

if __name__ == '__main__':
    # Multiple plots example
    data = np.random.randn(2, 1000) * 10
    results = double(data, n_splits=2)

    fig, axs = plt.subplots(nrows=2, squeeze=True)
    for result, ax in zip(results, axs):
        ax.plot(result)

    fig.show()
    plt.pause(5)
Thymen
  • 2,089
  • 1
  • 9
  • 13