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

报告相同问题?

悬赏问题

  • ¥50 关于#华为#的问题:华为HMS账号登录
  • ¥20 Html备忘录页面制作
  • ¥15 黄永刚的晶体塑性子程序中输入的材料参数里的晶体取向参数是什么形式的?
  • ¥20 数学建模来解决我这个问题
  • ¥15 计算机网络ip分片偏移量计算头部是-20还是-40呀
  • ¥15 stc15f2k60s2单片机关于流水灯,时钟,定时器,矩阵键盘等方面的综合问题
  • ¥15 YOLOv8已有一个初步的检测模型,想利用这个模型对新的图片进行自动标注,生成labellmg可以识别的数据,再手动修改。如何操作?
  • ¥30 NIRfast软件使用指导
  • ¥20 matlab仿真问题,求功率谱密度
  • ¥15 求micropython modbus-RTU 从机的代码或库?