小马良看了直呼“专业”,持续学习matplotlib绘图中···day2

学习matplotlib绘图day2,加油

1.Plotting categorical variables 绘制分类变量

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()

1.1matplotlib.pyplot.subplots

matplotlib.pyplot.subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw=None, **fig_kw)
参数:

  • nrows,ncols:类型要求:int,参数可选,默认值1;创建nrows行×ncols列子图
  • sharex,sharey:类型要求:bool或’none’,‘all’,‘row’,‘col’,默认为False;True或’all’:所有子图共用一个坐标轴;False或’none’:每个子图有单独的坐标轴;‘row’:每行子图共用坐标轴;‘col’:每列子图共用坐标轴

1.2 suptitle(self, t, **kwargs)

向图中增加一个居中标题
参数:
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()

2 Plotting the coherence of two signals 绘制两个信号的相干性

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()

2.1 random.seed()

随机数种子,每个种子数生成的随机数是不变的。

2.2 grid(True)

显示网格线

3 CSD Demo CSD演示

计算两个信号的交叉谱密度

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()

3.1 subplots_adjust(self, left=None, bottom=None, right=None, top=None, wspace=None, hspace=None)

参数:

  • hspace:类型要求:float;决定着子图间的预留空白,用平均轴的分数表示。

3.2 numpy.exp

计算输入数组中所有元素的指数。
numpy.exp(x, /, out=None, *, where=True, casting=‘same_kind’, order=‘K’, dtype=None, subok=True[, signature, extobj]) =
参数:

  • x:类型要求:类数组;输入值

3.3 numpy.convolve

返回两个一维序列的离散线性卷积。
numpy.convolve(a, v, mode=‘full’)
参数:

  • a:类型要求:类数组;第一组数据
  • v:类型要求:类数组;第二组数据
  • mode:从’full’,‘valid’,’same‘中选择;默认为full,涉及到卷积,以后深入学习。tag。

3.3 matplotlib.axes.Axes.csd

绘制交叉谱密度。
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)
参数:

  • x,y:一维数组或序列

4 Errorbar limit selection 误差条限制选择

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()

你可能感兴趣的:(python,numpy,数据可视化)