我想用pcolormesh用喷射色图绘制数据。我想通过将数据散布到间隔中并为每个间隔分配颜色来控制颜色拉伸。然后我要用红色绘制最后一个间隔(通常在“ redvalue”以上的任何值,通常为30)。
在Matlab中,我使用的数据间隔与颜色图中的元素一样多。最后一个间隔在颜色数组的最后一个元素中分配了颜色(“最闪亮”的红色)。
range = redvalue - datamin;
colours = colormap('hsv');
count = max(size(colours));
localrange = range/count;
localmin = datamin-localrange;
localmax = datamin;
% Plot the first n - 1 number of intervals
for j = 1 : count - 1
localmin = localmin + localrange;
localmax = localmax + localrange;
ind = find(data >= localmin & data < localmax)
p = plot3(x(ind),y(ind),data(ind), '.', 'Color', colours(j,:), 'MarkerSize', 5, 'MarkerFaceColor', colours(j,:));
end
%% Now plot the final colour to points >= 30
ind = find( data >= redvalue ); % make these points red
p = plot3(x(ind),y(ind),data(ind), '.', 'Color', colours(end,:), 'MarkerSize', 5, 'MarkerFaceColor', colours(end,:));
我的代码。我告诉cmap在查找表中仅创建7个条目,而不是使用其默认的颜色图条目数。
rng = redvalue - radmin
n_colours = 7
localrng = rng/n_colours
localmin = radmin - localrng
localmax = radmin
cmp = plt.get_cmap('jet', n_colours)
for index in range(1,n_colours - 1):
localmin = localmin + localrng
localmax = localmax + localrng
row,col = np.where(np.logical_and(rad >= localmin, rad < localmax))
plt.pcolormesh(x1[row][col],y1[row][col],rad[row][col], cmap = cmp(index), vmin = radmin, vmax = radmax, edgecolors = 'none')
这并不能很好地翻译成Python。我不知道我对数据矩阵元素的索引是否正常工作,因为直接错误与颜色图有关。我想要的甚至有可能吗?我可以从cmap对象中获取一种颜色,然后一次将它们提供给pcolormesh一种颜色吗?
Traceback (most recent call last):
File "C:\UserData\Documents\Stuff\tensorflow\OpenGDF2_2.py", line 199, in <module>
PlotRad(data, 30, 50)
File "C:\UserData\Documents\Stuff\tensorflow\OpenGDF2_2.py", line 165, in PlotRad
plt.pcolormesh(x1[row][col],y1[row][col],rad[row][col], cmap = cmp(index), vmin = radmin, vmax = radmax, edgecolors = 'none')
File "C:\Users\keepit20\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\pyplot.py", line 2773, in pcolormesh
**({"data": data} if data is not None else {}), **kwargs)
File "C:\Users\keepit20\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\__init__.py", line 1810, in inner
return func(ax, *args, **kwargs)
File "C:\Users\keepit20\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\axes\_axes.py", line 6002, in pcolormesh
collection.set_cmap(cmap)
File "C:\Users\keepit20\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\cm.py", line 342, in set_cmap
cmap = get_cmap(cmap)
File "C:\Users\keepit20\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\cm.py", line 182, in get_cmap
% (name, ', '.join(sorted(cmap_d))))
ValueError: Colormap (0.0, 0.16666666666666666, 1.0, 1.0) is not recognized. Possible values are: Accent, Accent_r, Blues, Blues_r, BrBG, BrBG_r, BuGn, BuGn_r, BuPu, BuPu_r, CMRmap, CMRmap_r, Dark2, Dark2_r, GnBu, GnBu_r, Greens, Greens_r, Greys, Greys_r, OrRd, OrRd_r, Oranges, Oranges_r, PRGn, PRGn_r, Paired, Paired_r, Pastel1, Pastel1_r, Pastel2, Pastel2_r, PiYG, PiYG_r, PuBu, PuBuGn, PuBuGn_r, PuBu_r, PuOr, PuOr_r, PuRd, PuRd_r, Purples, Purples_r, RdBu, RdBu_r, RdGy, RdGy_r, RdPu, RdPu_r, RdYlBu, RdYlBu_r, RdYlGn, RdYlGn_r, Reds, Reds_r, Set1, Set1_r, Set2, Set2_r, Set3, Set3_r, Spectral, Spectral_r, Wistia, Wistia_r, YlGn, YlGnBu, YlGnBu_r, YlGn_r, YlOrBr, YlOrBr_r, YlOrRd, YlOrRd_r, afmhot, afmhot_r, autumn, autumn_r, binary, binary_r, bone, bone_r, brg, brg_r, bwr, bwr_r, cividis, cividis_r, cool, cool_r, coolwarm, coolwarm_r, copper, copper_r, cubehelix, cubehelix_r, flag, flag_r, gist_earth, gist_earth_r, gist_gray, gist_gray_r, gist_heat, gist_heat_r, gist_ncar, gist_ncar_r, gist_rainbow, gist_rainbow_r, gist_stern, gist_stern_r, gist_yarg, gist_yarg_r, gnuplot, gnuplot2, gnuplot2_r, gnuplot_r, gray, gray_r, hot, hot_r, hsv, hsv_r, inferno, inferno_r, jet, jet_r, magma, magma_r, nipy_spectral, nipy_spectral_r, ocean, ocean_r, pink, pink_r, plasma, plasma_r, prism, prism_r, rainbow, rainbow_r, seismic, seismic_r, spring, spring_r, summer, summer_r, tab10, tab10_r, tab20, tab20_r, tab20b, tab20b_r, tab20c, tab20c_r, terrain, terrain_r, twilight, twilight_r, twilight_shifted, twilight_shifted_r, viridis, viridis_r, winter, winter_r
感谢JODY KLYMAK的解决方案:使用colors.BoundaryNorm构建离散的边界数组。记住要使用相同数量的离散量对色图进行get_cmap。
rng = redvalue - radmin
n_colours = 7
localrng = rng/n_colours
localmin = radmin - localrng
localmax = radmin
cmp = plt.get_cmap('jet', n_colours)
bounds = localmax
bounds = np.asarray(bounds)
for index in range(1,n_colours - 1):
localmin = localmin + localrng
localmax = localmax + localrng
bounds = np.append(bounds, localmax)
bounds = np.append(bounds, radmax)
norm = colors.BoundaryNorm(boundaries=bounds, ncolors = n_colours)
plt.pcolormesh(x1,y1,rad, norm = norm, cmap = cmp, edgecolors = 'none')
plt.axis([x1.min(), x1.max(), y1.min(), y1.max()])
plt.colorbar()
plt.show()
答案 0 :(得分:0)
感谢JODY KLYMAK的解决方案:使用colors.BoundaryNorm构建离散的边界数组。并记住使用相同数量的离散间隔get_cmap您的颜色图。
rng = redvalue - radmin
n_colours = 7
localrng = rng/n_colours
localmin = radmin - localrng
localmax = radmin
cmp = plt.get_cmap('jet', n_colours)
bounds = localmax
bounds = np.asarray(bounds)
for index in range(1,n_colours - 1):
localmin = localmin + localrng
localmax = localmax + localrng
bounds = np.append(bounds, localmax)
bounds = np.append(bounds, radmax)
norm = colors.BoundaryNorm(boundaries=bounds, ncolors = n_colours)
plt.pcolormesh(x1,y1,rad, norm = norm, cmap = cmp, edgecolors = 'none')
plt.axis([x1.min(), x1.max(), y1.min(), y1.max()])
plt.colorbar()
plt.show()