How can I plot 2D surface plot with color bar

I have an excel file containing 3 variables as attached. I want to plot the variables as a 2D surface plot with the third column representing the color bar. I also attach an example of the plot I am trying to plot.
When I used surf command, I get this error:
Error using surf (line 71)
Z must be a matrix, not a scalar or vector.
Error in PTC_calculation (line 74)
surf(w_ev,beta,PTC)
Thanks

1 comentario

You cannot plot a surface plot with the data you have.
T = readtable('https://in.mathworks.com/matlabcentral/answers/uploaded_files/1227857/DATA.xlsx') ;
x = T.w ;
y = T.beta ;
z = T.PTC ;
scatter(x,y,[],z)

Iniciar sesión para comentar.

 Respuesta aceptada

Star Strider
Star Strider el 12 de Dic. de 2022
Editada: Star Strider el 12 de Dic. de 2022
It is possible to create a surface plot from it, however it is probably not worth the effort —
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1227857/DATA.xlsx')
T1 = 202×3 table
w beta PTC __________ __________ _______ 0.00012407 628.76 0.14509 0.0050855 25773 0.12082 0.010047 50917 0.13112 0.015008 76061 0.17144 0.01997 1.012e+05 0.21571 0.024931 1.2635e+05 0.25994 0.029893 1.5149e+05 0.30277 0.034854 1.7664e+05 0.34359 0.039816 2.0178e+05 0.38222 0.044777 2.2693e+05 0.41867 0.049739 2.5207e+05 0.45304 0.0547 2.7721e+05 0.48543 0.059662 3.0236e+05 0.51597 0.064623 3.275e+05 0.54477 0.069584 3.5265e+05 0.57192 0.074546 3.7779e+05 0.59753
figure
stem3(T1{:,1}, T1{:,2}, T1{:,3})
xlabel('w')
ylabel('\beta')
view(60,30)
title('Original Data')
N = size(T1,1)*10;
wv = linspace(min(T1.w), max(T1.w), N);
betav = linspace(min(T1.beta), max(T1.beta), N);
PTCv = linspace(min(T1.PTC), max(T1.PTC), N);
[Wm,Bm] = ndgrid(wv, betav);
PTCm = griddata(T1.w, T1.beta, T1.PTC, Wm, Bm);
PTCm(isnan(PTCm)) = 0;
figure
surf(Wm, Bm, PTCm, 'EdgeColor','interp', 'FaceAlpha',0.5)
colormap(turbo)
colorbar
xlabel('w')
ylabel('\beta')
view(60,30)
title('Interpolated Surface Plot Of Original Data')
figure
surf(Wm, Bm, PTCm, 'EdgeColor','interp', 'FaceAlpha',0.5)
colormap(turbo)
colorbar
xlabel('w')
ylabel('\beta')
view(-15,45)
title('Interpolated Surface Plot Of Original Data')
EDIT — Corrected typograp[hical errors.
.

6 comentarios

Ambali Odebowale
Ambali Odebowale el 12 de Dic. de 2022
Editada: Ambali Odebowale el 12 de Dic. de 2022
@Star Strider Thanks for the effort. Though it doesn't really give me the information i need from the graph. I reaaly appreciate your assistance.
Star Strider
Star Strider el 12 de Dic. de 2022
What information do you need from it?
I want it to look like the attached image (Image.PNG). From which I will be able to see surface mode. The three parameters required to get the plot are contained in the excel file. Do you want me to paste my code, maybe that will help.
Thanks
I must have missed that last night when I first saw this.
Use contourf instead of surf
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1227857/DATA.xlsx');
N = size(T1,1)*10;
wv = linspace(min(T1.w), max(T1.w), N);
betav = linspace(min(T1.beta), max(T1.beta), N);
PTCv = linspace(min(T1.PTC), max(T1.PTC), N);
[Wm,Bm] = ndgrid(wv, betav);
PTCm = griddata(T1.w, T1.beta, T1.PTC, Wm, Bm);
PTCm(isnan(PTCm)) = 1E-8;
figure
contourf(Wm, Bm, PTCm)
colormap(turbo)
% colormap(flipud(turbo))
colorbar
xlabel('w')
ylabel('\beta')
title('Interpolated Surface Plot Of Original Data')
figure
contour(Wm, Bm, PTCm)
colormap(turbo)
% colormap(flipud(turbo))
colorbar
xlabel('w')
ylabel('\beta')
title('Interpolated Surface Plot Of Original Data')
This is the best it is possible to do with the supplied data. If you have more data or a different data set, you may get the result you want. My code should work with it without modification, if the variables in the Excel file have the same names and the same positions in the file.
.
Thanks so much....This looks better
Star Strider
Star Strider el 12 de Dic. de 2022
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (0)

Productos

Versión

R2020b

Preguntada:

el 12 de Dic. de 2022

Comentada:

el 12 de Dic. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by