Python Programming/matplotlib

From Wikibooks, open books for an open world
Jump to navigation Jump to search

matplotlib is a Python library that allows Python to be used like Matlab, visualizing data on the fly. It is able to create plots, histograms, power spectra, bar charts, errorcharts, scatterplots, etc. It can be used from normal Python and also from iPython.

Examples:

Plot a data series that represents the square function:

from matplotlib import pyplot as plt
data = [x * x for x in range(20)]
plt.plot(data)
plt.show()

Plot a data series that represents the square function but in reverse order, by providing not only the series of the y-axis values but also the series of x-axis values:

from matplotlib import pyplot as plt
datax = range(20)
datax.reverse()
datay = [x * x for x in range(20)]
plt.plot(datax, datay)
plt.show()

Plot the square function, setting the limits for the y-axis:

from matplotlib import pyplot as plt
data = [x * x for x in range(20)]
plt.ylim(-500, 500) # Set limits of y-axis
plt.plot(data)
plt.show()

Links[edit]