How to plot pcolor in matlab

Hello everyone,
i want to plot pcolor plot in matlab but i tried i cannot do it. I add my data here and code also
x = [13-04-2021 00:00 13-04-2021 01:00 13-04-2021 02:00 13-04-2021 03:00 13-04-2021 04:00] like this till 14-04-2021 23:00:00
y = [10.23 11.16 12.19 13.31 14.54 15.88 17.35 18.96 20.72 22.66 24.77 27.1 29.65 32.45 35.53 38.92 42.65 46.77 51.32]
z = [2.27E+03 3.94E+03 4.48E+03 6.26E+03 3.18E+03 3.78E+03 5.37E+03 5.60E+03 6.35E+03 8.47E+03 1.05E+04 1.05E+04
1.36E+04 1.12E+04 8.48E+03 3.96E+03 5.84E+03 4.55E+03 3.65E+03 6.17E+03 7.48E+03 6.98E+03 9.84E+03 1.11E+04
5.72E+04 6.90E+04 5.64E+04 4.71E+04 1.94E+04 6.37E+03 6.59E+03 6.89E+03 8.81E+03 9.70E+03 8.11E+03 9.59E+03
1.26E+04 1.34E+04 1.69E+04 1.99E+04 2.18E+04 2.62E+04 2.98E+04 9.09E+03 5.16E+03 8.73E+03 3.96E+03 4.95E+03
5.16E+03 4.06E+03 4.24E+03 6.17E+03 7.99E+03 8.20E+03 6.68E+03 1.07E+04 1.24E+04 1.36E+04 1.78E+04 1.89E+04]
my x data is in one range and y is in another range and my z data is in different size range.
how to plot that graph?
i hope i will get the answer for this thank you in advance.
[x,y] = meshgrid(x,y);
Pcolor(x,y,z);

5 Comments

You need to find a way to match up your data. It is not clear to me how you are planning to do that. How are you making sure you have as many z values as you have x-y-combinations?
xd = {'13-04-2021 00:00' '14-04-2021 23:00'};
xdt = datetime(xd, 'InputFormat', 'dd-MM-yyyy HH:mm');
x = xdt(1) : hours(1) : xdt(2);
y = [10.23 11.16 12.19 13.31 14.54 15.88 17.35 18.96 20.72 22.66 24.77 27.1 29.65 32.45 35.53 38.92 42.65 46.77 51.32];
z = [2.27E+03 3.94E+03 4.48E+03 6.26E+03 3.18E+03 3.78E+03 5.37E+03 5.60E+03 6.35E+03 8.47E+03 1.05E+04 1.05E+04
1.36E+04 1.12E+04 8.48E+03 3.96E+03 5.84E+03 4.55E+03 3.65E+03 6.17E+03 7.48E+03 6.98E+03 9.84E+03 1.11E+04
5.72E+04 6.90E+04 5.64E+04 4.71E+04 1.94E+04 6.37E+03 6.59E+03 6.89E+03 8.81E+03 9.70E+03 8.11E+03 9.59E+03
1.26E+04 1.34E+04 1.69E+04 1.99E+04 2.18E+04 2.62E+04 2.98E+04 9.09E+03 5.16E+03 8.73E+03 3.96E+03 4.95E+03
5.16E+03 4.06E+03 4.24E+03 6.17E+03 7.99E+03 8.20E+03 6.68E+03 1.07E+04 1.24E+04 1.36E+04 1.78E+04 1.89E+04];
size(x)
ans = 1×2
1 48
size(y)
ans = 1×2
1 19
size(z)
ans = 1×2
5 12
Your data is all different sizes; we cannot test with it, and we cannot figure out what you are trying to do.
Hello walter roberson i had attached some reference plot which one i need to try out using this data. I need my x data will come in x-axis and in my y-axis i need my y data from 10 to 50 and my z-axis my z data will come and matrix format.
Hello Rik i had attached reference graph just have a look then you will get some idea
Hello Walter sir i had attached some sample data sheet also. my column name should come in my y-axis date will come in my x-axis remaining all will come in z-axis.
i hope you will help me to come out from this sir i tired many way.
Thank you

Sign in to comment.

Answers (1)

filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/828415/Book1.xlsx';
T = readcell(filename);
x = [T{2:end,1}];
y = cell2mat(T(1,2:end));
z = cell2mat(T(2:end,2:end));
surf(x, y, z.', 'edgecolor', 'none');

34 Comments

Hai sir I tried but it shows undefined function or variable 'readcell'. I am using R2016a matlab
Then use xlsread() instead.
Hai sir I tried T = xlsread(Book1) but it shows error message. Error using xlsread (line 125) File name must be a string
Book1 = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/828415/Book1.xlsx';
%this line is only required if you're downloading the file, instead of
%using a local copy
Book1=websave('Book1.xlsx',Book1);
T = xlsread(Book1)
T = 85×20
1.0e+05 * NaN 0.0001 0.0001 0.0001 0.0001 0.0001 0.0002 0.0002 0.0002 0.0002 0.0002 0.0002 0.0003 0.0003 0.0003 0.0004 0.0004 0.0004 0.0005 0.0005 0.4431 0.0151 0.0091 0.0224 0.0156 0.0176 0.0182 0.0349 0.0275 0.0350 0.0307 0.0407 0.0434 0.0530 0.0514 0.0627 0.0736 0.0653 0.0668 0.0679 0.4431 0.0284 0.0228 0.0224 0.0198 0.0248 0.0204 0.0250 0.0293 0.0350 0.0489 0.0350 0.0401 0.0505 0.0486 0.0641 0.0668 0.0708 0.0680 0.0634 0.4431 0.0284 0.0243 0.0286 0.0282 0.0186 0.0212 0.0297 0.0269 0.0238 0.0316 0.0320 0.0428 0.0506 0.0476 0.0574 0.0607 0.0683 0.0654 0.0613 0.4431 0.0132 0.0136 0.0212 0.0187 0.0230 0.0258 0.0256 0.0292 0.0345 0.0356 0.0379 0.0474 0.0501 0.0550 0.0608 0.0634 0.0653 0.0762 0.0631 0.4431 0.0290 0.0263 0.0266 0.0250 0.0236 0.0340 0.0262 0.0310 0.0284 0.0318 0.0448 0.0415 0.0400 0.0548 0.0479 0.0628 0.0604 0.0693 0.0702 0.4431 0.0398 0.0349 0.0473 0.0260 0.0274 0.0342 0.0430 0.0362 0.0311 0.0369 0.0283 0.0477 0.0422 0.0572 0.0577 0.0659 0.0640 0.0640 0.0718 0.4431 0.0170 0.0182 0.0149 0.0093 0.0204 0.0251 0.0236 0.0263 0.0233 0.0261 0.0385 0.0418 0.0479 0.0545 0.0543 0.0587 0.0628 0.0654 0.0660 0.4431 0.0208 0.0288 0.0099 0.0229 0.0123 0.0181 0.0303 0.0288 0.0335 0.0389 0.0406 0.0340 0.0482 0.0491 0.0525 0.0542 0.0564 0.0611 0.0685 0.4431 0.0132 0.0152 0.0224 0.0156 0.0257 0.0235 0.0336 0.0217 0.0310 0.0375 0.0367 0.0398 0.0448 0.0481 0.0489 0.0546 0.0592 0.0676 0.0619
Thank you rik
When I entered y=cell2mat(T(1,2:end));it shows error message Error using cell2mat (line42) You cannot subscript a table using only one subscript. Table subscripting requires both row and variable subscrpts. I don't know next what I have to do ????
Book1 needs to be a string, and apparently it's not. Try
fullFileName = fullfile(pwd, 'Book1.xlsx');
if isfile(fullFileName)
[numbers, strings, T] = xlsread(fullFileName);
else
warningMessage = sprintf('File not found:\n%s', fullFileName);
uiwait(errordlg(warningMessage));
return;
end
T will be the raw cell array while numbers is just the numbers (with nan wherever there is a string in the matrix), and strings is a cell array with just the text cells.
I tried sir but it shows Undefined function 'isfile' for input arguments of type 'char'
You must have a really old version. Use exist() instead:
if exist(fullFileName, 'file')
Thank you Image Analyst
when i entered surf(x, y, z.', 'edgecolor', 'none'); it show errow message.
Error using matlab.graphics.chart.primitive.Surface/set
Invalid parameter/value pair arguments.
Error in matlab.graphics.chart.internal.ctorHelper (line 6)
set(obj, pvpairs{:});
Error in matlab.graphics.chart.primitive.Surface
Error in surf (line 126)
hh = matlab.graphics.chart.primitive.Surface(allargs{:});
The above Error message is pop up.
Full code i showed you
T = Book1;
fullFileName = fullfile(pwd, 'Book1.xlsx');
if exist(fullFileName, 'file')
[numbers, strings, T] = xlsread(fullFileName);
else
warningMessage = sprintf('File not found:\n%s', fullFileName);
uiwait(errordlg(warningMessage));
return;
end
x = [T{2:end,1}];
y = cell2mat(T(1,2:end));
z = cell2mat(T(2:end,2:end));
surf(x, y, z.', 'edgecolor', 'none');
Now i am standing in last line of the code. Help me to figure it out. Thank you so much i advance.
surf(x, y, z.');
Hai sir
Again i got same error
Error using matlab.graphics.chart.primitive.Surface/set
Invalid parameter/value pair arguments.
Error in matlab.graphics.chart.internal.ctorHelper (line 6)
set(obj, pvpairs{:});
Error in matlab.graphics.chart.primitive.Surface
Error in surf (line 126)
hh = matlab.graphics.chart.primitive.Surface(allargs{:});
please post your current code
T = Book1;
fullFileName = fullfile(pwd, 'Book1.xlsx');
if exist(fullFileName, 'file')
[numbers, strings, T] = xlsread(fullFileName);
else
warningMessage = sprintf('File not found:\n%s', fullFileName);
uiwait(errordlg(warningMessage));
return;
end
x = [T{2:end,1}];
y = cell2mat(T(1,2:end));
z = cell2mat(T(2:end,2:end));
surf(x, y, z.');
Error Message
Error using matlab.graphics.chart.primitive.Surface/set
Invalid parameter/value pair arguments.
Error in matlab.graphics.chart.internal.ctorHelper (line 6)
set(obj, pvpairs{:});
Error in matlab.graphics.chart.primitive.Surface
Error in surf (line 126)
hh = matlab.graphics.chart.primitive.Surface(allargs{:});
This is my current code sir
Till z the code went good i think i didnt receive any Error message
But when i enter Surf code i got the error
The code works when I try it in R2016a on my system.
To debug this, give the commands
dbstop in matlab.graphics.chart.internal.ctorHelper
surf(x, y, z.')
The code should run, but stop in ctorHelper.m . When it stops, please show us the value of the variable named pvpairs -- and also please show us class(obj)
Hai sir,
pvpairs show ['Parent' 1x1 Axes
'''21-04-202121-04-2021 00:15:0021-04-2021 00:30:0021-04-2021 00:45:0021-04-2021 01:00:0021-04-2021 01:15:0021-04-2021 01:30:0021-0...'' <Preview truncated at 128 characters>' 1x19 double
19x84 double]
obj shows 1 x 1 Surface
After entered surf(x,y,z.') it shows below code
5 if ~isempty(pvpairs)
K>>
Function:
function ctorHelper(obj, pvpairs)
% Copyright 2014-2015 The MathWorks, Inc.
if ~isempty(pvpairs)
set(obj, pvpairs{:});
end
end
Please show the output for
which -all surf
Your pvpairs are not even in the same order that I would expect for your release. I would expect
'XData' [1x84 double] 'YData' [1x19 double] 'ZData' [19x84 double] 'Parent' [1x1 Axes]
>> surf(x, y, z.')
5 if ~isempty(pvpairs)
K>> which -all surf
C:\Program Files\MATLAB\R2016a\toolbox\matlab\graph3d\surf.m
C:\Program Files\MATLAB\R2016a\toolbox\mbc\mbctools\@sweepset\surf.m % Shadowed sweepset method
K>>
It came like this Sir.
once when i entered above code, it will remove all x,y,z values from my workspace and shows only obj,pvpairs.
Dear all,
I need your help. I have wind data in 2D (Height, Time). I want to plot a 60-minute average data, and I want to do that using pcolor plot. I managed to generate the plot with the original data (30 min), but failed to do it on a 60-min average. I am relatively new to netcdf data. Any help is greatly appreciated!
Here is my code for original data:
clear all
close all
clc
ncid = netcdf.open('wl_geomar_CSM_l2_v04_20240916.nc','NC_NOWRITE');
filename = ncinfo('wl_geomar_CSM_l2_v04_20240916.nc');
disp(filename);
ncdisp('wl_geomar_CSM_l2_v04_20240916.nc');
longitude = ncread('wl_geomar_CSM_l2_v04_20240916.nc','lon');
latitude = ncread('wl_geomar_CSM_l2_v04_20240916.nc','lat');
time = ncread('wl_geomar_CSM_l2_v04_20240916.nc','time');
epoch = datetime(1970, 01, 01)
T = epoch + seconds(time)
u = ncread('wl_geomar_CSM_l2_v04_20240916.nc','u');
v = ncread('wl_geomar_CSM_l2_v04_20240916.nc','v');
wspeed = ncread('wl_geomar_CSM_l2_v04_20240916.nc','wspeed');
height = ncread('wl_geomar_CSM_l2_v04_20240916.nc','height');
u_mean=sqrt(u.^2+v.^2+wspeed.^2);
mymap=pcolor(T,height,u_mean);
shading interp
h= colorbar
caxis([0 20])
drawnow
ylabel(h,'Mean wind speed (m/s)');
grid on
xlabel('Time (UTC)')
ylabel('Height (m)')
xtk=xticks;
I would be happy to provide you the data if needed.
What does "failed to do it on a 60-min average" mean in this context?
  • Do you receive warning and/or error messages? If so the full and exact text of those messages (all the text displayed in orange and/or red in the Command Window) may be useful in determining what's going on and how to avoid the warning and/or error.
  • Does it do something different than what you expected? If so, what did it do and what did you expect it to do?
  • Did MATLAB crash? If so please send the crash log file (with a description of what you were running or doing in MATLAB when the crash occured) to Technical Support so we can investigate.
TT = timetable(T, u, v, wspeed, height);
TT60 = retime(TT, minutes(60));
u_mean60 = sqrt(TT60.u.^2 + TT60.v.^2);
mymap60 = pcolor(TT60.T, TT60.height, TT60.u_mean60);
Thank you so much. Here is the error message I am getting after I tried to plot it using your code:
" Error using timetable (line 328)
All table variables must have the same number of rows."
Please show the output for
whos T u v wspeed height
Here is the output:
>> whos T u v wspeed height
Name Size Bytes Class Attributes
T 48x1 384 datetime
height 99x1 792 double
u 99x48 38016 double
v 99x48 38016 double
wspeed 99x48 38016 double
@Walter Roberson, I can also share the data with you. Just let me know if you need it.
u = u.';
v = v.';
wspeed = wspeed.';
TT = timetable(T, u, v, wspeed);
TT60 = retime(TT, minutes(60));
u_mean60 = sqrt(TT60.u.^2 + TT60.v.^2);
mymap60 = pcolor(TT60.T, height, TT60.u_mean60);
I am still getting an error message, "Error using timetable/retime (line 142)
All input timetables must have row times with the same data type as target time
vector."
Is that error message coming from the retime() call?
If so, try
TT60 = retime(TT, 'regular', 'linear', 'timestep', minutes(60));
Dear @Walter Roberson, Thank you so much. I made a little modification and it works now.
Here is the final one:
u = u.';
v = v.';
wspeed = wspeed.';
TT = timetable(T, u, v, wspeed);
TT60 = retime(TT, 'regular', 'linear', 'timestep', minutes(60));
%TT60 = retime(TT, minutes(60));
u_mean60 = sqrt(TT60.u.^2 + TT60.v.^2+TT60.wspeed.^2);
mymap60 = pcolor(TT60.T, height, u_mean60').
Thank you again for helping me. I am now trying to write another code for the same dataset (this time I have the python code but I need to trasnlate that into Matlab code as I do not use Python). Could you please help me with is part too? I really apperciate your help.
Here is the python that I need to re-write in Matlab.
#filter signals for plotting, which are only marginally above molecular signal
bsc_mask = bsc_par_prl < np.repeat(np.reshape(1.5*mol_bsc, (1,len(mol_bsc))), att_bsc_crs_avg.shape[0], axis=0)
depol_par_masked = np.ma.array(depol, mask=bsc_mask)
bsc_par_masked = np.ma.array(bsc_par_prl, mask=bsc_mask)
Sorry, I do not know how to read python. In particular I do not know the meaning of np.ma.array especially in conjugation with the mask= parameter.
@Walter Roberson, No worries. I managed to write the code.

Sign in to comment.

Categories

Products

Release

R2016a

Asked:

on 9 Dec 2021

Commented:

on 31 Dec 2024

Community Treasure Hunt

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

Start Hunting!