Full Blog TOC

Full Blog Table Of Content with Keywords Available HERE

Thursday, August 1, 2024

Matplotlib Quick Start


 


This is a quickstart for matplotlib.

Many more examples can be located in: 

  • https://matplotlib.org/stable/gallery/index.html
  • https://matplotlib.org/stable/tutorials/index.html


To run the following first install:

pip3 install numpy
pip3 install matplotlib


Examples:


import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)
plt.plot(x, np.sin(x))
plt.savefig("plots/line.pdf")

plt.clf()
plt.xlim([3, 6])
plt.ylim([-1, 0.2])
plt.xlabel("x")
plt.ylabel("sin(x)")
plt.plot(x, np.sin(x))
plt.savefig("plots/line_zoom.pdf")

plt.clf()
plt.gca().relim()
plt.gca().autoscale()
plt.plot(x, np.sin(x), linestyle='', marker='x')
plt.savefig("plots/dots.pdf")

plt.clf()
x = np.random.normal(loc=0.0, scale=1.0, size=200)
y = np.random.normal(loc=0.0, scale=1.0, size=200)
plt.scatter(x, y)
plt.savefig("plots/scatter.pdf")

plt.clf()
x = np.random.normal(loc=0.0, scale=1.0, size=10000)
bins = np.arange(-5, 5, 0.1)
plt.hist(x, bins=bins, alpha=0.5)
plt.savefig("plots/histogram.pdf")

plt.clf()
fig, ax = plt.subplots(nrows=2, ncols=3, sharex=True, sharey=True)
x = np.random.normal(loc=0.0, scale=1.0, size=1000)
for row in ax:
for col in row:
x = np.random.normal(loc=0.0, scale=1.0, size=1000)
bins = np.arange(-5, 5, 0.5)
col.hist(x, bins=bins, alpha=0.5)
plt.savefig("plots/subplots.pdf")


Results:















No comments:

Post a Comment