如何使matplotlib在AWS EMR Jupyter笔记本中工作? [英] How do I make matplotlib work in AWS EMR Jupyter notebook?

查看:177
本文介绍了如何使matplotlib在AWS EMR Jupyter笔记本中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与这个问题非常接近,但是我添加了一些针对我的问题的详细信息:

This is very close to this question, but I have added a few details specific to my question:

使用AWS-EMR jupyter笔记本进行Matplotlib绘图

我想找到一种在Jupyter笔记本中使用matplotlib的方法.这是错误的代码片段,非常简单:

I would like to find a way to use matplotlib inside my Jupyter notebook. Here is the code-snippet in error, it's fairly simple:

笔记本

import matplotlib
matplotlib.use("agg")
import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
plt.show()

之所以选择此代码段,是因为该行尝试使用TKinter(AW​​S EMR集群中未安装)时,仅此行失败:

I chose this snippet because this line alone fails as it tries to use TKinter (which is not installed on an AWS EMR cluster):

import matplotlib.pyplot as plt

当我运行完整的笔记本代码片段时,结果是没有运行时错误,但也没有任何反应(未显示任何图形.)我可以通过以下两种方式之一来实现这一目的:

When I run the full notebook snippet, the result is no runtime error but also nothing happens (no graph is shown.) My understanding on one way this can work is by adding either of the following snips:

pyspark魔术符号

%matplotlib inline

结果

unknown magic command 'matplotlib'
UnknownMagic: unknown magic command 'matplotlib'

IPython显式魔术调用

from IPython import get_ipython
get_ipython().run_line_magic('matplotlib', 'inline')

结果

'NoneType' object has no attribute 'run_line_magic'
Traceback (most recent call last):
AttributeError: 'NoneType' object has no attribute 'run_line_magic'

到我的笔记本,该笔记本调用一个火花魔术命令,该命令可内联matplotlib图(至少是我的解释.)我在使用引导操作后尝试了这两种方法:

to my notebook which invokes a spark magic command which inlines matplotlib plots (at least that's my interpretation.) I have tried both of these after using a bootstrap action:

EMR引导程序

sudo pip install matplotlib
sudo pip install ipython

即使添加了这些,我仍然会得到一个错误,那就是matplotlib没有任何魔力.所以我的问题肯定是:

Even with these added, I still get an error that there is no magic for matplotlib. So my question is definitely:

问题

如何使matplotlib在AWS EMR Jupyter笔记本中工作?

How do I make matplotlib work in an AWS EMR Jupyter notebook?

(或者如何在AWS EMR Jupyter笔记本中查看图形并绘制图像?)

(Or how do I view graphs and plot images in AWS EMR Jupyter notebook?)

推荐答案

正如您提到的,matplotlib未安装在EMR群集上,因此会发生此类错误:

As you mentioned, matplotlib is not installed on the EMR cluster, therefore such error will occur:

但是,它实际上在托管的Jupyter笔记本实例(docker容器)中可用.使用%%local魔术将使您可以在本地运行单元:

However, it is actually available in the managed Jupyter notebook instance (the docker container). Using the %%local magic will allow you to run the cell locally:

这篇关于如何使matplotlib在AWS EMR Jupyter笔记本中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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