Effacer les filtres
Effacer les filtres

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

228 vues (au cours des 30 derniers jours)
André Silvestre
André Silvestre le 12 Jan 2022
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.

Réponses (3)

Ty Watkins
Ty Watkins le 12 Jan 2022
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 commentaires
Ty Watkins
Ty Watkins le 13 Jan 2022
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.
André Silvestre
André Silvestre le 13 Jan 2022
Thank you!! Cannot express how much this helped me. Have a good day

Connectez-vous pour commenter.


Bjorn Gustavsson
Bjorn Gustavsson le 12 Jan 2022
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
  2 commentaires
Bjorn Gustavsson
Bjorn Gustavsson le 13 Jan 2022
In addition to those tools I've also had good use of COLORMAP and COLORBAR utilities.
André Silvestre
André Silvestre le 13 Jan 2022
Thank you!

Connectez-vous pour commenter.


Image Analyst
Image Analyst le 13 Jan 2022
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);

Catégories

En savoir plus sur Colormaps dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by