【问题标题】:All heatmap colorbars displaying on last figure in MatplotlibMatplotlib 中最后一个图上显示的所有热图颜色条
【发布时间】:2018-06-21 03:49:27
【问题描述】:

我在 Matplotlib 中绘制多个热图时遇到了问题(Python 3.6.0 以防万一)。

我有一个函数可以绘制一些数据的热图,每个热图都在一个单独的图中。当我为不同的数据数组运行此函数时,热图在各自的图中都绘制得很好,但由于某种原因,它们的所有颜色条都显示在最近绘制的热图的图中,如下图链接所示。

热图错误

请注意,当我尝试在没有该功能的情况下手动绘制热图时,此行为仍然存在。另请注意,颜色条不仅显示在最近绘制的图形上,而且仅显示在最近绘制的包含热图的图形上。例如,如果我稍后绘制线图,则颜色条不会显示在此线图上,而只会显示在最后一个热图上。

这是一个最小的工作示例:

import numpy as np
from pylab import *

# Function
f1 = lambda X, Y: X*X + Y*Y
f2 = lambda X, Y: X*X - Y*Y
f3 = lambda X, Y: X*Y - Y

# Grid on which function is to be evaluated
x = np.arange(0, 100, 1)
y = np.arange(0, 100, 1)
Xaxis = x[:, None]
Yaxis = y[None, :]

# Evaluate functions and create labels for plotting
Z1 = f1(Xaxis, Yaxis)
l1 = ['F1', '1']
Z2 = f2(Xaxis, Yaxis)
l2 = ['F2', '2']
Z3 = f3(Xaxis, Yaxis)
l3 = ['F3', '3']


# Function to plot heatmaps
def DoPlot(fig, fun, label):
    title = label[0]
    subscript = label[1]
    ax = fig.add_subplot(111)
    im = ax.imshow(fun, cmap=cm.viridis, interpolation='nearest', 
                   aspect='auto')
    ax.set_ylabel('Y')
    ax.set_xlabel('X')
    cbar = colorbar(im)
    cbar.set_label(r'$Z_{{}}$'.format(subscript))
    fig.suptitle(title)
    fig.tight_layout()


# Plot the heatmaps

fig1 = figure()
fig2 = figure()
fig3 = figure()

DoPlot(fig1, Z1, l1)
DoPlot(fig2, Z2, l2)
DoPlot(fig3, Z3, l3)

show()

(是的,我确实意识到from pylab import * 不是最佳做法。这只是为了方便。)

非常感谢您对此事的任何帮助。

【问题讨论】:

  • 使用 fig.colorbar(...) (从 pylab 更新此代码将是微不足道的,顺便说一句)
  • @PaulH 非常感谢,效果很好。您能否将您的评论作为答案,以便我选择它?

标签: python matplotlib plot heatmap


【解决方案1】:

这里的技巧是直接对对象进行操作。所以不要使用colorbar,而是使用fig.colorbar

正如您在问题中提到的那样,我们强烈反对 from pylab import *。将代码升级到面向对象的接口很简单:

import numpy as np
from matplotlib import pyplot

# Function
f1 = lambda X, Y: X*X + Y*Y
f2 = lambda X, Y: X*X - Y*Y
f3 = lambda X, Y: X*Y - Y

# Grid on which function is to be evaluated
x = np.arange(0, 100, 1)
y = np.arange(0, 100, 1)
Xaxis = x[:, None]
Yaxis = y[None, :]

# Evaluate functions and create labels for plotting
Z1 = f1(Xaxis, Yaxis)
l1 = ['F1', '1']
Z2 = f2(Xaxis, Yaxis)
l2 = ['F2', '2']
Z3 = f3(Xaxis, Yaxis)
l3 = ['F3', '3']


# Function to plot heatmaps
def DoPlot(fig, fun, label):
    title = label[0]
    subscript = label[1]
    ax = fig.add_subplot(111)
    im = ax.imshow(fun, cmap=pyplot.cm.viridis, interpolation='nearest', 
                   aspect='auto')
    ax.set_ylabel('Y')
    ax.set_xlabel('X')
    cbar = fig.colorbar(im)  # change: use fig.colorbar
    cbar.set_label(r'$Z_{{}}$'.format(subscript))
    fig.suptitle(title)
    fig.tight_layout()


# Plot the heatmaps
## change: use the pyplot function
fig1 = pyplot.figure()
fig2 = pyplot.figure()
fig3 = pyplot.figure()

DoPlot(fig1, Z1, l1)
DoPlot(fig2, Z2, l2)
DoPlot(fig3, Z3, l3)

pyplot.show()  ## change

您还可以使用简单的for 循环来减少代码重复性,但这超出了本问题的范围。

【讨论】:

    猜你喜欢
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-06
    • 2021-01-19
    • 2018-04-09
    • 2022-07-05
    • 1970-01-01
    相关资源
    最近更新 更多