matplotlib matshow 标签 [英] matplotlib matshow labels

查看:127
本文介绍了matplotlib matshow 标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一个月前开始使用 matplotlib,所以我还在学习.
我正在尝试用 matshow 做一个热图.我的代码如下:

I start using matplotlib a month ago, so I'm still learning.
I'm trying to do a heatmap with matshow. My code is the following:

data = numpy.array(a).reshape(4, 4)  
cax = ax.matshow(data, interpolation='nearest', cmap=cm.get_cmap('PuBu'), norm=LogNorm())  
cbar = fig.colorbar(cax)

ax.set_xticklabels(alpha)  
ax.set_yticklabels(alpha)

其中 alpha 是来自 django 的模型,具有 4 个字段:'ABC'、'DEF'、'GHI'、'JKL'

where alpha is a model from django with 4fields: 'ABC', 'DEF', 'GHI', 'JKL'

问题是我不知道为什么,标签ABC"没有出现,最后一个单元格没有标签.
如果有人知道如何修改我的脚本以显示ABC",我将不胜感激:)

the thing is that I don't know why, the label 'ABC' doesn't appear, leaving the last cell without label.
If someone would have a clue how to modify my script in a way to appear the 'ABC' I would be grateful :)

推荐答案

使用 matshow 时 xticks 实际上延伸到显示的图形之外.(我不太确定这是为什么.不过,我几乎从未使用过 matshow.)

What's happening is that the xticks actually extend outside of the displayed figure when using matshow. (I'm not quite sure exactly why this is. I've almost never used matshow, though.)

为了证明这一点,请查看 ax.get_xticks() 的输出.在您的情况下,它是 array([-1., 0., 1., 2., 3., 4.]).因此,当您设置 xtick 标签时,ABC"在 <-1, -1> 处,并且不会显示在图中.

To demonstrate this, look at the output of ax.get_xticks(). In your case, it's array([-1., 0., 1., 2., 3., 4.]). Therefore, when you set the xtick labels, "ABC" is at <-1, -1>, and isn't displayed on the figure.

最简单的解决方案是在标签列表中添加一个空白标签,例如

The easiest solution is just to prepend a blank label to your list of labels, e.g.

ax.set_xticklabels(['']+alpha)
ax.set_yticklabels(['']+alpha)

作为一个完整的例子:

import numpy as np
import matplotlib.pyplot as plt

alpha = ['ABC', 'DEF', 'GHI', 'JKL']

data = np.random.random((4,4))

fig = plt.figure()
ax = fig.add_subplot(111)
cax = ax.matshow(data, interpolation='nearest')
fig.colorbar(cax)

ax.set_xticklabels(['']+alpha)
ax.set_yticklabels(['']+alpha)

plt.show()

这篇关于matplotlib matshow 标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