1

matplotlib でピクセル値が「-1」以外の値になるレイ トレース パスをプロットしようとしています。つまり、4 つの光線パスを表す次の 2D 配列があります。光線が交差する各ピクセルにはランダムな値があります。これらの交差したピクセルを除いて、残りは「-1」です。値「-1」を白色または非表示 (存在しない) で表示したい。それはどのように可能ですか?

import numpy as np
import scipy as sp
import pylab as pl

M = np.array([[ 0. , -1., -1., -1., -1., -1.],
          [ 0.25, -1.,-1.,-1.,-1.,-1.],
          [ 0.25, -1., -1., -1.,-1.,-1.],
          [ 0.22, -1., -1., -1., -1.,-1.],
          [ 0.16, -1., -1., -1., -1.,-1.],
          [ 0.16, -1., -1., -1., -1.,-1.],
          [ 0.13, -1., -1., -1., -1.,-1.],
          [ 0.10, -1., -1., -1., -1.,-1.],
          [-1., 0.06, 0.14, 0.087, 0.079,0.],
          [ 0., 0.16, 0.10, 0.15, 0.16, 0.],
          [-1., -1., 0., 0.004,-1., -1.]])

pl.subplot(111)
pl.imshow(M, origin='lower', interpolation='nearest')
pl.show()
4

2 に答える 2

4

これを行う別の方法は、カラー マップのset_underset_overおよびプロパティを使用することです(ドキュメント) 。set_bad

from copy import copy

# normalize data between vmin and vmax
my_norm = matplotlib.colors.Normalize(vmin=.25, vmax=.75, clip=False)
# clip=False is important, if clip=True, then the normalize function
# clips out of range values to 0 or 1 which defeats what we want to do here.

my_cmap = copy(cm.get_cmap('gray')) # make a copy so we don't mess up system copy
my_cmap.set_under('r', alpha=.5) # make locations over vmax translucent red
my_cmap.set_over('w', alpha=0)   # make location under vmin transparent white
my_cmap.set_bad('g')             # make location with invalid data green

test_data = np.random.rand(10, 10) # some random data between [0, 1]
test_data[5, 5] = np.nan           # add one NaN
# plot!
imshow(test_data, norm=my_norm, cmap=my_cmap, interpolation='nearest')

出力例

matplotlibこれは、手作業でマスク配列を作成するよりも優れた方法であり、3 つの異なる条件の色を個別に明示的に設定できると私は主張します。

于 2013-10-11T21:33:07.603 に答える
2

マスク配列を使用できます。http://docs.scipy.org/doc/numpy/reference/generated/numpy.ma.masked_where.html

>>> masked = np.ma.masked_where(M==-1,M)
>>> pl.subplot(111)
>>> pl.imshow(masked, origin='lower', interpolation='nearest')
>>> pl.show()

ここに画像の説明を入力

于 2013-10-11T20:24:49.377 に答える