【问题标题】:matplotlib - Draw a heatmap/pixelmap with ability to edit individual pixel colours (different colormaps by row)matplotlib - 绘制能够编辑单个像素颜色的热图/像素图(按行显示不同的颜色图)
【发布时间】:2016-05-10 08:29:23
【问题描述】:

我正在尝试使用 matplotlib 绘制矩阵的热图/像素图表示。我目前有以下代码,可根据需要为我提供像素图(改编自 Heatmap in matplotlib with pcolor?):

import matplotlib.pyplot as plt
import numpy as np

column_labels = list('ABCD')
row_labels = list('0123')

data = np.array([[0,1,2,0],
                     [1,0,1,1],
                     [1,2,0,0],
                     [0,0,0,1]])

fig, ax = plt.subplots()
heatmap = ax.pcolor(data, cmap=plt.cm.Blues)

# put the major ticks at the middle of each cell
ax.set_xticks(np.arange(data.shape[0])+0.5, minor=False)
ax.set_yticks(np.arange(data.shape[1])+0.5, minor=False)

# want a more natural, table-like display
ax.invert_yaxis()
ax.xaxis.tick_top()

ax.set_xticklabels(row_labels, minor=False)
ax.set_yticklabels(column_labels, minor=False)

ax.yaxis.grid(True, which='minor', linestyle='-', color='k', linewidth = 0.3, alpha = 0.5)
ax.xaxis.grid(True, which='minor', linestyle='-', color='k', linewidth = 0.3, alpha = 0.5)

# Set the location of the minor ticks to the edge of pixels for the x grid
minor_locator = AutoMinorLocator(2)
ax.xaxis.set_minor_locator(minor_locator)

# Lets turn off the actual minor tick marks though
for tickmark in ax.xaxis.get_minor_ticks():
    tickmark.tick1On = tickmark.tick2On = False

# Set the location of the minor ticks to the edge of pixels for the y grid
minor_locator = AutoMinorLocator(2)
ax.yaxis.set_minor_locator(minor_locator)

# Lets turn off the actual minor tick marks though
for tickmark in ax.yaxis.get_minor_ticks():
    tickmark.tick1On = tickmark.tick2On = False

plt.show()

这给出了以下情节:

但是我想扩展它,以便在鼠标单击时我可以在像素图中以绿色突出显示“行”,例如如果用户选择了“C”行,我会选择(我很欣赏绿色高亮对于值为 0 的像素不清晰):

我知道如何处理鼠标事件,但我不确定如何修改像素图中单行的颜色。如果我可以为像素图的各个像素设置标签以在鼠标单击时检索,而不是使用鼠标 x/y 位置来索引标签列表,这也会有所帮助。

【问题讨论】:

    标签: matplotlib highlight heatmap


    【解决方案1】:

    在这个问题的帮助下,我找到了自己的问题: Plotting of 2D data : heatmap with different colormaps.

    代码如下,cmets应该清楚地解释所采取的步骤。

    import matplotlib.pyplot as plt
    import numpy as np
    from numpy.ma import masked_array
    import matplotlib.cm as cm
    from matplotlib.ticker import AutoMinorLocator
    
    column_labels = list('ABCD')
    row_labels = list('0123')
    data = np.array([[0,1,2,0],
                     [1,0,1,1],
                     [1,2,0,0],
                     [0,0,0,1]])
    
    fig, ax = plt.subplots()
    
    # List to keep track of handles for each pixel row
    pixelrows = []
    
    # Lets create a normalizer for the whole data array
    norm = plt.Normalize(vmin = np.min(data), vmax = np.max(data))
    
    # Let's loop through and plot each pixel row
    for i, row in enumerate(data):
        # First create a mask to ignore all others rows than the current
        zerosarray = np.ones_like(data)
        zerosarray[i, :] = 0
    
        plotarray = masked_array(data, mask=zerosarray)
    
        # If we are not on the 3rd row down let's use the red colormap
        if i != 2:
            pixelrows.append(ax.matshow(plotarray, norm=norm, cmap=cm.Reds))
    
        # Otherwise if we are at the 3rd row use the green colormap
        else:
            pixelrows.append(ax.matshow(plotarray, norm=norm, cmap=cm.Greens))
    
    # put the major ticks at the middle of each cell
    ax.set_xticks(np.arange(data.shape[0]), minor=False)
    ax.set_yticks(np.arange(data.shape[1]), minor=False)
    
    # want a more natural, table-like display
    ax.xaxis.tick_top()
    
    ax.set_xticklabels(row_labels, minor=False)
    ax.set_yticklabels(column_labels, minor=False)
    
    ax.yaxis.grid(True, which='minor', linestyle='-', color='k', linewidth = 0.3, alpha = 0.5)
    ax.xaxis.grid(True, which='minor', linestyle='-', color='k', linewidth = 0.3, alpha = 0.5)
    
    # Set the location of the minor ticks to the edge of pixels for the x grid
    minor_locator = AutoMinorLocator(2)
    ax.xaxis.set_minor_locator(minor_locator)
    
    # Lets turn of the actual minor tick marks though
    for tickmark in ax.xaxis.get_minor_ticks():
        tickmark.tick1On = tickmark.tick2On = False
    
    # Set the location of the minor ticks to the edge of pixels for the y grid
    minor_locator = AutoMinorLocator(2)
    ax.yaxis.set_minor_locator(minor_locator)
    
    # Lets turn of the actual minor tick marks though
    for tickmark in ax.yaxis.get_minor_ticks():
        tickmark.tick1On = tickmark.tick2On = False
    
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-10
      • 1970-01-01
      • 2013-05-07
      相关资源
      最近更新 更多