Message Boards Message Boards

0
|
6090 Views
|
5 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Set up a ColorFunction on the following DensityPlot?

Hi,

I have a function, say f[x,y] that can return only 3 values: +1, 0, and -1 (it is similar to Sign, but comes from stability analysis of a control system with 2 state variables) I would like to use DensityPlot so that +1, 0 and -1 map to red, white and blue, respectively, over a specified (x,y) plot region Question: how do I set ColorFunction to achieve that mapping? The plot statement is simply

DensityPlot [f[x,y],{x,min,max},{y,min,max}, PlotPoints->50, ... ]  

where ... are additional options.

POSTED BY: Carlos Felippa
5 Replies

Carlos,

That is what is happening in my posted example. Look at the ColorFunction Documentation under details. It shows. a list of what is sent to the color function for each plotting command. For DensityPlot it sends "f", the function value, which in your case is "f[x,y]".

To use it, you make a function that takes that argument (in this case f[x,y]) and returns a color based on that value. Since DensityPlot does not send the x and y values to the color function and only f[x,y] you can only reference the function value as the sole argument of the color function. In my example, I probably confused you by calling that argument "x". The "x" in the color function is NOT the x coordinate, but rather "f[x,y]". I should have posted this as my colorfunction to make it easier to understand.

colorfun[funValue_] := Which[funValue == 1, Red, funValue == 0, White, funValue == -1, Blue]

Regards,

Neil

POSTED BY: Neil Singer

Can the ColorFunction point to the function being plotted, as in ColorFunction->Which[ f[x,y] == 1, Red, f[x,y] == 0, White, f[x,y] == -1, Blue]

POSTED BY: Carlos Felippa

Also, you can change the Hue of the colors to what you want by replacing the named colors with RGBColor[] instead.

POSTED BY: Neil Singer

Carlos,

I made up a function (since you did not post one)

ff[x_, y_] := Piecewise[{{1, 10 < x}, {0, 10 > x > 5}, {-1, x < 5}}]

Now do a color function that takes a value and determines the color.

colorfun[x_] := Which[x == 1, Red, x == 0, White, x == -1, Blue]

and your density plot

DensityPlot[ff[x, y], {x, 0, 15}, {y, 0, 15}, 
 ColorFunction -> colorfun, ColorFunctionScaling -> False]

will plot something which looks like the French Flag.

Note you MUST use ColorFunctionScaling->False so the color function is not rescaled -- you want the colors you specified exactly.

Regards,

Neil

POSTED BY: Neil Singer

Thanks, your ColorFunction works fine. I did not show the actual f[x,y] as that is a long code, about 500 lines of dynamic programming.

POSTED BY: Carlos Felippa
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract