2D Plot of CSV data
조회 수: 5 (최근 30일)
이전 댓글 표시
I would like to create a 2D plot with a colored surface (i.e. a xy coordinate system where the color indicates a temperature at each point). I did this before by using the contourf function, however, this function requires three matrices with the x, y, and color data of each point as entry.
The problem here: my data comes from a CSV file, where each line has three entries: x coordinate, y coordinate and temperature. Example:
X Y T (K)
31.999999999999964 0.19038773875061182 323.5661344306932
31.980976178007637 0.20000000000002974 323.58694805436954
31.999999999999957 0.20000000000003018 323.58694573460093
Unfortunately these coordinates don't follow a regular pattern and there is no regular grid I could generate from this data. So how can I generate a color plot from this?
(Regardless of my current problem: why does the countourf function require X and Y matrices? I found this annoying several times in the past when I had data given as a CSV with coordinates and data in one line, but usually my data had a regular grid as a basis and I could just generate the X and Y matrices using meshgrid(). However, even if I had a regular spacing for my x and y coordinates, I would still have to dig into my data to determine the periodicy of the grid first and as a bonus I will get various occasions to mess up x/y directions, table indices, etc. until I finally get it done correctly.)
댓글 수: 0
답변 (2개)
Voss
2022년 1월 2일
One way you can do it (still with contourf()) is to use a scatteredInterpolant to interpolate and/or extrapolate T from the X,Y points where T is defined to all X,Y points in a grid:
X = [32; 31.98; 32];
Y = [0.19; 0.2; 0.2];
T = [323.566; 323.587; 323.587];
I = scatteredInterpolant(X,Y,T)
xc = 30:0.1:34;
yc = 0.17:0.01:0.22;
[XX,YY] = meshgrid(xc,yc);
figure();
contourf(XX,YY,I(XX,YY));
colorbar();
Another example with more points (random data this time):
X = 31+2*rand(50,1)-1;
Y = 0.2+0.2*rand(50,1)-0.1;
T = 323.5+2*rand(50,1)-1;
I = scatteredInterpolant(X,Y,T)
xc = 30:0.1:32;
yc = 0.1:0.01:0.3;
[XX,YY] = meshgrid(xc,yc);
figure();
contourf(XX,YY,I(XX,YY));
colorbar();
댓글 수: 0
Star Strider
2022년 1월 2일
One approach —
M = [31.999999999999964 0.19038773875061182 323.5661344306932
31.980976178007637 0.20000000000002974 323.58694805436954
31.999999999999957 0.20000000000003018 323.58694573460093];
T1 = array2table(M, 'VariableNames',{'X','Y','T (K)'})
N = 6;
xv = linspace(min(T1.X), max(T1.X), N);
yv = linspace(min(T1.Y), max(T1.Y), N);
[Xm,Ym] = ndgrid(xv,yv);
TKm = griddata(T1.X, T1.Y, T1.('T (K)'), Xm, Ym);
figure
surfc(Xm, Ym, TKm)
grid on
xlabel('X')
ylabel('Y')
zlabel('T (K)')
There is not much to plot with only 3 data, however if more data are avaialble, this should work with them without modifying the code. Increase ‘N’ to provide effective resolution.
.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Contour Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


