python - imshow 上/下三角形周围的边框

标签 python matplotlib

这个问题与@bgbg 的 question about how to visualize only the upper or lower triangle of a symmetric matrix in matplotlib 有关.使用他的代码(显示在最后),我们可以生成如下图:

upper triangle of a symmetric matrix

现在我的问题是:我们如何才能仅在这组 block 周围绘制黑色边框?我问,因为我想绘制两组相关数据并将它们作为上下三角形并排放置。然后,我们可以独立在每个三角形周围绘制一个深色边框,以将两个三角形分开并显示它们是不同的指标。所以,像这样,但不要混淆:

upper and lower triangle of two different symmetric matrices

怎么做?

#Figure 1
import numpy as NP
from matplotlib import pyplot as PLT
from matplotlib import cm as CM

A = NP.random.randint(10, 100, 100).reshape(10, 10)
mask =  NP.tri(A.shape[0], k=-1)
A = NP.ma.array(A, mask=mask) # mask out the lower triangle
fig = PLT.figure()
ax1 = fig.add_subplot(111)
cmap = CM.get_cmap('jet', 10) # jet doesn't have white color
cmap.set_bad('w') # default value is 'k'
ax1.imshow(A, interpolation="nearest", cmap=cmap)
ax1.grid(True)
axis('off')

#Figure 2
A = NP.random.randint(10, 100, 100).reshape(10, 10)
mask =  NP.tri(A.shape[0], k=-1)
mask = NP.zeros_like(A)
mask[NP.arange(10), NP.arange(10)] = 1
A = NP.ma.array(A, mask=mask) # mask out the lower triangle
fig = PLT.figure()
ax1 = fig.add_subplot(111)
cmap = CM.get_cmap('jet', 10) # jet doesn't have white color
cmap.set_bad('w') # default value is 'k'
ax1.imshow(A, interpolation="nearest", cmap=cmap)
title("Correlation Data 1")
ylabel("Correlation Data 2")
yticks([])
xticks([])

最佳答案

您可以使用 patches.Polygon 绘制边框:

import numpy as NP
from matplotlib import pyplot as PLT
import matplotlib.patches as patches

N = 10
A = NP.random.randint(10, 100, N * N).reshape(N, N)
mask = NP.tri(A.shape[0], k=-1)
mask = NP.zeros_like(A)
mask[NP.arange(N), NP.arange(N)] = 1
A = NP.ma.array(A, mask=mask)  # mask out the lower triangle
fig, ax = PLT.subplots()

cmap = PLT.get_cmap('jet', 10)  # jet doesn't have white color
cmap.set_bad('w')  # default value is 'k'
ax.imshow(A, interpolation="nearest", cmap=cmap, extent=[0, N, 0, N])

line = ([(0, N - 1), (0, 0), (N - 1, 0)] +
        [(N - 1 - i - j, i + 1) for i in range(N - 1) for j in (0, 1)])
lines = [line, [(N - x, N - y) for x, y in line]]
for line in lines:
    path = patches.Polygon(line, facecolor='none', edgecolor='black',
                           linewidth=5, closed=True, joinstyle='round')
    ax.add_patch(path)
ax.set_xlabel("Correlation Data 1")
ax.xaxis.set_label_position('top')
ax.set_ylabel("Correlation Data 2")
ax.set_yticks([])
ax.set_xticks([])
margin = 0.09
ax.set_xlim(-margin, N + margin)
ax.set_ylim(-margin, N + margin)
ax.set_frame_on(False)
PLT.show()

enter image description here

关于python - imshow 上/下三角形周围的边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31328671/

相关文章:

python - 我如何判断一个值是 Django 模板中的字符串还是列表?

python - 使用 lxml.html 提取文本

python - cmap 和颜色列表之间的区别

python - 在 SymPy 的 `` plot3d`` 中更改颜色图

python - 将映射函数应用到 ndarray 的每个成员,并以索引作为参数

python - python环境中不规则网格上的曲面图

python - 如何在 Python 中重复 x 分钟?

python - 如何在 Streamlit 页面中制作折线图动画

python - 跳过条形图中的零值——Matplotlib

python - plt.scatter 列表中的二维数组元素