Plotting with matplotlib

matplotlib

?le:///home/kaustubh/Temp/matplotlib.html

Plotting with matplotlib

Yogesh Wadadekar

NCRA-TIFR, Pune

thanks to Varun Bhalerao for several slides used here.

Plotting with Python

ppgplot

sm

gnuplot

R

Matlab

Mathematica

IDL

Octave

Matplotlib - the standard plotting library for

Python

Matplotlib has very powerful plotting facilities. So before we start with the details, let us take a look at

() . It is a showcase of the capabilities of

Matplotlib: starting from very simple graphs, to far more complicated ones.

Let's get started!

Matplotlib commands are nearly identical to Matlab commands. So, if you know Matlab, you won't have to

learn much. For basic plotting, it is easiest to use pylab. You can start it from the command line by issuing

the command,

ipython --pylab

Or, if you already have an iPython session open, you can give the command,

%pylab or %pylab inline

Remember in a real program, you would never import a module namespace into a global namespace.

1 of 14

09/04/15 08:46

matplotlib

?le:///home/kaustubh/Temp/matplotlib.html

In [21]: %pylab inline

Populating the interactive namespace from numpy and matplotlib

WARNING: pylab import has clobbered these variables: ['f']

`%pylab --no-import-all` prevents importing * from pylab and numpy

In [22]: # Generate some test data

x = np.arange(0, 6.28, 0.01)

y = np.sin(x)

Now we plot y versus x

In [23]: plot(x, y)

Out[23]: []

Adding Labels

That was simple: we have plotted y as a function of x. Now let us add labels:

2 of 14

09/04/15 08:46

matplotlib

?le:///home/kaustubh/Temp/matplotlib.html

In [24]: plot(x, y)

plt.xlabel("theta (angle in radians)")

plt.ylabel('sin(theta)')

plt.title("This is my first plot")

Out[24]:

Fancier labels:

In [25]: plot(x, y)

xlab = plt.xlabel(r"$\theta$ (angle in radians)") # note the r before q

uotes

ylab = plt.ylabel(r'sin($\theta$)')

thetitle= plt.title("This is is a nicer plot")

thetitle.set_fontweight('bold')

ylab.set_color('blue')

xlab.set_color('red')

xlab.set_style('italic')

More examples at [ (

/examples/pylab_examples/fonts_demo.html)]

3 of 14

09/04/15 08:46

matplotlib

?le:///home/kaustubh/Temp/matplotlib.html

Axis Range and scale

In [26]: another_x = x+1

plot(another_x,another_x**4)

plt.title('X**4 versus X')

Out[26]:

In [27]: # Let's make it look a bit better

plot(another_x,another_x**4)

plt.title(r'X$^4$ versus X') # The $ signs are for latex inputs

plt.xlim(min(another_x), max(another_x))

plt.yscale('log')

plt.show()

Overplot

4 of 14

09/04/15 08:46

matplotlib

?le:///home/kaustubh/Temp/matplotlib.html

In [28]: plot(x, y)

plot(x, np.cos(x))

Out[28]: []

Styles, symbols and colours

In [29]: plot(x, y)

x_spaced = np.arange(0, 2.0*np.pi, 0.3)

plot(x_spaced, np.sin(x_spaced), marker='o', color='red', linestyle='No

ne')

plot(x_spaced, np.cos(x_spaced), 'b+')

Out[29]: []

5 of 14

09/04/15 08:46

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download