matplotlib matshow labels(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 标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:matplotlib matshow 标签
基础教程推荐
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
