0

When I run this code in Spyder or Jupyter Notebook, it runs once only. Afterward, it shows a blank graph without any plotting inside. The code will plot again only if I restart the Windows 10.

import numpy as np
import matplotlib.pyplot as pl

x = np.linspace(0,10,1)
y = np.sin(x)

pl.plot(x,y)

enter image description here Any help is appreciated.

KawaiKx
  • 8,125
  • 15
  • 64
  • 91
  • Can you clarify when it shows a blank graph? Does this mean even after running this code block, it does not plot anything? Also, if you can show a screenshot of your issue, that would be helpful as well. You might also consider the answer here https://stackoverflow.com/q/19410042/ – Eric Leung Oct 18 '19 at 07:10
  • You really need to restart *Windows* to make it work again? Closing and re-opening the notebook doesn't work, or restarting Jupyter? – Karl Knechtel Oct 18 '19 at 07:25
  • no. closing notebook was no help.. tried it. – KawaiKx Oct 18 '19 at 07:27

1 Answers1

1

Jupyter and matplotlib are working as you have programmed.

The issue is how you're using np.linspace().

Currently, it is

x = np.linspace(0,10,1)

If you were to print this out, this is what you would get

array([0.])

So there is only one x value and it is plotted as a single dot.

To plot something, you'll need to change the third argument to something else. That is the parameter for num, of the number of samples to generate. So having a 1 there produces one point.

So try something like this

x = np.linspace(0, 10, 100)
y = np.sin(x)

pl.plot(x,y)

enter image description here

Documentation for np.linspace() https://docs.scipy.org/doc/numpy/reference/generated/numpy.linspace.html

Eric Leung
  • 1,650
  • 10
  • 21