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 标签


基础教程推荐
- Kivy 使用 opencv.调整图像大小 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- 在 Python 中将货币解析为数字 2022-01-01