python – 使用matplotlib示例时Django出错
发布时间:2020-12-04 22:13:31 所属栏目:Python 来源:互联网
导读:我正在测试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.png Traceback (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 theseek ducktype在文件中使用响应.您可以写入缓冲区,如下所示:
import io def 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 (编辑:4S站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 在Python中存储三角形/六边形网格的最佳方法
- python – 编写一个通用的getattr()并根据attr名称填充方法
- 使用Python将二进制数据写入套接字(或文件)
- python – Flask:后台线程看到一个非空队列为空
- 如何更好地光栅化绘图而不会模糊matplotlib中的标签?
- python – tkinter中的标签宽度
- python – 具有超类类名称的子类变量
- python – ImportError:Elastic Beanstalk中没有名为djang
- Django 1.10中有哪些django.core.context_processors.reque
- python – 带有postgres的游标,存储数据的位置以及对DB的调