Main.SineCurve History

Hide minor edits - Show changes to markup

April 29, 2010, at 09:21 PM MST by 71.196.160.210 -
Added lines 1-55:

(:source lang=python:)

""" Our first matplotlib plot """

  1. matplotlib and pyplot
  2. pyplot is a module in matplotlib that provides a Matlab-like interface
  3. to most of the functionality of matplotlib. for most purposes it is all
  4. you need.
  5. first we import pyplot and numpy

import matplotlib.pyplot as plt import numpy as np

  1. create the data

x = np.arange(0, 10, 0.2) y = np.sin(x)

  1. plot it

plt.plot(x, y)

  1. you may sometimes need to tell matplotlib to actually show the figure

plt.show()

  1. to save the figure:

plt.savefig('sine.pdf')

  1. there is a more wordy object oriented way of achieving the same thing:

import matplotlib.pyplot as plt import numpy as np

x = np.arange(0, 10, 0.2) y = np.sin(x) fig = plt.figure() ax = fig.add_subplot(111) ax.plot(x, y) fig.show() fig.savefig('sine.pdf')

  1. adding some bells and whistles:

import matplotlib.pyplot as plt import numpy as np

x = np.arange(0, 10, 0.2) y = np.sin(x) fig = plt.figure() ax = fig.add_subplot(111) ax.plot(x, y, '-k', linewidth = 2) ax.plot(x, y, 'ob', markersize = 7) ax.set_title('This is a title') ax.set_xlabel('x axis') ax.set_ylabel('y axis') fig.savefig('sine2.pdf')

(:sourceend:)