# -*- coding: utf-8 -*- import test_numpy as np import matplotlib.pyplot as plt X = np.linspace(-4, 4, 1024) Y = .25 * (X + 4.) * (X + 1.) * (X - 2.) plt.annotate('Brackmard minimum', ha='center', va='bottom', xytext=(-1.5, 3.), xy=(0.75, -2.7), arrowprops={ 'facecolor': 'black', 'shrink': 0.05 }) plt.plot(X, Y) plt.show()
# -*- coding: utf-8 -*- """ ================== A simple Fill plot ================== This example showcases the most basic fill plot a user can do with matplotlib. """ import test_numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 1, 500) y = np.sin(4 * np.pi * x) * np.exp(-5 * x) fig, ax = plt.subplots() # 定义 zorder,是为了保证 grid 不会覆盖在 Polygon 上 ax.fill(x, y, zorder=10) # 打开网格, ax.grid(True, zorder=5) plt.show()
# -*- coding: utf-8 -*- import test_numpy as np import matplotlib.pyplot as plt def pdf(X, mu, sigma): a = 1. / (sigma * np.sqrt(2. * np.pi)) b = -1. / (2. * sigma**2) return a * np.exp(b * (X - mu)**2) X = np.linspace(-6, 6, 1000) for i in range(5): samples = np.random.standard_normal(50) mu, sigma = np.mean(samples), np.std(samples) plt.plot(X, pdf(X, mu, sigma), color='k') plt.plot(X, pdf(X, 0., 1.), color='k') plt.show()
# -*- coding: utf-8 -*- import test_numpy as np import matplotlib.pyplot as plt X = np.linspace(0, 2 * np.pi, 100) Ya = np.sin(X) Yb = np.cos(X) # 两次调用 plot,生成两条曲线 plt.plot(X, Ya) plt.plot(X, Yb) plt.show()
# -*- coding: utf-8 -*- '''plot the binomial x^2-2x+1 in the [-3,2] interval using 200 points''' import test_numpy as np import matplotlib.pyplot as plt X = np.linspace(-3, 2, 200) Y = X**2 - 2 * X + 1 plt.plot(X, Y) plt.show()
# -*- coding: utf-8 -*- import test_numpy as np import matplotlib.pyplot as plt def plot_slope(X, Y): Xs = X[1:] - X[:-1] Ys = Y[1:] - Y[:-1] plt.plot(X[1:], Ys / Xs) X = np.linspace(-3, 3, 100) Y = np.exp(-X**2) plt.plot(X, Y) plot_slope(X, Y) plt.show()
# -*- coding: utf-8 -*- """ ======================== A more complex fill demo ======================== In addition to the basic fill plot, this demo shows a few optional features: * Multiple curves with a single command. * Setting the fill color. * Setting the opacity (alpha value). """ import test_numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 2 * np.pi, 500) y1 = np.sin(x) y2 = np.sin(3 * x) fig, ax = plt.subplots() ax.fill(x, y1, 'b', x, y2, 'r', alpha=0.3) plt.show()