# Coloring grid spaces in a 2D by index

11 messages
Open this post in threaded view
|

## Coloring grid spaces in a 2D by index

 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. 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
Open this post in threaded view
|

## Re: Coloring grid spaces in a 2D by index

 > 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
Open this post in threaded view
|

## Re: Coloring grid spaces in a 2D by index

 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 GreenOn 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
Open this post in threaded view
|

## Re: Coloring grid spaces in a 2D by index

 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 GreenOn 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 GreenOn 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
Open this post in threaded view
|

## Re: Coloring grid spaces in a 2D by index

 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 GreenOn 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 GreenOn 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 GreenOn 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
Open this post in threaded view
|

## Re: Coloring grid spaces in a 2D by index

 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
Open this post in threaded view
|

## Re: Coloring grid spaces in a 2D by index

 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
Open this post in threaded view
|

## Re: Coloring grid spaces in a 2D by index

 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 GreenOn 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
Open this post in threaded view
|

## Re: Coloring grid spaces in a 2D by index

 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.