【问题标题】:How to Set a User-Defined Colormap in Octave?如何在 Octave 中设置用户定义的颜色图?
【发布时间】:2019-01-19 23:29:39
【问题描述】:

我有一段简单的代码可以计算一些数量并将其绘制为等高线:

%Calculate Biot number vs. h for a selected material
h = (0:5:1000)';
mat = "Copper";
lambda = 386;
r = (0:0.25:50);  %In cm
 R = r./100; %In m
%Calculate matrix of Bi values
% R = length(h) x C = length(r)
Bi = (h.*R)/lambda;
%Contour Plot of results
%Set boundaries at Bi = 0, 0.1, 1
conts = [0, 0.1, 1];
ptitle = ["Biot Number for a ", mat, " Sphere"];
%Create a personalized colormap with 30 values.
%    0<Bi<0.1  Green
%    0.1<=Bi<1 Yellow
%    Bi >= 1   Red
my_green = [229,255,204]./255;
my_yellow = [255,255,204]./255;
my_pink = [255,229,204]./255;
my_cmap = [repmat(my_green, 10, 1); repmat(my_yellow, 10, 1); repmat(my_pink, 10, 1) ];
clf;
colormap (my_cmap);
contourf(h, r, Bi, conts, 'showtext', 'on');
title(ptitle)
xlabel ("h(W/m^2*K)");
ylabel ("r(cm)");

结果缺少中间色(黄色):

对此有什么办法?

【问题讨论】:

  • 创建一个 Px3 数组并将其设置为颜色图,cm = [repmat([0 1 0], L, 1); repmat([1 1 0], M, 1); repmat([1 0 0], N, 1); ]L MN 的值取决于您的数据。
  • 你能稍微扩展一下“L M 和 N 的值取决于你的数据”吗?
  • 为它们使用不同的值,你会明白我的意思。我的意思是,如果您希望颜色条为 60% 的绿色、30% 的黄色和 10% 的红色,请确保 L:M:N 之间的比例为 60:30:10。如果您想自动计算百分比,则需要进行更多修改。另请参阅caxis
  • 我明白了,我要试试。

标签: plot colors octave contour colormap


【解决方案1】:

您的轮廓太少,所以选择了错误的颜色。如果你这样做contourf(h, r, Bi, 0:0.2:1, 'showtext', 'on');,你会得到:

另外,我建议让“绿色”和“黄色”更加不同,因为在某些显示器上可能很难区分它们。


这就是我所说的“玩弄L, M, N

conts = [0, 0.1, 1];
ptitle = ["Biot Number for a ", mat, " Sphere"];
%Create a personalized colormap
my_green = [229,255,204]./255;
my_yellow = [255,255,204]./255;
my_pink = [255,229,204]./255;
my_cmap = [repmat(my_green, 10, 1); repmat(my_yellow, 90, 1); repmat(my_pink, 1, 1) ];

figure(); contourf(h, r, Bi, conts, 'showtext', 'on');

colormap (my_cmap);
caxis([0 1.01])
title(ptitle)
xlabel ("h(W/m^2*K)");
ylabel ("r(cm)");

顺便说一句,我在 MATLAB R2018a 上运行了这个,以防你想知道为什么你没有得到完全相同的东西。

【讨论】:

  • 我知道我有点挑剔,但我至少需要将轮廓设为 0.1,因为该值具有物理意义。
  • 没问题,根据需要修改轮廓台阶即可。
  • @FabioCapezzuoli 查看我的编辑,您可能会发现它很有用。
【解决方案2】:

添加以下代码来定义计数并生成颜色图,该过程可以自动化。

conts = [0, 0.05, 0.1, 0.3, 0.7, 1];
%Create a personalized colormap with 50 values distributed proportionally to Bi values
step = 50/max(max(Bi));
L = ceil(step*0.1);
M = ceil(step*(1-0.1));
H = ceil(step*(max(max(Bi))-1));
my_green = [229,255,204]./255;
my_yellow = [255,255,204]./255;
my_pink = [255,229,204]./255;
my_cmap = [repmat(my_green, L, 1); repmat(my_yellow, M, 1); repmat(my_pink, H, 1)];

获取:

【讨论】:

    猜你喜欢
    • 2014-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-16
    • 1970-01-01
    • 2020-12-14
    相关资源
    最近更新 更多