実現したいこと
「該当のソースコード」はこのページを元にしたものですが、私の質問内容の具体例として挙げただけで、深い意味はありません。6、7行目のデータはこちらのCase BのCase 5: low particle density and small particleです。
「該当のソースコード」を実行すると、以下の図が出てきます。(test1.png)
ここで、等高線を描きたい量(displacement)は整数値しかとらないことに気づいたため、等高線をcontourfからcontourに変更しようと思いました。(48行目のcontourf→contour)
その実行結果が以下の図です。(test2.png)
test2.pngの色が少し見づらいので、それを設定する方法を教えてください。
test1.pngは4色、test2.pngは5色必要なので、そもそもtest1.pngから自分で1色増やさなければいけません。
例えば、test2.pngの色を、上から順に紫、赤、黄、緑、青と設定したいです。
該当のソースコード
python
1import matplotlib.pyplot as plt 2import numpy as np 3from skimage.io import imread 4from scipy.signal import correlate 5 6path1 = r'.\B005_1.tif' 7path2 = r'.\B005_2.tif' 8 9a = imread(path1) 10b = imread(path2) 11 12win_size = 32 13 14def vel_field(curr_frame, next_frame, win_size): 15 ys = np.arange(0, curr_frame.shape[0], win_size) 16 xs = np.arange(0, curr_frame.shape[1], win_size) 17 dys = np.zeros((len(ys), len(xs))) 18 dxs = np.zeros((len(ys), len(xs))) 19 20 for iy, y in enumerate(ys): 21 for ix, x in enumerate(xs): 22 int_win = curr_frame[y : y + win_size, x : x + win_size] 23 search_win = next_frame[y : y + win_size, x : x + win_size] 24 cross_corr = correlate( 25 search_win - search_win.mean(), int_win - int_win.mean(), method="fft" 26 ) 27 dys[iy, ix], dxs[iy, ix] = ( 28 np.unravel_index(np.argmax(cross_corr), cross_corr.shape) 29 - np.array([win_size, win_size]) 30 + 1 31 ) 32 33 # draw velocity vectors from the center of each window 34 ys = ys + win_size / 2 35 xs = xs + win_size / 2 36 37 return xs, ys, dxs, dys 38 39xs, ys, dxs, dys = vel_field(a, b, win_size) 40 41x = xs 42y = ys[::-1] 43x, y = np.meshgrid(x, y) 44z = dys 45 46levels1 = np.linspace(-10,10,5) 47levels2= np.linspace(-10,10,5) 48ax2 = plt.contourf(x,y,z,cmap='jet',levels=levels2) 49ax2.set_clim(vmin=min(levels2), vmax=max(levels2)) 50cb = plt.colorbar(ticks=levels1) 51cb.set_label("y-direction displacement[pixel]", fontsize=16) 52cb.ax.tick_params(labelsize=12) 53plt.xlabel("x[pixel]", fontsize=12) 54plt.ylabel("y[pixel]", fontsize=12) 55plt.xticks(fontsize=12) 56plt.yticks(fontsize=12) 57plt.savefig('test1.png')
試したこと
ネットで色々調べましたが、よく分かりませんでした。
「該当のソースコード」の48行目を
ax2 = plt.contour(x,y,z,cmap='jet',levels=levels2,colors=['purple', 'r', 'y', 'g', 'b'])
としてみましたが、下記のようなエラーメッセージが出てしまいました。
Traceback (most recent call last): File "・・・\testCV228-2.py", line 69, in <module> ax2 = plt.contour(x,y,z,cmap='jet',levels=levels2,colors=['purple', 'r', 'y', 'g', 'b']) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "・・・\Python311\Lib\site-packages\matplotlib\pyplot.py", line 2527, in contour __ret = gca().contour( ^^^^^^^^^^^^^^ File "・・・\Python311\Lib\site-packages\matplotlib\__init__.py", line 1461, in inner return func(ax, *map(sanitize_sequence, args), **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "・・・\Python311\Lib\site-packages\matplotlib\axes\_axes.py", line 6449, in contour contours = mcontour.QuadContourSet(self, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "・・・\Python311\Lib\site-packages\matplotlib\contour.py", line 756, in __init__ raise ValueError('Either colors or cmap must be None') ValueError: Either colors or cmap must be None

回答1件
あなたの回答
tips
プレビュー