python – 使用matplotlib示例时Django出错

python – 使用matplotlib示例时Django出错,第1张

概述我正在测试Django和matplotlib的几个案例,例如 this question或 in french. 每次,它都可以在我的Mac上运行,但不在我的服务器上,我收到以下错误: Internal Server Error: /mj/charts/mplimage.pngTraceback (most recent call last): File "/usr/local/lib/py 我正在测试Django和matplotlib的几个案例,例如 this question或 in french.

每次,它都可以在我的Mac上运行,但不在我的服务器上,我收到以下错误:

Internal Server Error: /mj/charts/mplimage.pngTraceback (most recent call last):  file "/usr/local/lib/python3.6/dist-packages/django/core/handlers/exception.py",line 35,in inner    response = get_response(request)  file "/usr/local/lib/python3.6/dist-packages/django/core/handlers/base.py",line 128,in _get_response    response = self.process_exception_by_mIDdleware(e,request)  file "/usr/local/lib/python3.6/dist-packages/django/core/handlers/base.py",line 126,in _get_response    response = wrapped_callback(request,*callback_args,**callback_kwargs)  file "/root/src/jm/majority_judgment/vIEws.py",line 39,in mplimage    canvas.print_png(response)  file "/usr/local/lib/python3.6/dist-packages/matplotlib/backends/backend_agg.py",line 526,in print_png    with cbook.open_file_cm(filename_or_obj,"wb") as fh:  file "/usr/lib/python3.6/contextlib.py",line 81,in __enter__    return next(self.gen)  file "/usr/local/lib/python3.6/dist-packages/matplotlib/cbook/__init__.py",line 624,in open_file_cm    fh,opened = to_filehandle(path_or_file,mode,True,enCoding)  file "/usr/local/lib/python3.6/dist-packages/matplotlib/cbook/__init__.py",line 615,in to_filehandle    raise ValueError('fname must be a Pathlike or file handle')ValueError: fname must be a Pathlike or file handle[28/Mar/2018 19:09:11] "GET /mj/charts/mplimage.png http/1.1" 500 82804

这是一个最小的片段:

def mplimage(request):    f = matplotlib.figure.figure()    canvas = figureCanvasAgg(f)    response = httpResponse(content_type='image/png')    canvas.print_png(response)    plt.close(f)    return response

我试图更新matplotlib,django等,但它没有做任何事……

解决方法 目前,matplotlib的编写函数 require the seek ducktype在文件中使用响应.您可以写入缓冲区,如下所示:
import iodef mplimage(request):    f = matplotlib.figure.figure()    # Code that sets up figure goes here; in the question,that's ...    figureCanvasAgg(f)    buf = io.BytesIO()    plt.savefig(buf,format='png')    plt.close(f)    response = httpResponse(buf.getvalue(),content_type='image/png')    return response
总结

以上是内存溢出为你收集整理的python – 使用matplotlib示例时Django出错全部内容,希望文章能够帮你解决python – 使用matplotlib示例时Django出错所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1207205.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存