15

I recently installed Atom as an IDE on my laptop, for university. I installed Hydrogen as a convenient solution to show some plots on the go. But whenever I run Hydrogen, I get this error:

NameError                                 Traceback (most recent call last)
< ipython-input-1-1eb00ff78cf2>  in <module>

----> 1 plt.show()


NameError: name 'plt' is not defined

However Matplotlib is working properly when executed normally, and IPython seems to do as well. This is the code I'm trying to run test-wise:

%matplotlib ipympl

import matplotlib.pyplot as plt

a_x=[1,2,3,4,5,6]
a_y=[1,2,3,4,5,6]

plt.plot(a_x, a_y)
plt.show()

A video of the problem is available here.

enter image description here

  • Selected Run

enter image description here

  • Resulting error

enter image description here

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Chewhakka
  • 195
  • 1
  • 1
  • 6

2 Answers2

28

You have to import the library first. Add this at start of the code.-

from matplotlib import pyplot as plt
Akshay Sehgal
  • 18,741
  • 3
  • 21
  • 51
ishan weerakoon
  • 419
  • 1
  • 5
  • 6
  • 2
    _You have to import the library first._ is not correct, and the canonical way to import pyplot is `import matplotlib.pyplot as plt`, as shown in the [matplotlib User Guide](https://matplotlib.org/stable/tutorials/introductory/usage.html#sphx-glr-tutorials-introductory-usage-py) – Trenton McKinney Sep 06 '21 at 15:47
14
  • As shown in the OP, import matplotlib.pyplot as plt is present, but it didn't get executed.
    • You only executed the selected line (9) with plt.show(), not the whole file.
  • You can see the problem by carefully reading the traceback. Line 9 in the script is line 1 in the traceback: ----> 1 plt.show()
  • The solution is to run the whole file, not one line. Click Run All not Run.
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
user3508453
  • 878
  • 3
  • 14
  • 24