Coloring grid spaces in a 2D by index

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

Coloring grid spaces in a 2D by index

BGreen
I would like to have a grid in which each box is filled with a color
corresponding to a value. What is the best way to do this?

Below is an example of what I've produced so far, which is okay but not
quite what I want.

<http://octave.1599824.n4.nabble.com/file/t373379/Example.png>

minindex is the matrix of values I wish to color by number, and the x and y
axes are Bvec and Dbiasvec. I have used the following code and it works, but
it's not the ideal solution.

        mksz = 10;
        clf
        hold on
        for m=1:length(Bvec)
                for j=1:length(Dbiasvec)
                        if minindex(m,j)==3 % FOP
                                clr = "green";
                       
plot(Bvec(m),Dbiasvec(j),"color",clr,"marker","s","markerfacecolor",clr,"markersize",mksz)
                        elseif minindex(m,j)==4 % POP
                                clr = [1,0.5,0];
                       
plot(Bvec(m),Dbiasvec(j),"color",clr,"marker","s","markerfacecolor",clr,"markersize",mksz)
                        elseif minindex(m,j)==2 % FVP
                                clr = "red";
                       
plot(Bvec(m),Dbiasvec(j),"color",clr,"marker","s","markerfacecolor",clr,"markersize",mksz)
                        elseif minindex(m,j)==1 % FSP
                                clr = "blue";
                       
plot(Bvec(m),Dbiasvec(j),"color",clr,"marker","s","markerfacecolor",clr,"markersize",mksz)
                        elseif minindex(m,j)==5 % FSP-FVP
                                clr = "black";
                       
plot(Bvec(m),Dbiasvec(j),"color",clr,"marker","s","markerfacecolor",clr,"markersize",mksz)
                        else % OSP-POP
                                clr = [0.75,0,0.75];
                       
plot(Bvec(m),Dbiasvec(j),"color",clr,"marker","s","markerfacecolor",clr,"markersize",mksz)
                        end
                end
        end

With this approach the colored boxes are centered on the corresponding
values, just as I would like, but there are unsightly gaps in between.

pcolor has the plus of no gaps, but as far as I know I can't just specify a
custom color corresponding to each discrete value.

Is there better way to do generate the sort of plot I would like?



--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: Coloring grid spaces in a 2D by index

Carlo de Falco-2


> Il giorno 02 lug 2019, alle ore 19:48, BGreen <[hidden email]> ha scritto:
>
> I would like to have a grid in which each box is filled with a color
> corresponding to a value. What is the best way to do this?


you may want to try the commands pcolor[1], or imshow[2],
hope this helps,
c.

[1] https://octave.org/doc/interpreter/Two_002dDimensional-Plots.html#XREFpcolor
[2] https://octave.org/doc/interpreter/Displaying-Images.html#XREFimshow




Reply | Threaded
Open this post in threaded view
|

Re: Coloring grid spaces in a 2D by index

BGreen
Thank you!

I mentioned in my first question that I had trouble with pcolor, but maybe I can get it to work the way I need if I write a custom colormap with 6 entries. I haven't played with colormaps much, so I'm not sure if it'll work, but I suppose it's worth a shot.

imshow would be good if I could control the axes. I definitely need to have a set of coordinates to go along with the plot, but I didn't see anything about that for imshow in the documentation. Since it's meant for image processing, I didn't really expect anything, though.

- Brett Green


On Tue, Jul 2, 2019 at 3:20 PM Carlo De Falco <[hidden email]> wrote:


> Il giorno 02 lug 2019, alle ore 19:48, BGreen <[hidden email]> ha scritto:
>
> I would like to have a grid in which each box is filled with a color
> corresponding to a value. What is the best way to do this?


you may want to try the commands pcolor[1], or imshow[2],
hope this helps,
c.

[1] https://octave.org/doc/interpreter/Two_002dDimensional-Plots.html#XREFpcolor
[2] https://octave.org/doc/interpreter/Displaying-Images.html#XREFimshow




Reply | Threaded
Open this post in threaded view
|

Re: Coloring grid spaces in a 2D by index

BGreen
It would also be very nice if I could have some areas discretely colored, but others mixed. For example, I would like to keep the blue, red, green and orange regions for minindex(m,j)==1,2,3,4, but the case currently colored black and assigned minindex(m,j)==5 would be better represented as a mixture of red and blue which continuously interpolates between them, as minindex(m,j) takes noninteger values between 1 and 2 (which are the integers which are colored with blue and red).

