【问题标题】:MATLAB: How do I shade plot area with increasing and decreasing shades of a colorMATLAB:如何通过增加和减少颜色的阴影来阴影绘图区域
【发布时间】:2016-02-10 08:33:09
【问题描述】:

我想用一种颜色(黑色)绘制图形的一个区域(x = 1:24y = -1:1),然后我想根据一天中的时间减少/增加阴影。所以,我有一个情节,它在晚上的背景中是“黑暗的”,在白天是“明亮的”,x 是一天中的几个小时,y 是一个数据值。我的日出将在6.8,我的日落将在22。然后我会在散点图上叠加数据。

我尝试过使用patcharea,但没有成功。这是我从互联网上其他地方获得的一些代码,但我不确定如何继续:

% Define x, f(x), and M(x)
x = linspace(6.8, 22)';
f = sin(x)+cos(x);
M = x.^2;

% Define the vertices: the points at (x, f(x)) and (x, 0)
N = length(x);
verts = [x(:), f(:), x(:) zeros(N,1)];

% Define the faces to connect each adjacent f(x) and the corresponding points at y = 0.
q = (1:N-1)';
faces = [q, q+1, q+N+1, q+N];
p = patch('Faces', faces, 'Vertices', verts, 'FaceVertexCData', [M(:); M(:)], 'FaceColor', 'interp', 'EdgeColor', 'none')

所以我希望最终结果类似于下面附加的图像(注意模糊阴影 - 我希望渐变更强),但使用 x = 1:24y = -1:1

【问题讨论】:

  • 您能手动绘制图表吗?您还需要添加示例数据
  • 为什么不包括你目前拥有的代码和你得到的结果?
  • 我在问题中添加了一些代码

标签: matlab plot area


【解决方案1】:

由于您忽略了我在您的问题中包含您的 MATLAB 代码结果的请求,因此我不得不查看我的水晶球以确定您的“胡闹”导致了以下错误消息:

使用补丁出错

在设置 Patch 的 'Vertices' 属性时:

值必须是数值类型的 1x2 或 1x3 向量

X 中的错误(第 13 行)

p = patch('Faces', faces, 'Vertices', verts, 'FaceVertexCData', [M(:); M(:)], 'FaceColor', 'interp', 'EdgeColor', 'none')

这可以通过在第 8 行垂直连接顶点而不是水平连接来解决:

verts = [x(:), f(:); x(:), zeros(N,1)];

这会产生以下情节:

这当然不是你想要的。

实际解决方案

您可以从 5 个矩形构建背景:

x = [0;
     6;    % 'First daylight'
     6.8;  % Full brightness
     22;   % Beginning of 'sunset'
     22.8; % Complete darkness
     24];

vertices = [repmat(x,2,1), [ones(size(x)); -1.*ones(size(x))]];
faces = [1:5; 2:6; 8:12; 7:11].';
color = repmat([0 0 0; % 'Night' color
                0 0 0;
                1 1 1; % 'Day' color
                1 1 1;
                0 0 0;
                0 0 0],2,1);
              
patch('Faces',faces, ...
      'Vertices',vertices, ...
      'FaceVertexCData',color, ...
      'FaceColor','interp', ...
      'EdgeColor','red');
xlim([0 24]);

通过将适当的 RGB 值分配给 FaceVertexCData 属性,将“夜晚”矩形的顶点设置为黑色,将“白天”矩形的顶点设置为白色。

然后通过将FaceColor 属性设置为'interp',将'sunrise'/'sunset' 矩形的颜色插值在黑白之间。

EdgeColor 仅设置为'red' 以说明背景是如何构建的。将属性设置为'interp',使线条消失。

【讨论】:

  • 很抱歉没有显示结果让我很痛苦,我在撰写本文时无法访问 Matlab,因为它正在运行我需要先完成的大量 for 循环。该图在 x 和 y 轴方面是正确的,并且存在渐变阴影。但是,我需要形状是矩形(而不是 sin 和 cos 的函数),并且我需要最亮的颜色在“中午”附近,最暗在“晚上”。
  • 我添加了一个接近我想要实现的图像。
  • @Michael 这是你想要的吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多