05 矩阵图 matshow
用途
带行列刻度的矩阵可视化,适用于阻抗矩阵、转移矩阵、相关系数阵。
示例(5 张)
05-1 节点阻抗矩阵幅值

05-2 月—时负荷矩阵(带行列刻度)

05-3 电气量相关系数矩阵

05-4 谐波次数与节点的耦合矩阵

05-5 负荷状态转移概率矩阵

代码
以下 5 个函数取自 code/第一批_分布与矩阵261107.py,直接调用即可出图。样式(宋体 + Times、10 磅、刻度朝内四边、留白自检)由 code/公共配置261107.py 的 setup_cn_style() 和 save() 统一提供,本节不再重复。
# 摘自 code/第一批_分布与矩阵261107.py
def ms_01():
"""节点阻抗矩阵幅值。"""
fig, ax = new_fig(left=0.19, bottom=0.21, right=0.86)
rng = np.random.default_rng(501)
n = 8
M = 0.12 + 0.05 * rng.random((n, n))
M = (M + M.T) / 2
np.fill_diagonal(M, 0.16 + 0.02 * rng.random(n))
im = ax.matshow(M, cmap=CMAP_SEQ)
ax.xaxis.set_ticks_position("bottom")
ax.set_xlabel("节点编号")
ax.set_ylabel("节点编号")
cbar(fig, ax, im, "阻抗模值 / pu")
save(fig, "05_矩阵图", "05_矩阵图_节点阻抗矩阵")
def ms_02():
"""月—时负荷矩阵(带行列刻度)。"""
fig, ax = new_fig(left=0.19, bottom=0.23, right=0.86)
rng = np.random.default_rng(502)
M = np.zeros((12, 24))
for m in range(12):
s = 1 + 0.2 * np.cos((m - 0.5) / 12 * 2 * np.pi + np.pi)
for h in range(24):
M[m, h] = s * (430 + 180 * np.exp(-((h - 10) / 3.4) ** 2)
+ 210 * np.exp(-((h - 16) / 3.0) ** 2)) + rng.normal(0, 12)
im = ax.matshow(M, cmap=CMAP_SEQ, aspect="auto")
ax.xaxis.set_ticks_position("bottom")
ax.set_xticks([0, 4, 8, 12, 16, 20, 23])
ax.set_yticks([0, 3, 6, 9, 11])
ax.set_yticklabels(["1", "4", "7", "10", "12"])
ax.set_xlabel("时刻 " + IT["t"] + " / h")
ax.set_ylabel("月份")
cbar(fig, ax, im, "负荷 " + IT["P"] + " / kW")
save(fig, "05_矩阵图", "05_矩阵图_月时负荷矩阵")
def ms_03():
"""电气量相关系数矩阵。"""
fig, ax = new_fig(left=0.26, bottom=0.28, right=0.86)
rng = np.random.default_rng(503)
names = ["负荷", "电压", "电流", "功率因数", "温度", "谐波"]
A = rng.normal(0, 1, (6, 300))
A[1] = -0.75 * A[0] + 0.4 * rng.normal(0, 1, 300)
A[2] = 0.92 * A[0] + 0.2 * rng.normal(0, 1, 300)
A[3] = 0.5 * A[0] + 0.5 * rng.normal(0, 1, 300)
A[4] = 0.68 * A[0] + 0.5 * rng.normal(0, 1, 300)
A[5] = 0.55 * A[0] + 0.6 * rng.normal(0, 1, 300)
Cm = np.corrcoef(A)
im = ax.matshow(Cm, cmap=CMAP_DIV, vmin=-1, vmax=1)
for i in range(6):
for j in range(6):
text_in(ax, j, i, f"{Cm[i, j]:.2f}", color="0.15", size=FS - 4,
ha="center", va="center")
ax.xaxis.set_ticks_position("bottom")
ax.set_xticks(range(6))
ax.set_yticks(range(6))
ax.set_xticklabels(names, rotation=30, ha="right")
ax.set_yticklabels(names)
cbar(fig, ax, im, "相关系数 " + IT["r"])
save(fig, "05_矩阵图", "05_矩阵图_相关系数矩阵")
def ms_04():
"""谐波次数与节点的耦合矩阵。"""
fig, ax = new_fig(left=0.20, bottom=0.22, right=0.86)
rng = np.random.default_rng(504)
orders = [3, 5, 7, 9, 11, 13]
M = np.zeros((6, 6))
for j, h in enumerate(orders):
M[:, j] = 30 / h ** 0.9 * (1 + 0.5 * rng.random(6))
im = ax.matshow(M, cmap=CMAP_SEQ, aspect="auto")
ax.xaxis.set_ticks_position("bottom")
ax.set_xticks(range(6))
ax.set_yticks(range(6))
ax.set_xticklabels([str(h) for h in orders])
ax.set_yticklabels([f"N{i + 1}" for i in range(6)])
ax.set_xlabel("谐波次数 " + IT["h"])
ax.set_ylabel("节点")
cbar(fig, ax, im, IT["I"] + SUB["h"] + " / A")
save(fig, "05_矩阵图", "05_矩阵图_谐波耦合矩阵")
def ms_05():
"""负荷状态转移概率矩阵。"""
fig, ax = new_fig(left=0.22, bottom=0.24, right=0.86)
rng = np.random.default_rng(505)
M = rng.random((5, 5))
M = M / M.sum(axis=1, keepdims=True)
im = ax.matshow(M, cmap=CMAP_SEQ, vmin=0, vmax=1)
for i in range(5):
for j in range(5):
text_in(ax, j, i, f"{M[i, j]:.2f}", color="0.15", size=FS - 4,
ha="center", va="center")
ax.xaxis.set_ticks_position("bottom")
ax.set_xticks(range(5))
ax.set_yticks(range(5))
ax.set_xticklabels(["低", "较低", "中", "较高", "高"])
ax.set_yticklabels(["低", "较低", "中", "较高", "高"])
ax.set_xlabel("次日负荷状态")
ax.set_ylabel("当日负荷状态")
cbar(fig, ax, im, "转移概率")
save(fig, "05_矩阵图", "05_矩阵图_负荷状态转移矩阵")
说明
- 图片尺寸 1181×944 px(100 mm × 80 mm @ 300 dpi),三维图为 1181×1122 px。
- 图内不放标题,图题由本文件的二级/三级标题承担。
- 数据为贴近广东实际量级的仿真算例,随机种子固定,重跑结果一致。
浙公网安备 33010602011771号