Matplotlib in Jupyter results in variable “is not defined”

[亡魂溺海] 提交于 2021-01-27 08:31:59

问题


I'm having a strange issue using Jupyter to plot some simple data. There is a lot of nuance to my specific use-case, not the least of which is a Jupyter notebook connected to our cloud-based Spark cluster with a PySpark kernel.

I can't, for the life of me, figure out why this simple code will not run without error. In reality I have to have the code set up like this, because instead of "x" and "y" I'm dealing with a data frame sourced from a Hive query - using the %sql magic and manipulating it before I get ready to plot it.

Here's a re-creation of the code within my Jupyter notebook - wherein I'm trying to illustrate the separation of code cells. I've tried every combination of ordering the cells and whatnot, I can't fathom why it tells me the "x" variable is not defined.

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5, 6, 7, 8]
y = [20, 21, 20.5, 20.81, 21.0, 21.48, 22.0, 21.89]

%matplotlib inline
xyplt = plt.plot(x, y, linestyle='dashed', marker='o', color='red')

NameError                                 Traceback (most recent call last)
<ipython-input-34-747daa8afe0d> in <module>()
      1 get_ipython().magic(u'pylab inline')
----> 2 xyplt = plt.plot(x, y, linestyle='dashed', marker='o', color='red')

NameError: name 'x' is not defined

来源:https://stackoverflow.com/questions/37867005/matplotlib-in-jupyter-results-in-variable-is-not-defined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!