2017-04-11 21 views
0

我需要一個程序來繪製矩陣中的斜線,平行線。這是我的python2代碼:python2中的matplotlib呈現問題

import numpy as np 
from pylab import imshow, show 

X, Y, m, s = 500, 1000, 8, 200   
x = np.zeros((Y, X)) 
c = -m * Y    
for i in xrange(Y): 
    for j in xrange(X):    
     k = 0       
     while k * s < 2 * m * Y: 
      if i == m * j + c + s * k: 
       x[i][j] = 1 
      k += 1 
imshow(x, aspect='auto'); show() 

產生的影像是: 沒有理由爲什麼在塊之間應該丟失。我放大了他們失蹤的第一個區域,發現點確實存在:enter image description here

爲了確保這些點應該看起來像這樣,下面是該區域之後,原始圖像的位置渲染要點:enter image description here

這是渲染問題還是我做錯了什麼?

+0

'np.rollaxis(np.array一起使用插值( np.where(x == 1)),1)'given'array [[0,0], [0,25], [0,50], ..., [992,449] , [992,474], [992,499]])'。這是你的數據應該是什麼樣子? – mhoff

回答

0

問題是,將1000個像素值放置到370個屏幕像素時會出現什麼結果。每個像素大概有3個值,兩個藍色,一個黃色。最終像素應具有哪種顏色?

你可以做什麼是你的圖像上用顏色表,其顯示的任何值相同的顏色大於0

import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.colors 

X, Y, m, s = 500, 1000, 8, 200   
x = np.zeros((Y, X)) 
c = -m * Y 

for i in xrange(Y): 
    for j in xrange(X):    
     k = 0       
     while k * s < 2 * m * Y: 
      if i == m * j + c + s * k: 
       x[i][j] = 1 
      k += 1 

clist = ((0, "w"), (1./255, "crimson"), (1, "crimson")) 
cmap = matplotlib.colors.LinearSegmentedColormap.from_list("n",clist) 
plt.imshow(x, aspect='auto', cmap=cmap,interpolation="bilinear"); 
plt.colorbar() 
plt.show() 

enter image description here