2017-06-29 3 views
2

での色解像度を上げるために、私は2D numpyのの関数meshgridのカラーマップを作っています:は、どのようにPythonのmatplotlibのカラーマップ

X, Y = np.meshgrid(fields, frequencies) 
cs = ax.contourf(X, Y, fields_freqs_abs_grid, cmap="viridis", N=256) 

色でプロットされているfields_freqs_abs_gridの値は、すでに対数スケールされています。

pythonのmatplotlibで生成されるカラーマップは粗いです - RGBピクセルの数に "N = 256"を使用しても、8色以上のスケールになります。 Nを2048に増やしても何も変わらなかった。同じデータに対してMatLab言語を使用するプロットは、色解像度が大幅に高いカラーマップを生成します。 Pythonでマッピングされる色の数を増やすにはどうすればよいですか? enter image description here

しかし、私は結果になりたい::

結果がenter image description here

ありがとうございました!

+2

は 'imshow'の代わりに、' contourf'を使用してみてください。 X軸とY軸の範囲を設定するには、「extent」引数を使用します。 –

答えて

1

​​は確かに動作し、高解像度の画像を表示できます。私は彼の考えを以下の例で実装しました。

contourf()を使用するに関しては、私はこれは、バージョン依存の問題であるかどうかわからないんだけど、最新バージョンでは、 contourf()Nためkwargを持っていません。

文書でわかるように、Nをarg(構文:contourf(X,Y,Z,N))として使用して、RGBピクセルの数ではなくプロットするレベルの数を指定します。 contourf()は塗りつぶされた輪郭を描き、解像度は描画するレベルの数によって決まります。 N=256は何もしません。contourf()は自動的に7 levelsを選択します。

次のコードは公式exampleから変更され、解像度が異なるNと比較されています。バージョンの問題がある場合には、このコードはpython 3.5.2; matplotlib 1.5.3と、次のプロットを与える:

import numpy as np 
import matplotlib.pyplot as plt 

delta = 0.025 

x = y = np.arange(-3.0, 3.01, delta) 
X, Y = np.meshgrid(x, y) 
Z1 = plt.mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0) 
Z2 = plt.mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1) 
Z = 10 * (Z1 - Z2) 

fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2) 
fig.set_size_inches(8, 6) 

# Your code sample 
CS1 = ax1.contourf(X, Y, Z, cmap="viridis", N=256) 
ax1.set_title('Your code sample') 
ax1.set_xlabel('word length anomaly') 
ax1.set_ylabel('sentence length anomaly') 
cbar1 = fig.colorbar(CS1, ax=ax1) 

# Contour up to N=7 automatically-chosen levels, 
# which should give the same as your code. 
N = 7 
CS2 = ax2.contourf(X, Y, Z, N, cmap="viridis") 
ax2.set_title('N=7') 
ax2.set_xlabel('word length anomaly') 
ax2.set_ylabel('sentence length anomaly') 
cbar2 = fig.colorbar(CS2, ax=ax2) 

# Contour up to N=100 automatically-chosen levels. 
# The resolution is still not as high as using imshow(). 
N = 100 
CS3 = ax3.contourf(X, Y, Z, N, cmap="viridis") 
ax3.set_title('N=100') 
ax3.set_xlabel('word length anomaly') 
ax3.set_ylabel('sentence length anomaly') 
cbar3 = fig.colorbar(CS3, ax=ax3) 

IM = ax4.imshow(Z, cmap="viridis", origin='lower', extent=(-3, 3, -3, 3)) 
ax4.set_title("Warren Weckesser's idea") 
ax4.set_xlabel('word length anomaly') 
ax4.set_ylabel('sentence length anomaly') 
cbar4 = fig.colorbar(IM, ax=ax4) 

fig.tight_layout() 
plt.show() 

enter image description here

関連する問題