I've had another idea: superimposing plots with different colormaps. That would allow me to have one colormap with just a few discrete colors in one plot, and then a different colormap for the blue-to-red continuum. I'll try that and send an update if I get something to work.

- Brett Green


On Tue, Jul 2, 2019 at 5:37 PM Brett Green <[hidden email]> wrote:
Thank you!

I mentioned in my first question that I had trouble with pcolor, but maybe I can get it to work the way I need if I write a custom colormap with 6 entries. I haven't played with colormaps much, so I'm not sure if it'll work, but I suppose it's worth a shot.

imshow would be good if I could control the axes. I definitely need to have a set of coordinates to go along with the plot, but I didn't see anything about that for imshow in the documentation. Since it's meant for image processing, I didn't really expect anything, though.

- Brett Green


On Tue, Jul 2, 2019 at 3:20 PM Carlo De Falco <[hidden email]> wrote:


> Il giorno 02 lug 2019, alle ore 19:48, BGreen <[hidden email]> ha scritto:
>
> I would like to have a grid in which each box is filled with a color
> corresponding to a value. What is the best way to do this?


you may want to try the commands pcolor[1], or imshow[2],
hope this helps,
c.

[1] https://octave.org/doc/interpreter/Two_002dDimensional-Plots.html#XREFpcolor
[2] https://octave.org/doc/interpreter/Displaying-Images.html#XREFimshow




Reply | Threaded
Open this post in threaded view
|

Re: Coloring grid spaces in a 2D by index

BGreen
To superimpose plots with different colormaps, I have to plot them to separate axes and then assign different colormaps to the separate axes. However, the axes have an opaque background. I just checked the documentation for axes(), and although there are properties for transparency, they're currently unused because that functionality has not been implemented yet. I guess that means I need to find another way.

For completeness, here's the code that overlays two axes objects with different colormaps, but will not work for my purposes because they're opaque.

blue = [0,0,1];
red = [1,0,0];
green = [0,1,0];
orange = [1,0.5,0];
purple = [0.75,0,0.75];

discrete_clrmp = [blue;red;green;orange];

