Main.SineCurve History
Hide minor edits - Show changes to markup
(:source lang=python:)
""" Our first matplotlib plot """
- matplotlib and pyplot
- pyplot is a module in matplotlib that provides a Matlab-like interface
- to most of the functionality of matplotlib. for most purposes it is all
- you need.
- first we import pyplot and numpy
import matplotlib.pyplot as plt import numpy as np
- create the data
x = np.arange(0, 10, 0.2) y = np.sin(x)
- plot it
plt.plot(x, y)
- you may sometimes need to tell matplotlib to actually show the figure
plt.show()
- to save the figure:
plt.savefig('sine.pdf')
- 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')
- 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:)
