本文介绍了ArrayPlot 中的自定义 ColorFunction/ColorData(和类似函数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与 Simon 在 Mathematica 中

颜色图是实数的 m×3 矩阵0.0 到 1.0 之间的数字.每一行是一个 RGB 向量,它定义了一个颜色.颜色图的第 k 行定义第 k 个颜色,其中 map(k,:)= [r(k) g(k) b(k)]) 指定红色、绿色和蓝色的强度.

这里map=cMapm=64.

我尝试查看 ColorDataFunction,我看到 ColorData 格式类似于 colormap.但是,我不确定如何让 ArrayPlot 使用它(大概其他绘图函数应该是相同的).

此外,由于我在这里的练习纯粹是为了在 mma 中达到一定程度的舒适度,类似于我在 MATLAB 中的情况,我很感激有关改进我的代码的评论和建议.具体来说,我对修复"FrameTicks 的方法不太满意......当然必须有一种更好/更简单的方法来做到这一点.

解决方案

用这个替换你的 ColorData["Rainbow"]:

函数[Blend[RGBColor @@@ cMap, Slot[1]]]

你会得到这个:

关于你的第二个问题,你可以这样做:

xMax = 3;yMax = 3;img = 转置@表[Sin[y^3 + x^2], {x, -xMax, xMax, 0.01}, {y, -yMax, yMax,0.01}];绘图 = ArrayPlot[img,颜色函数 ->函数[Blend[RGBColor @@@ cMap, Slot[1]]],纵横比 ->1、FrameTicks ->自动的,数据范围 ->{{-xMax, xMax}, {-yMax, yMax}}, DataReversed ->真的]

但你为什么不使用 DensityPlot?

DensityPlot[Sin[y^3 + x^2], {x, -xMax, xMax}, {y, -yMax, yMax},颜色函数 ->函数[Blend[RGBColor @@@ cMap, Slot[1]]],绘图点 ->300]

编辑
请注意,在第二个图中,y 范围标记被反转.这是因为它考虑了 DataReversed 设置.ArrayPlot 按照数组内容打印在屏幕上时出现的顺序绘制数组的行.所以第一行绘制在顶部,最后一行绘制在底部.高行值对应于低 y 值,反之亦然.DataReversed->True 纠正了这种现象,但在这种情况下,它还纠正"了 y 值.一种解决方法是从高 y 值开始填充数组到较低的值.在这种情况下,您不需要 DataReversed:

xMax = 3;yMax = 3;img = 转置@表[Sin[y^3 + x^2], {x, -xMax, xMax, 0.01}, {y,yMax, -yMax, -0.01}];绘图 = ArrayPlot[img,颜色函数 ->函数[Blend[RGBColor @@@ cMap, Slot[1]]],纵横比 ->1、FrameTicks ->自动的,数据范围 ->{{-xMax, xMax}, {-yMax, yMax}}]

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.


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

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.

解决方案

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

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

and you get this:


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]

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]

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}}]

这篇关于ArrayPlot 中的自定义 ColorFunction/ColorData(和类似函数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 12:10