redblue_clrmp = [linspace(0,1,100)',zeros(100,1),fliplr(linspace(0,1,100))'];
orangepurple_clrmp = [fliplr(linspace(0,1,100))',fliplr(linspace(0,0.5,100))',linspace(0,0.75,100)'];

fig = figure(3);
ax1 = axes(fig);
colormap(ax1,discrete_clrmp);
ax2 = axes(fig);
colormap(ax2,redblue_clrmp);

data = round(4*rand(100));
pcolor(ax1,data);
view(2)
continuousdata = rand(23);
pcolor(ax2,continuousdata)
view(2)


- Brett Green


On Tue, Jul 2, 2019 at 8:16 PM Brett Green <[hidden email]> wrote:
It would also be very nice if I could have some areas discretely colored, but others mixed. For example, I would like to keep the blue, red, green and orange regions for minindex(m,j)==1,2,3,4, but the case currently colored black and assigned minindex(m,j)==5 would be better represented as a mixture of red and blue which continuously interpolates between them, as minindex(m,j) takes noninteger values between 1 and 2 (which are the integers which are colored with blue and red).

I've had another idea: superimposing plots with different colormaps. That would allow me to have one colormap with just a few discrete colors in one plot, and then a different colormap for the blue-to-red continuum. I'll try that and send an update if I get something to work.

- Brett Green


On Tue, Jul 2, 2019 at 5:37 PM Brett Green <[hidden email]> wrote:
Thank you!

I mentioned in my first question that I had trouble with pcolor, but maybe I can get it to work the way I need if I write a custom colormap with 6 entries. I haven't played with colormaps much, so I'm not sure if it'll work, but I suppose it's worth a shot.

imshow would be good if I could control the axes. I definitely need to have a set of coordinates to go along with the plot, but I didn't see anything about that for imshow in the documentation. Since it's meant for image processing, I didn't really expect anything, though.

- Brett Green


On Tue, Jul 2, 2019 at 3:20 PM Carlo De Falco <[hidden email]> wrote:


> Il giorno 02 lug 2019, alle ore 19:48, BGreen <[hidden email]> ha scritto:
>
> I would like to have a grid in which each box is filled with a color
> corresponding to a value. What is the best way to do this?


you may want to try the commands pcolor[1], or imshow[2],
hope this helps,
c.

[1] https://octave.org/doc/interpreter/Two_002dDimensional-Plots.html#XREFpcolor
[2] https://octave.org/doc/interpreter/Displaying-Images.html#XREFimshow




Reply | Threaded
Open this post in threaded view
|

Re: Coloring grid spaces in a 2D by index

Pantxo
In reply to this post by BGreen
BGreen wrote
> imshow would be good if I could control the axes. I definitely need to
> have
> a set of coordinates to go along with the plot, but I didn't see anything
> about that for imshow in the documentation. Since it's meant for image
> processing, I didn't really expect anything, though.
>
> - Brett Green

Hi,

You can control the image "xdata" and "ydata" properties which define the
coordinates of the centers of you corner pixels (see "doc imshow"). If you
pass the whole x/y coordinate vectors you mention then only the first and
last elements will be taken into account. This should not be an issue if
your Bvec and Bbiasvec are regularly spaced.

Pantxo



--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: Coloring grid spaces in a 2D by index

Pantxo
In reply to this post by BGreen
BGreen wrote

> To superimpose plots with different colormaps, I have to plot them to
> separate axes and then assign different colormaps to the separate axes.
> However, the axes have an opaque background. I just checked the
> documentation for axes(), and although there are properties for
> transparency, they're currently unused because that functionality has not
> been implemented yet. I guess that means I need to find another way.
>
> For completeness, here's the code that overlays two axes objects with
> different colormaps, but will not work for my purposes because they're
> opaque.
>
> blue = [0,0,1];
> red = [1,0,0];
> green = [0,1,0];
> orange = [1,0.5,0];
> purple = [0.75,0,0.75];
>
> discrete_clrmp = [blue;red;green;orange];
>
> redblue_clrmp =
> [linspace(0,1,100)',zeros(100,1),fliplr(linspace(0,1,100))'];
> orangepurple_clrmp =
> [fliplr(linspace(0,1,100))',fliplr(linspace(0,0.5,100))',linspace(0,0.75,100)'];
>
> fig = figure(3);
> ax1 = axes(fig);
> colormap(ax1,discrete_clrmp);
> ax2 = axes(fig);
> colormap(ax2,redblue_clrmp);
>
> data = round(4*rand(100));
> pcolor(ax1,data);
> view(2)
> continuousdata = rand(23);
> pcolor(ax2,continuousdata)
> view(2)

IIUC you have data points in the range [1 2] and for some reason some of
them
are tagged 3 or 4:

## The original data
data = rand (100) + 1;
data(rand(100) < 0.1) = 3;
data(rand(100) < 0.1) = 4;

## The colormap you want
nlev = 50;
redblue = [linspace(0,1,nlev)',zeros(nlev,1),fliplr(linspace(0,1,nlev))'];
cmap = [redblue; 0 1 0; 1 0.5 0];

## Now manipulate the data for plotting
data(data == 3) = nlev + 1;
data(data == 4) = nlev + 2;
data(data<3) = (data(data<3) - 1) * (nlev-1) + 1;
imshow (data, cmap, "xdata", (-10:10), "ydata", (-10:10))

HTH,

Pantxo



--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: Coloring grid spaces in a 2D by index

BGreen
Thank you for pointing that out! That looks nice. Is there a way to do it with axes? imshow doesn't produce axes for me, but I need those.

- Brett Green


On Wed, Jul 3, 2019 at 4:43 PM Pantxo <[hidden email]> wrote:
BGreen wrote
> To superimpose plots with different colormaps, I have to plot them to
> separate axes and then assign different colormaps to the separate axes.
> However, the axes have an opaque background. I just checked the
> documentation for axes(), and although there are properties for
> transparency, they're currently unused because that functionality has not
> been implemented yet. I guess that means I need to find another way.
>
> For completeness, here's the code that overlays two axes objects with
> different colormaps, but will not work for my purposes because they're
> opaque.
>
> blue = [0,0,1];
> red = [1,0,0];
> green = [0,1,0];
> orange = [1,0.5,0];
> purple = [0.75,0,0.75];
>
> discrete_clrmp = [blue;red;green;orange];
>
> redblue_clrmp =
> [linspace(0,1,100)',zeros(100,1),fliplr(linspace(0,1,100))'];
> orangepurple_clrmp =
> [fliplr(linspace(0,1,100))',fliplr(linspace(0,0.5,100))',linspace(0,0.75,100)'];
>
> fig = figure(3);
> ax1 = axes(fig);
> colormap(ax1,discrete_clrmp);
> ax2 = axes(fig);
> colormap(ax2,redblue_clrmp);
>
> data = round(4*rand(100));
> pcolor(ax1,data);
> view(2)
> continuousdata = rand(23);
> pcolor(ax2,continuousdata)
> view(2)

IIUC you have data points in the range [1 2] and for some reason some of
them
are tagged 3 or 4:

## The original data
data = rand (100) + 1;
data(rand(100) < 0.1) = 3;
data(rand(100) < 0.1) = 4;

## The colormap you want
nlev = 50;
redblue = [linspace(0,1,nlev)',zeros(nlev,1),fliplr(linspace(0,1,nlev))'];
cmap = [redblue; 0 1 0; 1 0.5 0];

## Now manipulate the data for plotting
data(data == 3) = nlev + 1;
data(data == 4) = nlev + 2;
data(data<3) = (data(data<3) - 1) * (nlev-1) + 1;
imshow (data, cmap, "xdata", (-10:10), "ydata", (-10:10))

HTH,

Pantxo



--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html




Reply | Threaded
Open this post in threaded view
|

Re: Coloring grid spaces in a 2D by index

Pantxo
Le 03/07/2019 à 22:49, Brett Green a écrit :
Thank you for pointing that out! That looks nice. Is there a way to do it with axes? imshow doesn't produce axes for me, but I need those.

- Brett Green


On Wed, Jul 3, 2019 at 4:43 PM Pantxo <[hidden email]> wrote:
BGreen wrote
> To superimpose plots with different colormaps, I have to plot them to
> separate axes and then assign different colormaps to the separate axes.
> However, the axes have an opaque background. I just checked the
> documentation for axes(), and although there are properties for
> transparency, they're currently unused because that functionality has not
> been implemented yet. I guess that means I need to find another way.
>
> For completeness, here's the code that overlays two axes objects with
> different colormaps, but will not work for my purposes because they're
> opaque.
>
> blue = [0,0,1];
> red = [1,0,0];
> green = [0,1,0];
> orange = [1,0.5,0];
> purple = [0.75,0,0.75];
>
> discrete_clrmp = [blue;red;green;orange];
>
> redblue_clrmp =
> [linspace(0,1,100)',zeros(100,1),fliplr(linspace(0,1,100))'];
> orangepurple_clrmp =
> [fliplr(linspace(0,1,100))',fliplr(linspace(0,0.5,100))',linspace(0,0.75,100)'];
>
> fig = figure(3);
> ax1 = axes(fig);
> colormap(ax1,discrete_clrmp);
> ax2 = axes(fig);
> colormap(ax2,redblue_clrmp);
>
> data = round(4*rand(100));
> pcolor(ax1,data);
> view(2)
> continuousdata = rand(23);
> pcolor(ax2,continuousdata)
> view(2)

IIUC you have data points in the range [1 2] and for some reason some of
them
are tagged 3 or 4:

## The original data
data = rand (100) + 1;
data(rand(100) < 0.1) = 3;
data(rand(100) < 0.1) = 4;

## The colormap you want
nlev = 50;
redblue = [linspace(0,1,nlev)',zeros(nlev,1),fliplr(linspace(0,1,nlev))'];
cmap = [redblue; 0 1 0; 1 0.5 0];

## Now manipulate the data for plotting
data(data == 3) = nlev + 1;
data(data == 4) = nlev + 2;
data(data<3) = (data(data<3) - 1) * (nlev-1) + 1;
imshow (data, cmap, "xdata", (-10:10), "ydata", (-10:10))

HTH,

Pantxo



--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html



The axes is present but hidden, so you can use "axis on" to see it.

Pantxo

PS: It is good practice in this mailing list to bottom post, i.e. answer bellow the post you are citing.



Reply | Threaded
Open this post in threaded view
|

Re: Coloring grid spaces in a 2D by index

BGreen

The axes is present but hidden, so you can use "axis on" to see it.

Pantxo

PS: It is good practice in this mailing list to bottom post, i.e. answer bellow the post you are citing.

Thank you very much for both the plotting help and the etiquette pointer!

I read your solution and realized that this is something I had thought of earlier (since the same method can be applied to surf and pcolor) but hadn't done because there is one more problem - I actually want two sections with two different continuous color transitions. The problem here is that if I do something like

colormap([ redblue ; 0, 1, 0 ; 1, 0.5, 0 ; orangepurple ]

and have a continuum of numbers from 1 to 2 for redblue, then 3 and 4 discrete for the middle two, and finally a continuum 5 to 6 for orangepurple, then Octave will decide where it wants to draw the line in the colormap as to what number receives which color. For example, 3 and 4 could end up as part of the redblue spectrum, so that everything from 1 to 4 is some shade of red or blue, and then 5 and greater would be in the orangepurple part of the colormap.

Is there a way to fix specific values to specific colors?

- Brett Green


On Wed, Jul 3, 2019 at 5:18 PM Pantxo Diribarne <[hidden email]> wrote:
Le 03/07/2019 à 22:49, Brett Green a écrit :
Thank you for pointing that out! That looks nice. Is there a way to do it with axes? imshow doesn't produce axes for me, but I need those.

- Brett Green


On Wed, Jul 3, 2019 at 4:43 PM Pantxo <[hidden email]> wrote:
BGreen wrote
> To superimpose plots with different colormaps, I have to plot them to
> separate axes and then assign different colormaps to the separate axes.
> However, the axes have an opaque background. I just checked the
> documentation for axes(), and although there are properties for
> transparency, they're currently unused because that functionality has not
> been implemented yet. I guess that means I need to find another way.
>
> For completeness, here's the code that overlays two axes objects with
> different colormaps, but will not work for my purposes because they're
> opaque.
>
> blue = [0,0,1];
> red = [1,0,0];
> green = [0,1,0];
> orange = [1,0.5,0];
> purple = [0.75,0,0.75];
>
> discrete_clrmp = [blue;red;green;orange];
>
> redblue_clrmp =
> [linspace(0,1,100)',zeros(100,1),fliplr(linspace(0,1,100))'];
> orangepurple_clrmp =
> [fliplr(linspace(0,1,100))',fliplr(linspace(0,0.5,100))',linspace(0,0.75,100)'];
>
> fig = figure(3);
> ax1 = axes(fig);
> colormap(ax1,discrete_clrmp);
> ax2 = axes(fig);
> colormap(ax2,redblue_clrmp);
>
> data = round(4*rand(100));
> pcolor(ax1,data);
> view(2)
> continuousdata = rand(23);
> pcolor(ax2,continuousdata)
> view(2)

IIUC you have data points in the range [1 2] and for some reason some of
them
are tagged 3 or 4:

## The original data
data = rand (100) + 1;
data(rand(100) < 0.1) = 3;
data(rand(100) < 0.1) = 4;

## The colormap you want
nlev = 50;
redblue = [linspace(0,1,nlev)',zeros(nlev,1),fliplr(linspace(0,1,nlev))'];
cmap = [redblue; 0 1 0; 1 0.5 0];

## Now manipulate the data for plotting
data(data == 3) = nlev + 1;
data(data == 4) = nlev + 2;
data(data<3) = (data(data<3) - 1) * (nlev-1) + 1;
imshow (data, cmap, "xdata", (-10:10), "ydata", (-10:10))

HTH,

Pantxo



--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html



The axes is present but hidden, so you can use "axis on" to see it.

Pantxo

PS: It is good practice in this mailing list to bottom post, i.e. answer bellow the post you are citing.



Reply | Threaded
Open this post in threaded view
|

Re: Coloring grid spaces in a 2D by index

Pantxo


Le jeu. 4 juil. 2019 à 01:18, Brett Green <[hidden email]> a écrit :


Is there a way to fix specific values to specific colors?


Yes, this is exactly what the example I provided does...
How is it different from the previous case? Your original data matrix has to be re-conditioned in order to represent an indexed image, i.e. a matrix in which each number represents a row in a given colormap:

## The original data with two continuous sections and discrete values in between
data = [(rand (100, 50) + 1), (rand (100, 50) + 5)];
data(rand(100) < 0.1) = 3;
data(rand(100) < 0.1) = 4;

## The colormap you want
nlev = 50;
redblue = [linspace(0,1,nlev)',zeros(nlev,1),fliplr(linspace(0,1,nlev))'];
orangepurple = [fliplr(linspace(0,1,nlev))',fliplr(linspace(0,0.5,nlev))',linspace(0,0.75,nlev)'];
cmap = [redblue; 0 1 0; 1 0.5 0; orangepurple];

## Now manipulate the data for plotting
data(data == 3) = nlev + 1;
data(data == 4) = nlev + 2;
data(data<3) = (data(data<3) - 1) * (nlev-1) + 1;
data(data<7 & data >=5) = nlev + 2 + (data(data<7 & data >=5) - 5) * (nlev-1) + 1;
imshow (data, cmap, "xdata", (-10:10), "ydata", (-10:10))
colorbar

Pantxo