How to plot a graph using matplotlib in Eclipse

Post date: Aug 30, 2012 2:05:18 AM

Just make sure that you

  1. import the matplotlib package. The package contains lots of plotting tools.
  2. import pylab package and use pylab.show() to show the plotted figure.

Here is the code snippet that works in both Eclipse and Python IDLE:

import numpy as np
import matplotlib.pyplot as plt
import pylab
# Come up with x and y
x = np.arange(0, 5, 0.1)
y = np.sin(x)
# Just print x and y for fun
print x
print y
# plot the x and y and you are supposed to see a sine curve
plt.plot(x, y)
# without the line below, the figure won't show
pylab.show()

The code above should give you something like this

Another code you might want to try is this:

from numpy import *
a = arange(15).reshape(3, 5)
print "The matrix a is \n"+str(a)
print "a[1,2] = " +str(a[1,2])
print "the matrix dimension is " + str(a.shape)
from matplotlib.pyplot import *
from pylab import *
matshow(a)
show()

and you are going to see this image: