💾 Archived View for gem.sdf.org › s.kaplan › cheatsheets › libraries-and-frameworks › matplotlib.md captured on 2024-03-21 at 15:44:30.
⬅️ Previous capture (2023-09-08)
-=-=-=-=-=-=-
# Matplotlib Cheatsheet Matplotlib is a popular plotting library for Python. It provides a wide range of tools for creating static, animated, and interactive visualizations in Python. This cheatsheet provides a quick reference for some of Matplotlib's unique features, including code blocks for basic plots, subplots, customization, and more. Additionally, it includes a list of resources for further learning. ## Basic Plots
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.plot(x, y)
plt.show()
x = np.random.rand(100)
y = np.random.rand(100)
plt.scatter(x, y)
plt.show()
x = np.random.normal(size=1000)
plt.hist(x, bins=30)
plt.show()
## Subplots
fig, axs = plt.subplots(2, 2, figsize=(6, 6))
axs[0, 0].plot(x, y)
axs[0, 1].scatter(x, y)
axs[1, 0].hist(x, bins=30)
axs[1, 1].imshow(np.random.rand(100, 100))
plt.show()
## Customization
plt.plot(x, y)
plt.title('Sine Wave')
plt.xlabel('X')
plt.ylabel('Y')
plt.show()
plt.style.use('ggplot')
plt.plot(x, y)
plt.show()
plt.plot(x, np.sin(x), label='sin(x)')
plt.plot(x, np.cos(x), label='cos(x)')
plt.legend()
plt.show()
## Other Useful Features
plt.plot(x, y)
plt.savefig('figure.png')
plt.clf()
plt.close('all')
## Resources - [Matplotlib documentation](https://matplotlib.org/stable/contents.html) - [Matplotlib tutorials](https://matplotlib.org/stable/tutorials/index.html) - [Python Data Science Handbook](https://jakevdp.github.io/PythonDataScienceHandbook/index.html)