Matlab, Python: исправление цветовой карты к указанным значениям

1

Это простая, но общая задача, требуемая при попытке исправить цветную карту в соответствии с 2D-матрицей значений. Чтобы продемонстрировать рассмотрение проблемы в Matlab, решение не обязательно должно быть в Matlab (т.е. Представленный здесь код предназначен только для демонстрационной цели).


x = [0,1,2; 3,4,5; 6,7,8];
imagesc(x)
axis square
axis off
So the output is as:
Изображение 174551
when some values change to over the maximum value it happens like:

x = [0,1,2; 3,4,5; 6,7,18];
which looks logical but makes problems when we wish to compare/trace elements in two maps. Since the colormap association is changed it is almost impossible to find an individual cell for comparison/trace etc. Изображение 174551
The solution I implemented is to mask the matrix as:

x = [0,1,2; 3,4,5; 6,7,18];
m = 8;
x(x>=m) = m;
which works perfectly.
Since the provided code requires searching/filtering (extra time consuming!) I wonder if there is a general/more efficient way for this job to be implemented in Matlab, Python etc?

One of the cases that this issue occurs is when we have many simulations sequentially and wish to make a sense-making animation of the progress; in this case each color should keep its association fixed.

Теги:
masking
color-mapping

3 ответа

2
Лучший ответ

Индексирование довольно быстро, поэтому я не думаю, что вам нужно беспокоиться.

Однако, в Matlab, вы можете передать в clims аргумент imagesc:

imagesc(x,[0 8]);

Это отображает все значения выше 8 в верхний цвет в цветовой шкале и все значения ниже 0 до нижнего цвета в цветовой шкале, а затем растягивает масштаб для цветов между ними.

imagesc documentation.

2

В Python с использованием пакета MatPlotLib решение выглядит следующим образом:


import pylab as pl
x = [[0,1,2],[3,4,5],[6,7,18]]
pl.matshow(x, vmin=0, vmax=8)
pl.axis('image')
pl.axis('off')
show()
So vmin and vmax are boundary limits for the full range of colormap.
1
f1 = figure;
x = [0,1,2; 3,4,5; 6,7,8];
imagesc(x)
axis square
axis off

limits = get(gca(f1),'CLim');

f2 = figure;
z = [0,1,2; 3,4,5; 6,7,18];
imagesc(z)
axis square
axis off
caxis(limits)

Ещё вопросы

Сообщество Overcoder
Наверх
Меню