This document provides an introduction to Matplotlib, a Python library for creating 2D plots. It explains how to initialize Matplotlib, prepare and render data, customize plots, and save figures in different formats. The document also covers common plot types like line plots, scatter plots, histograms, and more.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
15 views1 page
Handout Beginner44
This document provides an introduction to Matplotlib, a Python library for creating 2D plots. It explains how to initialize Matplotlib, prepare and render data, customize plots, and save figures in different formats. The document also covers common plot types like line plots, scatter plots, histograms, and more.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1
Matplotlib for beginners
Matplotlib is a library for making 2D plots in Python. It is
Z = np.random.uniform(0, 1, (8, 8)) Organize designed with the philosophy that you should be able to create simple plots with just a few commands: You can plot several data on the same figure, but you can ax.contourf(Z) also split a figure in several subplots (named Axes): 1 Initialize Z = np.random.uniform(0, 1, 4) X = np.linspace(0, 10, 100) import numpy as np Y1, Y2 = np.sin(X), np.cos(X) import matplotlib.pyplot as plt ax.pie(Z) ax.plot(X, Y1, X, Y2)
2 Prepare ax1.plot(X, Y1, color=”C1”) X = np.linspace(0, 10*np.pi, 1000) ax.hist(Z) ax2.plot(X, Y2, color=”C0”) Y = np.sin(X) X = np.arange(5) fig, (ax1, ax2) = plt.subplots(1, 2) 3 Render Y = np.random.uniform(0, 1, 5) ax1.plot(Y1, X, color=”C1”) ax.errorbar(X, Y, Y∕4) ax2.plot(Y2, X, color=”C0”) fig, ax = plt.subplots() ax.plot(X, Y) Z = np.random.normal(0, 1, (100, 3)) plt.show() Label (everything) ax.boxplot(Z) 4 Observe A Sine wave ax.plot(X, Y) 1.0 fig.suptitle(None) 0.5 Tweak ax.set_title(”A Sine wave”) 0.0 0.5 You can modify pretty much anything in a plot, including lim- ax.plot(X, Y) 1.0 its, colors, markers, line width and styles, ticks and ticks la- 0 5 10 15 20 25 30 ax.set_ylabel(None) bels, titles, etc. ax.set_xlabel(”Time”) Time
Choose X = np.linspace(0, 10, 100)
Y = np.sin(X) Explore Matplotlib offers several kind of plots (see Gallery): ax.plot(X, Y, color=”black”) Figures are shown with a graphical user interface that al- X = np.random.uniform(0, 1, 100) X = np.linspace(0, 10, 100) lows to zoom and pan the figure, to navigate between the Y = np.random.uniform(0, 1, 100) Y = np.sin(X) different views and to show the value under the mouse. ax.scatter(X, Y) ax.plot(X, Y, linestyle=”--”) Save (bitmap or vector format) X = np.arange(10) X = np.linspace(0, 10, 100) Y = np.random.uniform(1, 10, 10) Y = np.sin(X) ax.bar(X, Y) ax.plot(X, Y, linewidth=5) fig.savefig(”my-first-figure.png”, dpi=300) fig.savefig(”my-first-figure.pdf”) Z = np.random.uniform(0, 1, (8, 8)) X = np.linspace(0, 10, 100) Y = np.sin(X) Matplotlib 3.7.4 handout for beginners. Copyright (c) 2021 Matplotlib Development ax.imshow(Z) ax.plot(X, Y, marker=”o”) Team. Released under a CC-BY 4.0 International License. Supported by NumFOCUS.