How to plot a line graph (x,y) with a color bar representing z-axis...

I have data in multiple y and z axis...
x-axis is fixed while y-axis is varying with eah value of x and z is fixed for each (x,y) pair .... as shown in below figure ...
for example red colour show the fixed value of z-axis (color bar) ... and y is vary w.r.t x.. same is the case for other ..
Both data nd required plot is shown here ...

 Accepted Answer

You can use plot, colorbar, colormap, and caxis. You can adjust the line color by setting the Color property of the line object returned by plot.
If you have trouble implementing this, feel free to post a comment about which step exactly is causing you issues, and what you tried to solve it.
The code below was tested on R2011a as well, so it should work for R2013a. Next time, please mention all warnings or errors that you're getting, but most importantly: mention your release. It is 7.5 years old, which in software is very old.
data=xlsread('Pt_data.xlsx');
x=data(:,1);
y=data(:,2:2:end);
z=data(1,3:2:end);
%set z-axis
upper_z=1e-6;
lower_z=1e-9;
%create the colorbar, retrieve the colomap data, and let it match your example image
c=colorbar;
cmap=colormap('jet');
caxis([log10(lower_z) log10(upper_z)])
%set tick positions and create tick labels
Ticks=round(log10(lower_z)):round(log10(upper_z));
TickLabels=arrayfun(@(x) sprintf('10^{%d}',x),Ticks,'UniformOutput',false);
try
set(c,'Ticks',Ticks);
set(c,'TickLabels',TickLabels);
catch %HG1
TickLabels=strrep(strrep(TickLabels,'{',''),'}','');%remove TeX formatting
set(c,'YTick',Ticks);
set(c,'YTickLabel',TickLabels);
end
%normalize the z values to the color scale
z_scaled=(z-lower_z)./(upper_z-lower_z);
z_scaled(z_scaled<0)=0;z_scaled(z_scaled>1)=1;
z_scaled=round(1+z_scaled*(size(cmap,1)-1));%round to nearest index
%plot and select colors from colormap
hold on
for n=1:size(y,2)
C=cmap(z_scaled(n),:);
plot(x,y(:,n),'Color',C);
end
hold off

12 Comments

