Ivan
Ivan

Reputation: 703

Using Matplotlib with Django

I have built a simple web application using Django that reads a csv file and allows users to plot graphs by selecting X & Y attributes.

The frontend makes use of AJAX calls to call backend methods to plot graphs using Python's Matplotlib. The issue arises when the plots are called asynchronously, causing a race condition: Different charts get plotted to the same figure.

In attempts to overcome this problem, I assign random "id" to each user so that I can call matplotlib to get a figure number -- So each user plots on different figures.

import matplotlib
import pandas as pd
matplotlib.use('agg')

#cm is an array containing 2 confusion matrices generated from http://scikit-learn.org/stable/modules/generated/sklearn.metrics.confusion_matrix.html
def plot_confusion_matrix(user_id, cm, classes, path,
                          normalize=False,
                          title='Confusion Matrix',
                          cmap=plt.cm.Blues):
    id = user_id + random.randint(1, 10000)
    fig = plt.figure(id)
    axis1 = fig.add_subplot(121)
    title1 = title + " (train)"
    title2 =title + " (test)"
    def plot_cm(cm, title):
        plt.imshow(cm, interpolation='nearest', cmap=cmap)
        plt.title(title)
        #plt.colorbar()
        tick_marks = np.arange(len(classes))
        plt.xticks(tick_marks, classes, rotation=45)
        plt.yticks(tick_marks, classes)
        thresh = cm.max() / 2.
        for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
            plt.text(j, i, cm[i, j],
                     horizontalalignment="center",
                     color="white" if cm[i, j] > thresh else "black")

        plt.tight_layout()
        plt.ylabel('True label')
        plt.xlabel('Predicted label')
    plot_cm(cm=cm[0], title=title1)
    axis2 = fig.add_subplot(122)
    plot_cm(cm=cm[1], title=title2)
    plt.tight_layout()
    fig.savefig(path)
    plt.close(id)

However, this does not solve the problem -- When a user plots 3 graphs at one time the graphs overlap on each other.

Upvotes: 1

Views: 725

Answers (1)

Radek Hofman
Radek Hofman

Reputation: 527

ok, try to plot directly into the axis you create like this:

def plot_confusion_matrix(user_id, cm, classes, path,
                          normalize=False,
                          title='Confusion Matrix',
                          cmap=plt.cm.Blues):
    id = user_id + random.randint(1, 10000)
    fig = plt.figure(id)
    axis1 = fig.add_subplot(121)
    title1 = title + " (train)"
    title2 =title + " (test)"
    def plot_cm(cm, title, ax):
        ax.imshow(cm, interpolation='nearest', cmap=cmap)
        ax.set_title(title)
        #plt.colorbar()
        tick_marks = np.arange(len(classes))
        ax.set_xticks(tick_marks, classes, rotation=45)
        ax.set_yticks(tick_marks, classes)
        thresh = cm.max() / 2.
        for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
            ax.text(j, i, cm[i, j],
                     horizontalalignment="center",
                     color="white" if cm[i, j] > thresh else "black")

        plt.tight_layout()
        ax.set_ylabel('True label')
        ax.set_xlabel('Predicted label')
    plot_cm(cm=cm[0], title=title1, axis1)
    axis2 = fig.add_subplot(122)
    plot_cm(cm=cm[1], title=title2, axis2)
    plt.tight_layout()
    plt.savefig(path)
    plt.close(id)

Upvotes: 1

Related Questions