matplotlib matshow标签 [英] matplotlib matshow labels

查看:170
本文介绍了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天全站免登陆