避免网络上多个Matplotlib数字之间的干扰 [英] Avoid interference between multiple Matplotlib figures on the web

查看:201
本文介绍了避免网络上多个Matplotlib数字之间的干扰的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个Matplotlib数字的页面。这些数字通过Ajax插入/操作,如下所示(使用时间戳,以便通过浏览器重新加载):

  ; img src =/ charts / somedata.png?time ='+ str(time.time())+'/> 

然后将URL与一个视图匹配(我应该提到我使用Django):

 (r'^图表/(?P<图表[A-Za-z _] +)png $' 


$动态加载适当的模块
chart_module = import_module('functions.charts。'+ chart)
return chart_module.show_chart(request)

图表模块(somedata.py)如下所示:

 <_ c $ c> from __future__ import division 
from django.http import HttpResponseRedirect,HttpResponse

def show_chart (请求):
尝试:
#启动图
从matplotlib.pyplot导入图
从matplotlib.backends.backend_agg import图CanCanvasAgg作为FigureCanvas
fig1 = figure(figsize =(5.33,4),facecolor ='white')
ax = fig1.add_axes([0.02,0.02,0.98,0.98],aspect ='equal')

#一些绘图为

画布=图卡(fig1)
响应= HttpResponse(content_type ='image / png')
canvas.print_png(响应)
fig1.clear()
返回响应

除了:
返回HttpResponseRedirect(/)

使用matplotlib版本0.99。我有时会遇到问题,一个图表的一部分是在另一个图表上绘制的。有时,这是在重新加载后被修复的。我想出的解决方案是在settings.py中放置以下内容:

 #强制matplotlib不要使用任何Xwindows后端。 
#这是必要的,然后导入pyplot或其他任何东西从matplotlib
import matplotlib
matplotlib.use('Agg')

但是,在更新到matplotlib 1.0.1版之后,问题已经回到了。这些数字相互干扰,或者一部分图形根本没有画出来?任何关于问题可能在哪里的想法?



编辑:一个两个地块的例子。顶部的线条属于另一个数字,它们在哪里丢失。我还应该提到,这不是在本地运行开发服务器的问题!

解决方案

将matplotlib导入行从settings.py移动到每个图表模块的顶部以及使用savefig(由jozzas提供 - 谢谢)似乎已经解决了干扰问题: p>

这是通过从matplotlib.backends.backend_agg导入中删除:

  FigureCanvasAgg as FigureCanvas 
canvas = FigureCanvas(fig1)
canvas.print_png(response)

然后我添加了:

  fig1.savefig(response,format ='png')

更新:
主要问题是pyplot函数没有正确附加到图中实例,在此帖中讨论和解决


I have a page with multiple Matplotlib figures embedded. The figures get inserted/manipulated via Ajax as follows (with a time stamp in order to get reloaded by the browser):

<img src="/charts/somedata.png?time=' + str(time.time()) + '" />

The urls are then matched to a view like (I should mention that I use Django):

(r'^charts/(?P<chart>[A-Za-z_]+).png$', 'views.charts'),

def charts(request, chart):
    # Dynamically loading the appropriate module
    chart_module = import_module('functions.charts.' + chart)
    return chart_module.show_chart(request)

The chart modules (somedata.py) look like this:

from __future__ import division
from django.http import HttpResponseRedirect, HttpResponse

def show_chart(request):
    try:
        # Initiating figure
        from matplotlib.pyplot import figure
        from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
        fig1 = figure(figsize=(5.33,4), facecolor = 'white')
        ax = fig1.add_axes([0.02,0.02,0.98,0.98], aspect='equal')

        # Some plotting to ax

        canvas=FigureCanvas(fig1)
        response=HttpResponse(content_type='image/png')
        canvas.print_png(response)
        fig1.clear()
        return response

    except:
        return HttpResponseRedirect("/")

With matplotlib version 0.99.? I sometimes got the problem that part of one chart was drawn in another chart on the same page. Sometimes, that was fixed after a reload. The solution I came up with was to put the following in settings.py:

# Force matplotlib to NOT use any Xwindows backend.
# This is necessary before importing pyplot or anything else from matplotlib
import matplotlib
matplotlib.use('Agg')

However, after updating to matplotlib version 1.0.1, the problem is back. The figures interfere with each other, or parts of a figure are not drawn at all? Any ideas about where the problem could be?

EDIT: Example of two plots in one. The lines on top belong to another figure, where they are missing. I should also mention that this is never an issue running the development server locally!

解决方案

Moving the matplotlib import lines from settings.py to the top of each chart module as well as using savefig (as proposed by jozzas - thanks) seems to have solved the interference problem:

This was done by removing:

from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
canvas=FigureCanvas(fig1)
canvas.print_png(response)

Then I added instead:

fig1.savefig(response, format='png')

UPDATE: The main issue was that the pyplot function did not correctly attach to the figure instance, which was discussed and solved in this post.

这篇关于避免网络上多个Matplotlib数字之间的干扰的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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