【问题标题】:Displaying row sum in a secondary colorbar while plotting matrix绘制矩阵时在辅助颜色栏中显示行总和
【发布时间】:2020-11-21 22:27:12
【问题描述】:

我正在使用matshow 绘制一个矩阵。我有一个数组

sum = np.sum(A, axis=0)   

我想将存储在sum 中的值显示为辅助图例。

import numpy as np
import matplotlib.pyplot as plt
plt.ion()
A = np.arange(0,100).reshape(10,10)
plt.matshow(A)   
plt.colorbar()

我想知道如何在上述代码中添加辅助图例。

例如预期输出:

右侧的图例是自动创建的。通过次要图例,我的意思是底部显示的色标。例如,这可能对应于每列中值的总和(y 轴条目)。

【问题讨论】:

  • 请提供A 矩阵。
  • 嗨@MichaelSidorov 我想你错过了。 A = np.arange(0,100).reshape(10,10) 出现在代码块中。
  • 嗨! @Natasha 你所说的次要传奇是什么意思?你有一个显示你想要的情节的例子吗?
  • 你好@tomjn 你能检查我的编辑吗?
  • 有什么建议吗?

标签: python-3.x numpy matplotlib matrix legend


【解决方案1】:

我认为您需要重新考虑为什么需要在颜色栏上表示列总和。因为颜色条上的颜色代表矩阵中的一个值。连列总和的值都不包括在内,用彩条显示列总和是什么意思?

import numpy as np
import matplotlib.pyplot as plt

plt.ion()

fig, ax = plt.subplots(figsize=(4,4))

A = np.arange(0,100).reshape(10,10)

column_sum = A.sum(axis=0)

im2 = ax.matshow(np.expand_dims(column_sum, axis=0))
# Override upper matrix
im = ax.matshow(A)

fig.colorbar(im2, orientation="horizontal") # Note: the color in this color bar represents value in im2, im2 is override by im. To distinguish it from im, you may interested in https://matplotlib.org/gallery/images_contours_and_fields/custom_cmap.html
fig.colorbar(im)

【讨论】:

  • 非常感谢。The value of column sum is even not included你能解释一下吗?
  • 您可以将颜色条视为map。矩阵中的每种颜色都可以在右侧颜色栏中找到对应的值。但是,对于底部列总和颜色条,您无法在矩阵中找到与其颜色对应的颜色。
  • 我想没关系,因为我对感兴趣的行/列的行/列条目的总和感兴趣,而不是第二个图例中矩阵本身的值。
猜你喜欢
  • 1970-01-01
  • 2017-04-14
  • 2011-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多