How to make a line plot with a colorbar as the third variable?

Hello community,
Lets say I have three variables, being Y the variable one. X and Z are constant, of which Y depends on.
x= linspace(0,100,500);
z= [5, 15, 25, 35];
(I won't show the whole equations, so I'm just going to leave a made up function to demonstrate how all the variables influence each other)
for i = 1:length(z)
K1 = konstant+ z(i);
y(i,:) = (K1.*x)./((1 + (K1.*x)));
end
As an amateur in coding (general), I've seen similarish examples, but not one that could resemble to what I'm looking foward, which is shown below:
I know that is possible to apply the colormap function, but I'm having a bit of dificulties setting up the function, including the Ticks.
for i = 1:length(z)
color = z(i,:);
plot(x, y(i,:), 'color',color);
h = colorbar('Ticks',0:0.25:1,'TickLabels',{'5','15','25','35'});
Something like this? All help is appeciated. Thank you.

Answers (3)

You need to set the 'color' for each corresponding z value based on the colormap.
Given the equations you provided in your questions (though I added a value for konstant and changed the line for K1 so the lines would actually show up on the plot) here is a simple solution.
konstant = 0.01;
x= linspace(0,100,500);
z= [5, 15, 25, 35];
for i = 1:length(z)
K1 = konstant * z(i);
y(i,:) = K1.*x ./ (1 + K1.*x);
end
figure();
hold on;
grid on;
box on;
cmap = colormap('winter'); % take your pick (doc colormap)
zmap = linspace( min(z), max(z), length(cmap));
for i = 1:length(z)
color = interp1( zmap, cmap, z(i));
plot(x, y(i,:), 'color',color);
end
caxis([min(z), max(z)])
colorbar()

4 Comments

My solution seems to fit what you are trying to do, which is a single color for the entire line. Just separate colors for each line.
If, however, you are looking to color the line with a gradient based on a third array of values, you can use the suggestiongs provided @Bjorn Gustavsson.
I've also found a solution without extra functions from the exchange here. Though I had to add a 'drawnow' command before calling the colorbar for the lines to color correctly.
This worked perfectly, thank you!
I also was plotting markers (specific Y values correspondent to experimental data), and it appears that those markers have different colors compared with the colors present in the colormap, shown below:
Each marker as a Y and Z associtated. Is there a way to color them to the correspondent Z color in the colorbar?
Thank you and Bjorn for the feedback, very appreciatted.
Yes, that can just as easily be done by getting the color code from the colorbar for the Z value of each data point.
mcolor = interp1( zmap, cmap, ZVALUE);
And then you can use this RGB triplet to set either the 'markerfacecolor' or the 'markeredgecolor' in the plot command.
plot(xdata, ydata), '+', 'markerfacecolor', mcolor, 'markeredgecolor', mcolor);
If you have a set of ZVALUE's, the above code snippet will return the same size array of corresponding color values.
Thank you!! Cannot express how much this helped me. Have a good day

Sign in to comment.

When I want to plot colour-coded curves I use one of these functions from the file exchange:
They should give you something to either use as is or take as a template for how to do it.
HTH
You need to have a colormap that is an N-by-3 array, not like your z which is a 1-by-4 array. Try this:
% Initialization Steps.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 18;
x = linspace(0,100,500);
z = [5, 15, 25, 35]/1000;
konstant = 0.00005;
% Create a colormap with as many colors as elements in z.
Lz = length(z);
cMap = jet(Lz);
% Create y matrix.
y = zeros(Lz, length(x));
for k = 1 : Lz
K1 = konstant + z(k);
y(k, :) = (K1.*x) ./ ((1 + (K1.*x)));
end
% Plot the different y curves.
for k = 1 : Lz
thisColor = cMap(k,:);
plot(x, y(k,:), 'color',thisColor, 'LineWidth', 4);
hold on;
tickLabels{k} = sprintf('%g', 1000 * z(k));
end
miny = min(y(:))
maxy = max(y(:))
caxis([miny, maxy])
grid on;
xlabel('x', 'FontSize', fontSize);
ylabel('y', 'FontSize', fontSize);
colormap(cMap);
h = colorbar('Ticks', linspace(miny, maxy, Lz), 'TickLabels', tickLabels);

Products

Release

R2021a

Asked:

on 12 Jan 2022

Commented:

on 13 Jan 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!