python - matplotlib 表:每列范围的单独颜色图
问题描述
我正在使用 matplotlib 创建一个 5 行 4 列的表。我想使用颜色显示每个单独列中所有条目的值差异。理想情况下,我想使用为每列个性化的颜色图比例,这意味着该列的颜色图的比例将具有该列值的范围。
澄清一下 - 在第二列中,值在 800-1200 之间,但第一列值在 120-230 之间。当在整个表的范围上应用相同的颜色图时,与颜色图范围为 120-230 而不是 120-1200 时相比,第一列中值之间的差异定义得更少。
这似乎不适用于 matplotlib,因为颜色图适用于整个表格。我想要的也可能只是一个糟糕而令人困惑的演示文稿,所以如果有更好的方法来展示我想要的东西,请告诉我!
这就是我现在所拥有的:
fig, ax = plt.subplots()
rows = ['%d nodes' % x for x in (10, 30, 50, 75, 100)]
columns=['TP', 'TN', 'FP', 'FN']
conf_data = np.array(
[[ 230, 847, 784, 208],
[ 156, 1240, 391, 282],
[ 146, 1212, 419, 292],
[ 130, 1148, 483, 308],
[ 122, 1173, 458, 316]]
)
normal = plt.Normalize(np.min(conf_data), np.max(conf_data))
fig.patch.set_visible(False)
ax.axis('off')
ax.axis('tight')
ax.table(cellText=conf_data,
rowLabels=rows,
colLabels=columns,
cellColours=cm.GnBu(normal(conf_data)),
loc='center',
colWidths=[0.1 for x in columns])
fig.tight_layout()
plt.show()
解决方案
您可以减去每列的最小值并除以它们的ptp
. np.ptp
或峰到峰距离是最大值和最小值之间的差异。将其保存到一个新数组中以用于颜色值。
为避免最大值的蓝色太深,您可以将结果乘以 0.8 之类的值。(或者,您可以更改文本颜色,这需要一些额外的代码。)
import numpy as np
from matplotlib import pyplot as plt
fig, ax = plt.subplots()
rows = ['%d nodes' % x for x in (10, 30, 50, 75, 100)]
columns = ['TP', 'TN', 'FP', 'FN']
conf_data = np.array([[230, 847, 784, 208],
[156, 1240, 391, 282],
[146, 1212, 419, 292],
[130, 1148, 483, 308],
[122, 1173, 458, 316]])
normed_data = (conf_data - conf_data.min(axis=0, keepdims=True)) / conf_data.ptp(axis=0, keepdims=True)
fig.patch.set_visible(False)
ax.axis('off')
ax.axis('tight')
table = ax.table(cellText=conf_data,
rowLabels=rows,
colLabels=columns,
cellColours=plt.cm.GnBu(normed_data*0.8),
loc='center',
colWidths=[0.1 for x in columns])
table.scale(2, 2) # make table a little bit larger
fig.tight_layout()
plt.show()
在结果下方有两个不同的颜色图:左侧的“GnBu”具有介于 0 和 0.8 之间的标准值,右侧的“coolwarm”具有介于 0.1 和 0.9 之间的标准值
PS:另一种提高单元格文本和背景颜色对比度的方法是设置每个单元格的 alpha:
for cell in table._cells:
table._cells[cell].set_alpha(.6)
推荐阅读
- node.js - 本地存储节点插件事件以供以后回调
- sql - AWS Timestream / Grafana 查询
- flutter - 如何实现搜索功能?
- javascript - 为 d3.js 中的不同点添加多色渐变
- pdf - pdfbox 嵌入子集字体用于注释 - 第 2 部分
- java - 在创建 StorageManager 客户端时,得到“java.lang.NoSuchMethodError: reactor.netty.http.client.HttpClient.doAfterResponseSuccess”
- node.js - 找不到模块“fs-extra”npm
- postgresql - 如何更新然后插入具有唯一键约束的记录
- php - 在购物车和结帐中显示产品自定义字段 - WooCommerce
- javascript - 使用 Javascript 重新启动计时器