2016-02-01 36 views
0

我試圖用matplotlib繪製矩陣的熱圖/像素圖表示。目前,我有下面的代碼根據需要(改編自Heatmap in matplotlib with pcolor?),它給了我pixelmap:matplotlib - 繪製一個能夠編輯單個像素顏色的熱圖/像素圖(按行排列不同的顏色圖)

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):

我知道如何處理鼠標事件,但我不知道如何修改pixelmap中單個行的顏色。如果我可以爲像素圖的單個像素設置標籤以便在鼠標單擊時檢索,這與使用鼠標x/y位置索引標籤列表相反,這也將有所幫助。

回答

0

我已經想出了我自己的問題,在這個問題的幫助下: Plotting of 2D data : heatmap with different colormaps

代碼如下,註釋應該清楚地說明所採取的步驟。

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()