weixin_33720956 2017-08-21 05:15 采纳率: 0%
浏览 400

将Matplotlib与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.

  • 写回答

1条回答 默认 最新

  • H_MZ 2017-08-21 08:39
    关注

    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)
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用VH6501干扰RTR位,CANoe上显示的错误帧不足32个就进入bus off快慢恢复,为什么?
  • ¥15 大智慧怎么编写一个选股程序
  • ¥100 python 调用 cgps 命令获取 实时位置信息
  • ¥15 两台交换机分别是trunk接口和access接口为何无法通信,通信过程是如何?
  • ¥15 C语言使用vscode编码错误
  • ¥15 用KSV5转成本时,如何不生成那笔中间凭证
  • ¥20 ensp怎么配置让PC1和PC2通讯上
  • ¥50 有没有适合匹配类似图中的运动规律的图像处理算法
  • ¥15 dnat基础问题,本机发出,别人返回的包,不能命中
  • ¥15 请各位帮我看看是哪里出了问题