116 KiB
116 KiB
Libraries
# libraries
import scipy
# "SciPy" provides algorithms for optimization, integration, interpolation, eigenvalue problems,
# algebraic equations, differential equations, statistics and many other classes of problems.
import numpy as np
# Fast and versatile, the "NumPy" vectorization, indexing, and broadcasting concepts are the
# de-facto standards of array computing today.
import matplotlib.pyplot as plt
# "Matplotlib" is a comprehensive library for creating static, animated, and interactive
# visualizations in Python.
Data
= np.linspace(0,2*np.pi)
x= np.sin(x) y
Anatomy of a figure
%matplotlib inline
from PIL import Image
= Image.open('anatomy.webp')
img "anatomy.png") img.save(
# import image module
from IPython.display import Image
# get the image
="anatomy.png", width=500, height=500) Image(url

Implicit or explicit?
Using figures
- Explicitly create Figures and Axes, and call methods on them (the "object-oriented (OO) style").
- Rely on pyplot to implicitly create and manage the Figures and Axes, and use pyplot functions for plotting.
# implicit
="sin")
plt.plot(x,y,label'x label')
plt.xlabel('y label')
plt.ylabel("Simple Plot")
plt.title(
plt.legend() plt.show()
# explicity
= plt.figure(figsize=(6,6)) # size
fig = plt.subplot(aspect=1) # aspect ratio
ax ="sin") # label
ax.plot(x,y,label'x') # Add an x-label to the axes.
ax.set_xlabel('y') # Add a y-label to the axes.
ax.set_ylabel("Simple Plot") # Add a title to the axes.
ax.set_title(# Add a legend. ax.legend()
<matplotlib.legend.Legend at 0x7fcca3b14dc0>
Figure : lines
= plt.figure(figsize=(6,6))
fig = plt.subplot(aspect=1)
ax ="sin",color='blue', linewidth=3, linestyle='--')
ax.plot(x,y,label*y,label="$\sin^2$",color='red', linewidth=1, linestyle='dotted')
ax.plot(x,y'x') # Add an x-label to the axes.
ax.set_xlabel('y') # Add a y-label to the axes.
ax.set_ylabel("Simple Plot") # Add a title to the axes.
ax.set_title(# Add a legend. ax.legend()
<matplotlib.legend.Legend at 0x7fdc69fe4b90>
ax.plot?
Figure and Axes
#
= plt.figure(figsize=(6,6))
fig = plt.subplot(aspect=1)
ax ".r",label="$\sin(x)$")
ax.plot(x,y,*y,".g",label="$\sin(x)^2$")
ax.plot(x,y
ax.legend()"x")
ax.set_xlabel("y") ax.set_ylabel(
Text(0,0.5,'y')
Figures : axes and text
# adding text
= plt.figure(figsize=(6,6))
fig = plt.subplot(aspect=1)
ax ".",label="sin")
ax.plot(x,y,
ax.legend()
0.3, 0.1, "-> Mot",family="cursive",size=14)
ax.text(0.3, -0.5, "-> Mot",family="serif",size = 14)
ax.text(
'point (3,0)', xy=(3, 0), xytext=(4, 0.5),
ax.annotate(=dict(facecolor='black', shrink=0.05))
arrowprops
'Title')
ax.set_title("x")
ax.set_xlabel("y")
ax.set_ylabel(
Text(0,0.5,'y')
figure : scales
= plt.figure(figsize=(6,6)) # size
fig = plt.subplot(aspect=1) # aspect ratio
ax
="sin") # label
ax.plot(x,y,label'log')
ax.set_xscale('log')
ax.set_yscale('x') # Add an x-label to the axes.
ax.set_xlabel('y') # Add a y-label to the axes.
ax.set_ylabel("Title") # Add a title to the axes.
ax.set_title(# Add a legend. ax.legend()
<matplotlib.legend.Legend at 0x7fdc5860e790>
figures multiples
= plt.subplots(nrows=1, ncols=3,
fig, (ax0, ax1, ax2) =(6, 6))
figsize
fig.tight_layout()'a')
ax0.set_title(="sin") # label
ax0.plot(x,y,label
="sin") # label
ax1.plot(x,y,label
'title')
ax2.set_title(="sin") # label ax2.plot(x,y,label
[<matplotlib.lines.Line2D at 0x7fdc69e73190>]
figure : save
#save
"output.png")
fig.savefig("output.pdf") fig.savefig(