0

I'm a complete newbie to python (this is my first post) but I have some experience programming in R. I've been going through the Crash Course in Python for Scientists tutorial.

I got as far as the matplotlib bit-I havent been able to go beyond this as the plot function is not recognized despite importing matplotlib. I'm using the idle in python 2.7.3 (I'm using a mac).

Here's what I'm trying to do:

>>> import matplotlib
>>> plot(range(20))

Here's the error message: # error message

Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
plot(range(20))
NameError: name 'plot' is not defined

Can anyone help out at all? I know there are loads of other editors I could use but I prefer something similar to the R console where I can just type directly into the command line. I still haven't figured out a short cut for running a code directly from the idle editor-my f5 key is for something else and it doesn't run when I type it.

3 Answers 3

3

plot is a function of matplotlib.pyplot, so:

import matplotlib.pyplot as plt

plt.plot(range(20))

EDIT:

To see the plot, you will normally need to call

plt.show()

to display the figure, or

plt.savefig('figname.png')

to save the figure to a file after calling plt.plot().

As @JRichardSnape pointed out in the comments, import matplotlib.pyplot as plt is now a widely used convention, and is often implied around this site. The beginners guide has some useful information on this.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, this seems to be working. it gave me [<matplotlib.lines.Line2D object at 0x1069611d0>] and then its trying to load the output which is taking ages. cheers.
you might need to call plt.show() after plt.plot to view the plot
Just a thought - don't want to steal your thunder by adding another answer - it's worth highlighting for a new user that you (usually) need to follow this with plt.show() to see the actual plot. It might also be worth highlighting that as plt is a widely used convention and often assumed and pointing to the beginner's guide
Oops - just noticed that a load of comments hadn't refreshed in my browser for some reason - sorry.
@JRichardSnape no worries, its all helpful info
3

You have to import the pyplot module of matplotlib and call the plot function.

import matplotlib.pyplot as plt

This plt is an alias for matplotlib.pyplot, so when plotting your range of values, just use the defined alias:

plt.plot(range(20))

I usually need to show my plots to colleages, so I save them in a figure instead of showing directly:

plt.savefig('output_image.png', dpi=300)

But if you need only a brief look in the output, just show your plot:

plt.show()

Comments

2

I prefer something similar to the R console where I can just type directly into the command line.

matplotlib is a plotting library, far from being the environment you seem to be seeking

If you want an environment similar to R, you probably will want to use pylab along with matplotlib

from pylab import *
plot(...)
show()

e.g. http://matplotlib.org/examples/pylab_examples/simple_plot.html

Also consider using ipython instead of the stock IDLE/Python prompt, highly recommended for data science, http://matplotlib.org/users/shell.html#ipython-to-the-rescue

That gives a much better environment, notice the %pylab command to trigger that mode, or try ipython --pylab to launch it

If you're used to doing literate programming in R, I recommend learning IPython notebooks, http://ipython.org/notebook.html

4 Comments

but this isn't recommended any more: matplotlib.org/faq/…
That's the closest you can get to a numerical computing environment, not same thing as making sound programs
fair enough, I guess it just comes down to personal preference at some point
@John added a note on ipython, notebooks etc to bridge you from R to a Python centric data science environment

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.