This is related to Simon's question on changing default ColorData in Mathematica. While the solutions all addressed the issue of changing ColorData
in line plots, I didn't quite find the discussion helpful in changing the ColorFunction
/ColorData
in ContourPlot
/ArrayPlot
/Plot3D
, etc.
TLDR: Is there a way to get mma to use custom colors in ArrayPlot/ContourPlot/etc.
Consider the following example plot of the function
sin(x^2+y^3)
that I created in MATLAB:
Now doing the same in mma as:
xMax = 3; yMax = 3;
img = Transpose@
Table[Sin[y ^3 + x^2], {x, -xMax, xMax, 0.01}, {y, -yMax, yMax,
0.01}];
plot = ArrayPlot[img, ColorFunction -> ColorData["Rainbow"],
AspectRatio -> 1,
FrameTicks -> {FindDivisions[{0, (img // Dimensions // First) - 1},
4], FindDivisions[{0, (img // Dimensions // Last) - 1}, 4],
None, None},
DataReversed ->
True] /. (FrameTicks -> {x_,
y_}) :> (FrameTicks -> {x /. {a_?NumericQ, b_Integer} :> {a,
2 xMax (b/((img // Dimensions // First) - 1) - 1/2)},
y /. {a_?NumericQ, b_Integer} :> {a,
2 yMax (b/((img // Dimensions // Last) - 1) - 1/2)}})
I get the following plot:
I prefer the rich, bright colors in MATLAB to mma's pastel/dull colors. How do I get mma to use these colors, if I have the RGB values of the colormap from MATLAB?
You can download the RGB values of the default colormap in MATLAB, and import it into mma as
cMap = Transpose@Import["path-to-colorMapJet.mat", {"HDF5",
"Datasets", "cMap"}];
cMap
is a 64x3
array of values between 0
and 1
.
Just to give you some background, here's some relevant text from the MathWorks documentation on colormap
A colormap is an m-by-3 matrix of real numbers between 0.0 and 1.0. Each row is an RGB vector that defines one color. The kth row of the colormap defines the kth color, where map(k,:) = [r(k) g(k) b(k)]) specifies the intensity of red, green, and blue.
Here map=cMap
, and m=64
.
I tried poking at ColorDataFunction
, and I see that the ColorData
format is similar to the colormap
. However, I'm not sure how to get ArrayPlot
to use it (and presumably it ought to be the same for other plot functions).
Also, since my exercise here is purely to reach a level of comfort in mma, similar to what I have in MATLAB, I'd appreciate comments and suggestions on improving my code. Specifically, I'm not too satisfied with my hack of a way to "fix" the FrameTicks
... surely there must be a nicer/easier way to do it.