[英]Matplotlib: How to increase colormap/linewidth quality in streamplot?
[英]How to increase color resolution in python matplotlib colormap
我正在制作2D numpy meshgrid的色彩映射:
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生成的色圖是粗糙的 - 它可以縮放8種顏色,即使我使用“N = 256”來表示RGB像素的數量。 將N增加到2048並沒有改變任何東西。 在相同數據上使用MatLab語言的圖可生成具有明顯更高顏色分辨率的色彩圖。 如何增加Python中映射的顏色數量?
謝謝!
Warren Weckesser的評論絕對有效,可以為您提供高分辨率的圖像。 我在下面的例子中實現了他的想法。
關於使用contourf()
,我不確定這是否是版本相關問題,但在最新版本中, contourf()
沒有N
的kwarg。
正如您在文檔中看到的,您希望使用N
作為arg(在語法中: contourf(X,Y,Z,N)
)來指定要繪制的級別數而不是RGB像素數。 contourf()
繪制填充的輪廓,分辨率取決於要繪制的級別數。 你的N=256
將不會做任何事情, contourf()
將自動選擇7個級別 。
從官方示例中修改以下代碼,比較具有不同N
分辨率。 如果存在版本問題,此代碼使用python 3.5.2; matplotlib 1.5.3
給出以下圖表python 3.5.2; matplotlib 1.5.3
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.