注解
此笔记本可在此处下载: 1_Advanced_Plotting.ipynb
高级绘图
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import cm
%matplotlib nbagg
def func(x, y):
"""
A nice looking mapping.
"""
r = (x**2 + y**2)**.5
theta = np.where( r != 0., np.arccos(x/r) * np.sign(y), 0.)
z = (1. / (r + 5.)**.5) * np.sin(2. * np.pi * r / 3.) * np.cos(4 * theta)
return z
x = np.linspace(-3., 3., 101)
y = np.linspace(-3., 3., 101)
X, Y = np.meshgrid(x, y)
X
array([[-3. , -2.94, -2.88, ..., 2.88, 2.94, 3. ],
[-3. , -2.94, -2.88, ..., 2.88, 2.94, 3. ],
[-3. , -2.94, -2.88, ..., 2.88, 2.94, 3. ],
...,
[-3. , -2.94, -2.88, ..., 2.88, 2.94, 3. ],
[-3. , -2.94, -2.88, ..., 2.88, 2.94, 3. ],
[-3. , -2.94, -2.88, ..., 2.88, 2.94, 3. ]])
Y
array([[-3. , -3. , -3. , ..., -3. , -3. , -3. ],
[-2.94, -2.94, -2.94, ..., -2.94, -2.94, -2.94],
[-2.88, -2.88, -2.88, ..., -2.88, -2.88, -2.88],
...,
[ 2.88, 2.88, 2.88, ..., 2.88, 2.88, 2.88],
[ 2.94, 2.94, 2.94, ..., 2.94, 2.94, 2.94],
[ 3. , 3. , 3. , ..., 3. , 3. , 3. ]])
Z = func(X, Y)
/home/lcharleux/Documents/Informatique/anaconda/envs/myconda1/lib/python3.6/site-packages/ipykernel_launcher.py:6: RuntimeWarning: invalid value encountered in true_divide
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.set_aspect("equal")
cont = plt.contourf(X, Y, Z, 10, cmap = cm.jet)
cbar = plt.colorbar(cont)
cbar.set_label("Amplitude, $z$")
plt.xlabel("Position, $x$")
plt.ylabel("Position, $y$")
plt.contour(X, Y, Z, 10, colors = "black")
plt.show()
<IPython.core.display.Javascript object>