# Naming the Axes of my Graph on iPython Using Matplotlib.pyplot

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: But when I got ready to name the axes, the curve disappeared. I wrote this code:

plt.xlabel("Time")


And I got this: I also wrote this:

plt.ylabel("Speed")


And got this:

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)

• 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. Commented May 6, 2019 at 17:46
• Thanks. I will consider that option in future. However, I have edited the question and you may take a look Commented May 6, 2019 at 17:52
• 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. Commented May 6, 2019 at 17:53
• I am still trying to load the image Commented May 6, 2019 at 18:25
• 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. Commented May 6, 2019 at 19:06

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()


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

• 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 Commented May 6, 2019 at 18:00
• 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. Commented May 6, 2019 at 18:05
• I think it is iPython. Somewhere around the logo iP[y] is "Jupyter QtConsole" Commented May 6, 2019 at 18:24
• And where can I possibly look for "%matplotlib inline"? Commented May 6, 2019 at 18:27
• I just uploaded the image. I will appreciate it if you guys can just help me check out what might be wrong Commented May 6, 2019 at 19:08