I am new to data science. I use Anaconda on windows 7. I plotted a sine curve by doing the following on iPython:

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 10, 1000)
y = np.sin(x)
plt.plot(x, y)

And I got this: enter image description here But when I got ready to name the axes, the curve disappeared. I wrote this code:


And I got this: enter image description here I also wrote this:


And got this: enter image description here

So my question is, how can I plot a curve with labelled axes? (In other words, I will like the x-axis to be Time, the y-axis to be Speed and the curve intact)

  • $\begingroup$ Unfortunately your links do not work (for me at least). You can load images directly into your question in the GUI, there is an image icon. You could edit your question to add them if my answer doesn't already give you what you need. $\endgroup$
    – n1k31t4
    May 6, 2019 at 17:46
  • $\begingroup$ Thanks. I will consider that option in future. However, I have edited the question and you may take a look $\endgroup$
    – Mr Prof
    May 6, 2019 at 17:52
  • $\begingroup$ I have formatted the question to make it more readale and to match the forum's expected quality. Please do not remove the code formatting. $\endgroup$
    – n1k31t4
    May 6, 2019 at 17:53
  • $\begingroup$ I am still trying to load the image $\endgroup$
    – Mr Prof
    May 6, 2019 at 18:25
  • $\begingroup$ I just uploaded the image. I will appreciate it if you guys can just take a look at what I have been trying to say in words and maybe suggest what I can do. $\endgroup$
    – Mr Prof
    May 6, 2019 at 19:06

1 Answer 1


When you use matplotlib's plot function, it holds an object behind the scenes for you. You can change this object with more calls to plt and then only once everything has been done should you plt.show() the graph.

Here is a simply example that does what you want:

In [1]: import matplotlib.pyplot as plt
In [2]: import numpy as np                                                           

In [3]: x = np.linspace(0, 10, 100)                                                  
In [4]: y = x ** 2                                   

The following lines change the plot object, adding the axes labels - but we don't show it until all are complete...

In [5]: plt.plot(x, y)                                                               
Out[5]: [<matplotlib.lines.Line2D at 0x7f3c896bb9b0>]

In [6]: plt.xlabel("Time")                                                           
Out[6]: Text(0.5, 0, 'Time')

In [7]: plt.ylabel("Speed")  
Out[7]: Text(0, 0.5, 'Speed')

Now we are done, so show it:

In [8]: plt.show()                                                                  

enter image description here

Have a look here for a more thorough demo, which also shows the object explicitly and lets you better understand what is going on.

  • $\begingroup$ The lecture video I am watching here does the same i.e. it kind of keep the whole thing behind the scene and when the guy in the video enters plt.show(), all his work will be displayed. However, whenever I enter plt.plot(x, y) or plt.xlabel("Time"), my graph is generated immediately $\endgroup$
    – Mr Prof
    May 6, 2019 at 18:00
  • $\begingroup$ Are you working in a Jupyter Notebook? In iPython it will not show immediately by default. If you have this line anywhere: %matplotlib inline - you should remove it too. It forces plots to be shown immediately. $\endgroup$
    – n1k31t4
    May 6, 2019 at 18:05
  • $\begingroup$ I think it is iPython. Somewhere around the logo iP[y] is "Jupyter QtConsole" $\endgroup$
    – Mr Prof
    May 6, 2019 at 18:24
  • $\begingroup$ And where can I possibly look for "%matplotlib inline"? $\endgroup$
    – Mr Prof
    May 6, 2019 at 18:27
  • $\begingroup$ I just uploaded the image. I will appreciate it if you guys can just help me check out what might be wrong $\endgroup$
    – Mr Prof
    May 6, 2019 at 19:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.