Surf plot is not plotting negative values and I want to assign colors to values
5 views (last 30 days)
Show older comments
Hey all,
I got this code to make a surfplot. It was made to display positive values.
I'm trying to make it work on a different data set which has both negative and positive values.
The plot that is produced does not pick up on the negative values in my data set, which is a problem.
Additionally i want to color values differently.
Red for values <-10
Blue for values between -10 and -1
Green for values >-1
Help would be greatly appreciated!
This is my simplified code:
a=xlsread('example.xlsx','tab1');
b=xlsread('example.xlsx','tab2');
dates = 1:1:631;
f1=figure(1);
h=surf(dates,b,a);
set(gca,'colorscale','log');
set(h,'edgecolor','none');
view(2);
c=colormap;
c(1,:)=[1 1 1];
colormap(c);
0 Comments
Answers (1)
Deepak
on 12 Dec 2024
To achieve the desired surf plot with specific colour coding for different value ranges, we need to manually define a custom colormap that assigns colours based on the value thresholds we have specified.
First, read data from Excel and create the surf plot using “surf”. Then, remove the edge colours and set the view to 2D. Define a colormap array and iterate over it to assign red for values less than -10, blue for values between -10 and -1, and green for values greater than -1.
Use “caxis” to set the colour axis to include the full range of your data, ensuring both negative and positive values are represented. Finally, apply the custom colormap and set the colour scale to linear, as a logarithmic scale cannot handle negative values.
Here is the modified MATLAB code to achieve the same:
% Read data from Excel
a = xlsread('example.xlsx', 'tab1');
b = xlsread('example.xlsx', 'tab2');
dates = 1:1:631;
% Create the surf plot
f1 = figure(1);
h = surf(dates, b, a);
% Remove edges and set view
set(h, 'edgecolor', 'none');
view(2);
% Define custom colormap
% Initialize the colormap with a default size
c = zeros(256, 3);
% Define thresholds
threshold1 = -10;
threshold2 = -1;
% Assign colors based on value ranges
for i = 1:length(c)
value = (i - 128) / 128 * max(abs(a(:))); % Normalize index to value range
if value < threshold1
c(i, :) = [1 0 0]; % Red for values <-10
elseif value >= threshold1 && value < threshold2
c(i, :) = [0 0 1]; % Blue for values between -10 and -1
else
c(i, :) = [0 1 0]; % Green for values >-1
end
end
% Apply custom colormap
colormap(c);
% Adjust color axis to include negative values
caxis([min(a(:)) max(a(:))]);
% Remove log scale for color
set(gca, 'colorscale', 'linear');
Please find attached the documentation of functions used for reference:
I hope this will help in resolving the issue.
0 Comments
See Also
Categories
Find more on Color and Styling in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!