2015-11-20 11 views
11

के साथ प्रदर्शन सरणी मान प्रदर्शित करें मैं matplotlib फ़ंक्शन imshow जैसे फ़ंक्शन का उपयोग करके ग्रिड बनाने की कोशिश कर रहा हूं।
इस सरणी से:Matplotlib: imshow

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

मैं एक ही ग्रिड पर एक रंग और पाठ मान ही (1,2, ...) के रूप में मूल्य प्लॉट करने के लिए करना चाहते हैं।

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

उत्तर

1

किसी भी कारण से आप एक है कि से एक अलग हद उपयोग करने के लिए है, तो imshow द्वारा स्वाभाविक रूप से प्रदान की गई विधि (यहां तक ​​कि अगर अधिक प्रतिस्पर्धा हुई) नौकरी करता है:

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

आप डेटा की अन्य प्रकार और जरूरी नहीं कि मानों कि आप छवि के लिए इस्तेमाल किया आप निम्नलिखित तरीके से ऊपर स्क्रिप्ट को संशोधित कर दिया चाहते हैं (डेटा के बाद जोड़ा मान):

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

आप grid में मूल्यों पर लूप करना चाहते हैं, और साजिश करने के लिए लेबल जोड़ने के लिए ax.text का उपयोग करें: यह है कि मैं क्या पल के लिए है (मैं केवल प्रत्येक मान से जुड़े रंग प्लॉट कर सकते हैं) है।

सौभाग्य से, 2 डी सरणियों के लिए, numpyndenumerate है, जो इस काफी सरल बनाता है:

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

टॉम; जिज्ञासा से बाहर: मैनुअल लूप की तुलना में 'ndenumerate' का उपयोग करके ऐसा लूप है? – Bart

+1

नहीं, मुझे ऐसा नहीं लगता (हालांकि यह 'ग्रिड' के आकार पर निर्भर हो सकता है)। मैं श्रेणी के लिए जम्मू-रेंज (ग्रिड.शिप [0]) की तुलना में कोड की सादगी की तरह, रेंज में (ग्रिड.शिप [1]): ax.text (i, j, ग्रिड [ जे, मैं]) – tom

+0

क्या मैं इसे नीले पाठ के साथ कर सकता हूं? –

संबंधित मुद्दे