[Mathematica]Frustratedhow to color a plot according to the values?

• Mathematica
• hanson

hanson

Hi all.
I have been searching over the web for some time, kind of frustrated...
I would like to plot the contourplot of a function, and I would like the contourplot to be colored.
The rule is that...the negative values should be blue in colors and the positive values should be red in colors, but I also want some gradient between these two limits...
I suppose this can be quite easily done, but I still don't know how to do that...

Try playing with the contour option:
contour-># of contour lines
and contourstyle

they should be in the help context

ColorFunction

http://documents.wolfram.com/mathematica/functions/ColorFunction [Broken]

I can't remember the syntax (haven't had a Mathematica license in years), but you want something like ColorFunction -> (RGBColor[1.0#, 0.0, (1.0-1.0#)]&) or similar - you can fiddle with the constants to change the parametric variation, even make it nonlinear if you want.

Last edited by a moderator:
Thanks for the replies!
I have found a user-written Mathematica notebook which do exactly what i want. Thanks.

1. How can I color a plot in Mathematica based on the values?

In Mathematica, you can use the ColorFunction option to set a color scheme for your plot based on the values. For example, if your plot represents a function f(x,y), you can use the option ColorFunction -> ColorData["Rainbow"][f(x,y)]. This will color the plot using the colors from the Rainbow color scheme based on the values of the function.

2. Can I specify my own color scheme for a plot in Mathematica?

Yes, you can specify your own color scheme by defining a custom color function and using it in the ColorFunction option. For example, you can create a function called myColorFunction that takes in a value and returns a specific color, and then use it as ColorFunction -> myColorFunction[value] in your plot.

3. How can I add a color legend to my plot in Mathematica?

To add a color legend to your plot, you can use the option ColorFunctionScaling -> False to make sure the colors are mapped directly to the values. Then, you can use the option PlotLegends -> BarLegend[{ColorData["Rainbow"], {min, max}}] to add a color bar legend to your plot, where min and max are the minimum and maximum values of your plot.

4. Is it possible to change the color of individual data points in a plot?

Yes, you can use the option PlotStyle -> {color1, color2, ...} to specify a different color for each data point in your plot. The colors will be applied in the order of the data points, so the first color will be applied to the first data point, the second color to the second data point, and so on.

5. Can I use a different color scheme for different parts of my plot in Mathematica?

Yes, you can use the option ColorFunction -> {colorFunction1, colorFunction2, ...} to specify different color functions for different parts of your plot. For example, you can use the option ColorFunction -> {ColorData["Rainbow"], ColorData["TemperatureMap"]} to use the Rainbow color scheme for one part of the plot and the TemperatureMap color scheme for another part.