首页 文章

使用带有矩形切片的colorbar绘制相关矩阵中的数字范围[matshow(matplotlib)]

提问于
浏览
1

我试图绘制一个相关矩阵,其中的值显示在图中 . 我想要瓦片上的范围(置信区间),而不是单个相关系数值 . 为了达到这个目的,我首先使用matshow绘制带有颜色条的矩阵,并使用居中选项在绘图上绘制低和高间隔的单独写入值 . 这是该代码的片段 .

cax = ax.matshow(data, interpolation='nearest', cmap=GnRd, vmin=-1, vmax=1 ,alpha=1 )
fig.colorbar(cax, ticks=[-1,0,1], shrink=0.8)
for (i, j), z in np.ndenumerate(data2):
  if i>=j and z > -0.5 and z < 1:
     ax.text(j, i, '{:0.2f}'.format(z), ha='left', va='center', size=28, color='black', **hfont)
  if z < -0.5 :
     ax.text(j, i, '{:0.2f}'.format(z), ha='left', va='center', size=28, color='black', fontweight='bold', **hfont)
  if z == 1 :
     ax.text(j, i, '{:0.2f}'.format(z), ha='center', va='center', size=28, color='black', fontweight='bold', **hfont)
for (i, j), z in np.ndenumerate(data3):
  if i>=j and z > -0.5 and z <1:
     ax.text(j, i, '{:0.2f}'.format(z), ha='right', va='center', size=28, color='black', **hfont)
  if z < -0.5 :
     ax.text(j, i, '{:0.2f}'.format(z), ha='right', va='center', size=28, color='black', fontweight='bold', **hfont)

plt.show()

这是我的情节:

enter image description here

这个图的问题是瓷砖着色不正确,我不知道如何为一个范围着色瓷砖 . 此外,瓷砖上有一个浪费的空间,可以通过制作 Headers 矩形来减少,但我相信matshow不是那种选择 . 我找到了一些基于对象绘图的解决方法,这可能会让我的生活更加复杂 . 任何帮助将不胜感激 .

1 回答

  • 0

    我想出了一个问题的答案 . 我更改了我的 aspect='auto' 并且瓷砖变成了另一个问题colorbar问题的矩形,我决定简单地使用这些数字的平均值进行绘图:

    cax = ax.matshow(data, interpolation='nearest', cmap=GnRd, vmin=-1, vmax=1 ,alpha=1, aspect='auto' )
    fig.colorbar(cax, ticks=[-1,0,1], shrink=0.8)
    for (i, j), z in np.ndenumerate(data2):
       if i>=j and z > -0.5 and z < 1:
          ax.text(j, i, '{:0.2f}'.format(z), ha='left', va='center', size=28, color='black', **hfont)
       if z < -0.5 :
          ax.text(j, i, '{:0.2f}'.format(z), ha='left', va='center', size=28, color='black', fontweight='bold', **hfont)
        if z == 1 :
          ax.text(j, i, '{:0.2f}'.format(z), ha='center', va='center', size=28, color='black', fontweight='bold', **hfont)
    for (i, j), z in np.ndenumerate(data3):
         if i>=j and z > -0.5 and z <1:
           ax.text(j, i, '{:0.2f}'.format(z), ha='right', va='center', size=28, color='black', **hfont)
         if z < -0.5 :
           ax.text(j, i, '{:0.2f}'.format(z), ha='right', va='center', size=28, color='black', fontweight='bold', **hfont)
    
    plt.show()
    

    enter image description here

相关问题