1

There is a code for the decision tree classifier. I display several graphs in it. I call the graphs through functions with the definition of arguments. The problem is that the next chart doesn't open until I close the previous one. The problem went further, when using the tkinter, the second graph does not appear there at all, even if I close the first one.

Some code:

def visualize_classifier(classifier, x, y, heading = None, view_result = None):

    min_x, max_x = x[:, 0].min() - 1.0, x[:, 0].max() + 1.0
    min_y, max_y = x[:, 1].min() - 1.0, x[:, 1].max() + 1.0

    mesh_step_size = 0.01
    x_vals, y_vals = np.meshgrid(np.arange(min_x, max_x, mesh_step_size), np.arange(min_y, max_y, mesh_step_size))

    output = classifier.predict(np.c_[x_vals.ravel(), y_vals.ravel()])
    output = output.reshape(x_vals.shape)

    plt.figure()
    plt.pcolormesh(x_vals, y_vals, output, cmap=plt.cm.gray)
    plt.scatter(x[:, 0], x[:, 1], c=y, s=75, edgecolors='black', linewidth=1, cmap=plt.cm.Paired)

    plt.xlim(x_vals.min(), x_vals.max())
    plt.ylim(y_vals.min(), y_vals.max())

    plt.xticks((np.arange(int(x[:, 0].min() - 1), int(x[:, 0].max() + 1), 1.0)))
    plt.yticks((np.arange(int(x[:, 1].min() - 1), int(x[:, 1].max() + 1), 1.0)))

    if view_result != None:
        plt.xlabel(view_result)

    if heading != None:
        plt.title(heading)

    plt.tight_layout()
    plt.show()


def input_data_visualization(class_0, class_1):

    # Визуализация входных данных
    plt.figure()
    plt.scatter(class_0[:, 0], class_0[:, 1], s=75, facecolors='black',
                edgecolors='black', linewidth=1, marker='x')
    plt.scatter(class_1[:, 0], class_1[:, 1], s=75, facecolors='white',
                edgecolors='black', linewidth=1, marker='o')
    plt.title('Input data')
    plt.show()


def decision_tree_classifier():
   .
   .
   .
    input_data_visualization(class_0, class_1)
    visualize_classifier(classifier, X_train, y_train, 'Training dataset')
    visualize_classifier(classifier, X_test, y_test, 'Test dataset')

Next, I use a tkinter to build an interface with buttons. I upload the file and click on the button to calculate and output 3 graphs. But only the first is displayed, when you close it, nothing else happens. There is an idea to simply use a call to several functions at the same time on a button, but this is probably not entirely correct?

enter image description here

ps. just tried to separate all these functions and call them through one button - the result is the same, only one graph appears

kostya ivanov
  • 613
  • 5
  • 15

1 Answers1

0

Hey so you ask 2 questions basically. For your first question how to show multiple plots at the same time, you have to call plt.show() only once. See this post. Or check my example:

import matplotlib.pyplot as plt

Figure1 = plt.figure()
plt.scatter([1, 2, 3, 4, 5], [1, 2, 3, 4, 5])

Figure2 = plt.figure()
plt.scatter([1, 2, 3, 4, 5], [5, 4, 3, 2, 1])

plt.show()

For your second question how to implement it in tkinter it is a little more complex. You have to create tkinter compatible figure objects. You might check out the matplotlib manual. Here is a working example:

import tkinter as tk
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

def make_figures():
    Figure1 = Figure(figsize=(6, 6), dpi=100)
    subplot1 = Figure1.add_subplot(111)
    subplot1.scatter([1, 2, 3, 4, 5], [1, 2, 3, 4, 5])

    Figure2 = Figure(figsize=(6, 6), dpi=100)
    subplot2 = Figure2.add_subplot(111)
    subplot2.scatter([1, 2, 3, 4, 5], [5, 4, 3, 2, 1])

    return Figure1, Figure2


if __name__ == '__main__':
    root = tk.Tk()

    Fig1, Fig2 = make_figures()

    canvas1 = FigureCanvasTkAgg(Fig1, master=root)
    canvas1.get_tk_widget().grid(row=0, column=0, sticky='wens')
    canvas2 = FigureCanvasTkAgg(Fig2, master=root)
    canvas2.get_tk_widget().grid(row=0, column=1, sticky='wens')

    root.mainloop()
steTATO
  • 550
  • 4
  • 12