Custom ColorFunction/ColorData in ArrayPlot (and similar functions)

前端 未结 2 1089
情深已故
情深已故 2021-02-03 10:03

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,

2条回答
  •  礼貌的吻别
    2021-02-03 10:51

    Replace your ColorData["Rainbow"] with this one:

    Function[Blend[RGBColor @@@ cMap, Slot[1]]]
    

    and you get this:

    enter image description here


    As to your second question, you can do it this way:

    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 -> Function[Blend[RGBColor @@@ cMap, Slot[1]]], 
      AspectRatio -> 1, FrameTicks -> Automatic, 
      DataRange -> {{-xMax, xMax}, {-yMax, yMax}}, DataReversed -> True]
    

    enter image description here

    but why don't you use DensityPlot?

    DensityPlot[Sin[y^3 + x^2], {x, -xMax, xMax}, {y, -yMax, yMax}, 
     ColorFunction -> Function[Blend[RGBColor @@@ cMap, Slot[1]]], 
     PlotPoints -> 300]
    

    enter image description here


    EDIT
    Note that in the second plot the y-range labeling is reversed. That's because it takes the DataReversed setting into account. ArrayPlot plots the rows of the arrays in the same order as they appear when the array's content is printed on screen. So the first row is plotted on top and the last row is plotted at the bottom. High row values correspond to low y-values and vice versa. DataReversed->True corrects for this phenomenon, but in this case it also 'corrects' the y values. A workaround is to fill the array starting from high y-values going down to the lower ones. In that case you don't need DataReversed:

    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 -> Function[Blend[RGBColor @@@ cMap, Slot[1]]], 
      AspectRatio -> 1, FrameTicks -> Automatic, 
      DataRange -> {{-xMax, xMax}, {-yMax, yMax}}]
    

    enter image description here

提交回复
热议问题