Hi,
I am trying but did not uderstand this .. My data set show as below: 1 x axis and multiple x and z axis ,,,
What did you try? What exact step do you have trouble with?
This is the code... I just write for desired plot
a=xlsread('Pt_data');
x=a(:,1);
y1=a(:,2);
z1=a(:,3);
c=z(1);
y2=a(:,4);
z2=a(:,5);
c2=z(2);
y3=a(:,6);
z3=a(:,7);
c3=z(3)
y4=a(:,8);
z4=a(:,9);
c4=z(4)
plot(x,y1)
hold on
plot(x,y2)
hold on
plot(x,y3)
zz=[c,c1,c2,c3]
color bar zz
Why didn't you post your Excel file? Why are you numbering your variables instead of using arrays? Why didn't you use the edit tools to format your code as code? Why is there a space inside your call to colorbar? Did you even read the documentation of that function? Why didn't you answer the second question I posted?
Despite all these questions/issues I wrote some code for you that should solve your problem.
%generate your excel data because you didn't bother posting it as data instead of a picture
data=rand(10,1+6*2);data(:,1)=0.1:0.1:1;data(:,3)=1.91e-7;data(:,5)=3.351e-7;
data(:,7)=1.91e-6;data(:,9)=1.03e-7;data(:,11)=4.57e-6;data(:,13)=3.26e-7;
%retrieve data from your excel
x=data(:,1);
y=data(:,2:2:end);
z=data(1,3:2:end);
%set z-axis
upper_z=1e-6;
lower_z=1e-9;
%create the colorbar, set tick positions and create tick labels
c=colorbar;
c.Ticks=round(log10(lower_z)):round(log10(upper_z));
c.TickLabels=arrayfun(@(x) sprintf('10^{%d}',x),c.Ticks,'UniformOutput',false);
%retrieve the colomap data (and let it match your example image)
cmap=colormap('jet');
caxis([log10(lower_z) log10(upper_z)])
%normalize the z values to the color scale
z_scaled=(z-lower_z)./(upper_z-lower_z);
z_scaled(z_scaled<0)=0;z_scaled(z_scaled>1)=1;
z_scaled=round(1+z_scaled*(size(cmap,1)-1));%round to nearest index
hold on
for n=1:size(y,2)
C=cmap(z_scaled(n),:);%select colors from colormap
plot(x,y(:,n),'Color',C);
end
hold off
I am very gratful to you for this help.... Now coming towards the question ... When I try to plot z in colour bar their is an error (Your earlier question)
Why didn't you post your Excel file? ,.. Aplogy for this.. I am new here ... Normally I observe people post just picture to get a generic idea for the problem. From now, I will always post the data ..
Why are you numbering your variables instead of using arrays? ,,, Honestly, I did not have an idea about this .. You used this so it's good for me ... from now, i will always use this ...
Why didn't you use the edit tools to format your code as code? ... I did not understand about this ...
Why is there a space inside your call to colorbar? .. I am new so apply hit and trial method ... I think both works .. but not sure ...
Yes , I read but how thsi work for my case ... have no idea ,...
Why didn't you answer the second question I posted? ... I have already post my code ... so I asusme when you try this you get an idea where is the issue ...
Despite all these questions/issues I wrote some code for you that should solve your problem. .... I am very gratful to you for this help .... I just apply this to my data set and get this one (see below)..
I little confsue with the color bar ... instead of 1, 2 ,3 ... 10 (10^-7).... I should look like as 10^-9, 10^-8,10^-7 and 10^-6,,,
I have also attached my data for your kind considertion ... Thank you ..
What release are you using? It looks like you're using a relatively old one. The code I posted works on R2020a.
Yes,,, I am using 2013 ... Colorbar show 1, 2,3 ... While I am interested in 1e-9, 1e-8,1e-7,1e-6
You posted a screenshot, so I saw what was the result. Which 2013, a or b?
And if I recall correctly, the dot notation I used was only supported from R2014b. So what exact code did you use?
Matlab 2013a...
Code is as below
clear all
clc
data=xlsread('Pt_data');
x=data(:,1);
y=data(:,2:2:end);
z=data(1,3:2:end);
%set z-axis
upper_z=1e-6;
lower_z=1e-9;
%create the colorbar, set tick positions and create tick labels
c=colorbar;
c.Ticks=round(log10(lower_z)):round(log10(upper_z));
c.TickLabels=arrayfun(@(x) sprintf('10^{%d}',x),c.Ticks,'UniformOutput',false);
%retrieve the colomap data (and let it match your example image)
cmap=colormap('jet');
caxis([log10(lower_z) log10(upper_z)])
%normalize the z values to the color scale
z_scaled=(z-lower_z)./(upper_z-lower_z);
z_scaled(z_scaled<0)=0;z_scaled(z_scaled>1)=1;
z_scaled=round(1+z_scaled*(size(cmap,1)-1));%round to nearest index
hold on
for n=1:size(y,2)
C=cmap(z_scaled(n),:);%select colors from colormap
plot(x,y(:,n),'Color',C);
end
hold off
When you post a comment, you will see a bar with editing tools. Please use it to format your code as code. See this page. You will see a button to edit your comment.
As far as your code itself: you shouldn't use clear all. Use clear or clearvars instead. I will also probably only be able to modify the code for R2013a tomorrow.
Sure ,,, whenever possible for you ... thank
You can already edit your comment in the mean time.

Sign in to comment.

More Answers (0)

Products

Release

R2013a

Asked:

aa
on 18 Sep 2020

Edited:

Rik
on 21 Sep 2020

Community Treasure Hunt

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

Start Hunting!