Search code examples
pythonpython-3.xmesa

Why can't this Jupyter Code run in Python?


I am trying to recreate the results of the following Jupyter Notebook by copying the code and running it in PyCharm. When I run the code, I am not able to import numpy or matplotlib because they are showing up in grey, however, the other imports seem to work. I am trying to only recreate up until the 8th line in the Jupyter notebook but when I run that code I do not get any results and my code only finishes with "Process finished with exit code 0".

So I am wondering if copying and pasting from a Jupyter notebook might not be compatible with PyCharm or if someone might be able to provide insight as to why I cannot recreate the image then that would be helpful.

Here is a link to an image of the code I have, it is simply just a copy and paste from the Jupyter Notebook:


Solution

  • I think the code in the notebook works because it calls an iPython magic here:

    import matplotlib.pyplot as plt
    %matplotlib inline  # this is the iPython magic
    

    So, as per this answer, this magic function will show the plots right away, while with a regular Python you'll have to call show:

    import matplotlib.pyplot as plt
    
    plt.plot(x, y)  # this line doesn't show anything, it only prepares the plot
    plt.grid(True)  # modify the plot
    plt.show()  # actually show the plot
    

    Try calling plt.show() after results.plot().