0

I am trying to draw a line on a plit, but the plot is not even showing. I have checked the values of xPoints and yPoints and they exist.
What is the cause?

import matplotlib.pyplot as plt
import numpy as np

def calculateFuncFor(x):
    ePower = np.e**np.exp(x)
    result = 1 - ePower
    
    return "{:.4f}".format(result) #format the result


xPoints = np.linspace(0,1) #outputs 50 points between 0 and 1 
yPoints = np.zeros(len(xPoints)) #fill a list with 50 zeros


for i in range(len(xPoints)):
    yPoints[i] = calculateFuncFor(xPoints[i])

plt.plot(xPoints, yPoints,'ro')
plt.show()
bibscy
  • 2,132
  • 1
  • 19
  • 61
  • "the plot is not even showing" - well, you should `plt.show()` it :D – ForceBru Nov 01 '20 at 14:04
  • I've just added, ```plt.show()``` after ```plt.plot(xPoints, yPoints,'ro')``` and the plot is still not showing. Could you run my code in your environment? – bibscy Nov 01 '20 at 14:08
  • Well, after I fixed `zeros(len(xPoints))` to be `np.zeros(len(xPoints))`, it worked fine on my machine. Does the script exit immediately or does it sit around waiting for something? – ForceBru Nov 01 '20 at 14:10
  • It exits immediately. I tried with ```np.zeros(len(xPoints))``` and still same behaviour. – bibscy Nov 01 '20 at 14:14
  • Like @ForceBru, I just fixed `zeros(len(xPoints))` and it works fine – vinzee Nov 01 '20 at 14:14
  • How do you launch your script? `python myscript.py`? Through the IDE? – vinzee Nov 01 '20 at 14:15
  • I launch it in Jupyter Notebook. @vinzee – bibscy Nov 01 '20 at 14:15
  • @vinzee I have tried to run it from Sublime with ```python myscript.py``` and it works. I guess I can't rely on Jupyter Notepbook. – bibscy Nov 01 '20 at 14:18
  • Does this answer your question? [How to make IPython notebook matplotlib plot inline](https://stackoverflow.com/questions/19410042/how-to-make-ipython-notebook-matplotlib-plot-inline) – vinzee Nov 01 '20 at 14:33

1 Answers1

1

Try putting in the first cell of your Jupyter Notebook the following:

%matplotlib inline

% denotes the IPython magic built-in commands

vinzee
  • 10,965
  • 9
  • 34
  • 51
  • To make it a complete answer, could you write in your answer what does ```%``` operator do in front of the module ```matplotlib inline```. Many thanks – bibscy Nov 01 '20 at 14:22