import matplotlib.pyplot as plt
data = {'apple':10, 'orange':15, 'lemon':5, 'lime':20}
names = list(data.keys())
values = list(data.values())
fig, axs = plt.subplots(1, 3, figsize=(9, 3), sharey = True)
axs[0].bar(names, values)
axs[1].scatter(names, values)
axs[2].plot(names, values)
fig.suptitle('categorical plotting')
plt.show()
matplotlib.pyplot.subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw=None, **fig_kw)
参数:
向图中增加一个居中标题
参数:
t:类型要求:str;即标题内容
x:类型要求:float,默认为0.5;标题的x坐标
y同上,默认为0.98
import matplotlib.pyplot as plt
cat = ['bored','happy','bored','bored','happy','bored']
dog = ['happy','happy','happy','happy','bored','bored']
activity = ['combing','drinking','feeding','napping','playing','washing']
fig, ax = plt.subplots()
ax.plot(activity,dog,label = 'dog')
ax.plot(activity,cat,label = 'cat')
ax.legend()
plt.show()
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(19680801)
dt = 0.01
t = np.arange(0,30,dt)
nse1 = np.random.randn(len(t))
nse2 = np.random.randn(len(t))
s1 = np.sin(2 * np.pi * 10 * t) + nse1
s2 = np.sin(2 * np.pi * 10 * t) + nse2
fig, axs = plt.subplots(2,1)
axs[0].plot(t, s1, t, s2)
axs[0].set_xlim(0, 2)
axs[0].set_xlabel('time')
axs[0].set_ylabel('s1 and s2')
axs[0].grid(True)
cxy, f = axs[1].cohere(s1, s2, 256, 1. / dt)
axs[1].set_ylabel('coherence')
fig.tight_layout()
plt.show()
随机数种子,每个种子数生成的随机数是不变的。
显示网格线
计算两个信号的交叉谱密度
import numpy as np
import matplotlib.pyplot as plt
fig, (ax1, ax2) = plt.subplots(2,1)
fig.subplots_adjust(hspace=0.5)
dt = 0.01
t = np.arange(0, 30, dt)
np.random.seed(19680801)
nse1 = np.random.randn(len(t))
nse2 = np.random.randn(len(t))
r = np.exp(-t / 0.05)
cnse1 = np.convolve(nse1, r, mode='same') * dt
cnse2 = np.convolve(nse2, r, mode='same') * dt
s1 = dt * np.sin(2 * np.pi * 10 * t) + cnse1
s2 = dt * np.sin(2 * np.pi * 10 * t) + cnse2
ax1.plot(t, s1, t, s2)
ax1.set_xlim(0, 5)
ax1.set_xlabel('time')
ax2.set_ylabel('s1 and s2')
ax1.grid(True)
cxy, f = ax2.csd(s1, s2, 256, 1. / dt)
ax2.set_ylabel('CSD (db)')
plt.show()
参数:
计算输入数组中所有元素的指数。
numpy.exp(x, /, out=None, *, where=True, casting=‘same_kind’, order=‘K’, dtype=None, subok=True[, signature, extobj]) =
参数:
返回两个一维序列的离散线性卷积。
numpy.convolve(a, v, mode=‘full’)
参数:
绘制交叉谱密度。
Axes.csd(self, x, y, NFFT=None, Fs=None, Fc=None, detrend=None, window=None, noverlap=None, pad_to=None, sides=None, scale_by_freq=None, return_line=None, *, data=None, **kwargs)
参数:
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
x = np.arange(10)
y = 2.5 * np.sin(x / 20 * np.pi)
yerr = np.linspace(0.05, 0.2, 10)
plt.errorbar(x, y+3, yerr=yerr, label = 'both limits (default)')
plt.errorbar(x, y+2, yerr = yerr, uplims= True, label = 'uplims_true')
plt.errorbar(x, y-1, yerr=yerr, uplims=True, lolims=True, label = 'uplims_true,lolims_true')
upperlimits = [True, False] * 5
lowerlimits = [False, True] * 5
plt.errorbar(x, y, yerr=yerr, uplims=upperlimits,lolims=lowerlimits, label = 'subsets of uplims and lolims')
plt.legend(loc='lower right')
plt.show()
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
x = np.arange(10) / 10
upperlimits = [True, False] * 5
lowerlimits = [False, True] * 5
y = (x + 0.1) **2
plt.errorbar(x,y,xerr=0.1,xlolims=True,label='xlolims_true')
y = (x+0.1)**3
plt.errorbar(x+0.6,y,xerr=0.1,xuplims=upperlimits,xlolims=lowerlimits, label = 'subsets of xuplims and xlolims')
y = (x+0.1)**4
plt.errorbar(x+1.2,y,xerr=0.1,xuplims=True,label = 'xuplims_true')
plt.legend()
plt.show()