Matplotlib无法在OSX上使用Python 2.7和Django [英] Matplotlib not working with Python 2.7 and Django on OSX

查看:62
本文介绍了Matplotlib无法在OSX上使用Python 2.7和Django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 matplotlib mpld3 在我的 Django 报告应用程序上生成一些html图.

I am trying to use matplotlib and mpld3 to produce some html plots on my Django report app.

基本上,我有一个用于以下情节的控制器:

Basically I have a controller for the plot that is the following:

from django.shortcuts import render
import mpld3
from matplotlib.pyplot import figure, title, bar

def cpfLogin(request):    
     mpl_figure = figure(1)
     xvalues = (1,2,3,4,5)   
     yvalues = (1,2,3,4,5)

     width = 0.5  # the width of the bars    
     title(u'Custom Bar Chart')
     bar(xvalues, yvalues, width)
     fig_html = mpld3.fig_to_html(mpl_figure)

     context = {
         'figure': fig_html,
     }

     return render(request, 'reports/CPFReport.html', context)

reports/CPFReport.html的代码为:

The code for reports/CPFReport.html is:

 {% load i18n %}

 {% block extrahead %}
     <style type="text/css">
         .chart_title {
             font-weight: bold;
             font-size: 14px;
         }
     </style>
  {% endblock %}

 {% block content %}
     <div id="content-main">
         <div class="chart_title">
             {% trans "Custom Bar Chart" %}
         </div>
         {{ figure|safe }}
     </div>
 {% endblock %}

代码已正确执行,并且情节正确显示,但是几秒钟后,应用程序终止并出现以下错误:

The code is executed right and the plot is displayed correctly but after a couple of seconds the app terminates with the following error:

断言失败:(NSViewIsCurrentlyBuildingLayerTreeForDisplay()!=currentBuildingLayerTree),函数NSViewSetCurrentlyBuildingLayerTreeForDisplay,文件/BuildRoot/Library/Caches/com.apple.xbs/Sources/AppKit/AppKit-1561.20.106/AppKit.subproj/NSView.m,行14480.

Assertion failed: (NSViewIsCurrentlyBuildingLayerTreeForDisplay() != currentlyBuildingLayerTree), function NSViewSetCurrentlyBuildingLayerTreeForDisplay, file /BuildRoot/Library/Caches/com.apple.xbs/Sources/AppKit/AppKit-1561.20.106/AppKit.subproj/NSView.m, line 14480.

我发现,如果我注释所有代码,则当调用任何 matplotlib 库时,都会引发此异常.

I found out that if I comment all the code this exception is thrown when any of the matplotlib libraries are called.

有人针对此问题有解决方法或解决方案吗?

Does anyone has a workaround or solution for this problem?

推荐答案

就我而言,我必须避免导入:

In my case I had to avoid importing :

import matplotlib.pyplot as plt

fig,ax = plt.subplots(figsize=(8,9))
l      = plt.plot(x,s, 'y-', label="line")

并替换为:

from matplotlib.figure import Figure

fig = Figure()
ax  = fig.add_subplot(111))
l   = ax.plot(x,s, 'y-', label="line")

这篇关于Matplotlib无法在OSX上使用Python 2.7和Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