435DCC17-F033-405B-8F3D-1702B20F4214.jpeg

Full Transcript

# Matplotlib ## What is Matplotlib? * Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python. * Matplotlib makes easy things easy and hard things possible. * Create publication-quality plots. * Make interactive figures that can zoom, p...

# Matplotlib ## What is Matplotlib? * Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python. * Matplotlib makes easy things easy and hard things possible. * Create publication-quality plots. * Make interactive figures that can zoom, pan, and update. * Customize visual style and layout. * Export to many file formats. * Embed in JupyterLab and graphical user interfaces. * Use a rich array of third-party packages built on Matplotlib. ## Matplotlib Simple Plot ### Code ```python import matplotlib.pyplot as plt import numpy as np # Prepare the data x = np.linspace(0, 10, 100) # Plot the data plt.plot(x, x, label='linear') # Add a legend plt.legend() # Show the plot plt.show() ``` ### Output The output is a simple plot of a line with a label. ## Anatomy of a Matplotlib Plot ### Figure * The **Figure** keeps track of all the child **Axes**, a smattering of 'special' artists (titles, figure legends, etc), and the **Canvas**. * One way to think of the **Figure** is as a single container that contains all the objects representing axes, graphics, text, and labels. ### Axes * This is what you think of as 'a plot', it is the region of the image with the data space. * A given **Figure** can contain many **Axes**, but a given **Axes** object can only be in one **Figure**. * The **Axes** contains two (or three in the case of 3D) **Axis** objects (be aware of the difference between **Axes** and **Axis**) which take care of the data limits. ### Axis * These are the number-line-like objects. * They take care of generating the plot scale and setting the limits and generating the ticks (the marks on the Axis) and ticklabels (strings labeling the ticks). ### Artist * Basically, everything you can see on the Figure is an **Artist** (even the **Figure**, the **Axes**, and the **Axis** objects). * This includes **Text** objects, **Line2D** objects, **collections** objects, **Patch** objects, etc. * When the Figure is rendered, all of the Artists are drawn to the **Canvas**.