Jump to content
Wikibooks The Free Textbook Project

Python Programming/matplotlib

From Wikibooks, open books for an open world

This is the current revision of this page, as edited by Dan Polansky (discuss | contribs) at 09:07, 29 March 2024 (+link to wiki:plotting, although it is for a more general subject for which this book currently has no dedicated page). The present address (URL) is a permanent link to this version.

Revision as of 09:07, 29 March 2024 by Dan Polansky (discuss | contribs) (+link to wiki:plotting, although it is for a more general subject for which this book currently has no dedicated page)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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:

frommatplotlibimport 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:

frommatplotlibimport pyplot as plt
datax = list(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:

frommatplotlibimport 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()
[edit | edit source ]

AltStyle によって変換されたページ (->オリジナル) /