2015-11-20 28 views
11

Sto provando a creare una griglia utilizzando una funzione matplotlib come imshow.
Da questa matrice:Matricola: visualizza i valori degli array con imshow

[[ 1 8 13 29 17 26 10 4], 
[16 25 31 5 21 30 19 15]] 

Vorrei tracciare il valore come un colore e il valore del testo in sé (1,2, ...) sulla stessa griglia. Questo è quello che ho per il momento (posso tracciare solo il colore associato a ciascun valore):

from matplotlib import pyplot 
import numpy as np 

grid = np.array([[1,8,13,29,17,26,10,4],[16,25,31,5,21,30,19,15]]) 
print 'Here is the array' 
print grid 

fig1, (ax1, ax2)= pyplot.subplots(2, sharex = True, sharey = False) 
ax1.imshow(grid, interpolation ='none', aspect = 'auto') 
ax2.imshow(grid, interpolation ='bicubic', aspect = 'auto') 
pyplot.show() 

risposta

1

Se per qualsiasi motivo è necessario utilizzare un diversa misura da quello che è fornito naturalmente da imshow il seguente metodo (anche se più inventato) fa il lavoro:

enter image description here

size = 4 
data = np.arange(size * size).reshape((size, size)) 

# Limits for the extent 
x_start = 3.0 
x_end = 9.0 
y_start = 6.0 
y_end = 12.0 

extent = [x_start, x_end, y_start, y_end] 

# The normal figure 
fig = plt.figure(figsize=(16, 12)) 
ax = fig.add_subplot(111) 
im = ax.imshow(data, extent=extent, origin='lower', interpolation='None', cmap='viridis') 

# Add the text 
jump_x = (x_end - x_start)/(2.0 * size) 
jump_y = (y_end - y_start)/(2.0 * size) 
x_positions = np.linspace(start=x_start, stop=x_end, num=size, endpoint=False) 
y_positions = np.linspace(start=y_start, stop=y_end, num=size, endpoint=False) 

for y_index, y in enumerate(y_positions): 
    for x_index, x in enumerate(x_positions): 
     label = data[y_index, x_index] 
     text_x = x + jump_x 
     text_y = y + jump_y 
     ax.text(text_x, text_y, label, color='black', ha='center', va='center') 

fig.colorbar(im) 
plt.show() 

(valori aggiunti dopo che i dati) Se si vuole mettere altro tipo di dati e non necessariamente i valori che hai usato per l'immagine è possibile modificare lo script precedente nel modo seguente:

enter image description here

size = 4 
data = np.arange(size * size).reshape((size, size)) 
values = np.random.rand(size, size) 

# Limits for the extent 
x_start = 3.0 
x_end = 9.0 
y_start = 6.0 
y_end = 12.0 

extent = [x_start, x_end, y_start, y_end] 

# The normal figure 
fig = plt.figure(figsize=(16, 12)) 
ax = fig.add_subplot(111) 
im = ax.imshow(data, extent=extent, origin='lower', interpolation='None', cmap='viridis') 

# Add the text 
jump_x = (x_end - x_start)/(2.0 * size) 
jump_y = (y_end - y_start)/(2.0 * size) 
x_positions = np.linspace(start=x_start, stop=x_end, num=size, endpoint=False) 
y_positions = np.linspace(start=y_start, stop=y_end, num=size, endpoint=False) 

for y_index, y in enumerate(y_positions): 
    for x_index, x in enumerate(x_positions): 
     label = values[y_index, x_index] 
     text_x = x + jump_x 
     text_y = y + jump_y 
     ax.text(text_x, text_y, label, color='black', ha='center', va='center') 

fig.colorbar(im) 
plt.show() 
11

Si desidera ciclo sui valori in grid, e utilizzare ax.text per aggiungere l'etichetta per la trama.

Fortunatamente, per gli array 2D, numpy ha ndenumerate, che rende questo abbastanza semplice:

for (j,i),label in np.ndenumerate(grid): 
    ax1.text(i,j,label,ha='center',va='center') 
    ax2.text(i,j,label,ha='center',va='center') 

enter image description here

+0

tom; per curiosità: è un tale ciclo che usa 'ndenumerate' più veloce di un ciclo manuale? – Bart

+1

no, non la penso così (anche se potrebbe dipendere dalla dimensione di 'grid'). Mi piace solo la semplicità del codice, rispetto a 'per j nella gamma (grid.shape [0]): per i in gamma (grid.shape [1]): ax.text (i, j, griglia [ j, i]) ' – tom

+0

Posso farlo con un testo blu? –