You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Trying to create a filled colour contour plot. Specifically in the form of a depth profile.
3 views (last 30 days)
Show older comments
Joshua
on 2 Mar 2011
Hello, I am trying to create a depth profile of a stretch of water, with X being Distance, Y being Depth and Z being the coloured fill for Salinity.
I've found it possible to create an X-Y plot of the depth and distance but can't figure how to add the Z and get it as the coloured fill.
I have however already looked at the graph tools section and inputted x, y and z variable inputs, but it still only gives me a graph of x or y.
FYI. each variable was originally in one variable ('num') but i have since seperated into 'Distance','Depth','Salinity'. Also all columns have data of the same length (1;332). Can anyone help me?
10 Comments
Sarah Wait Zaranek
on 3 Mar 2011
Do you want a filled contour plot (aka contourf)? Or plots x vs y with z being the color of the points (scatter(x,y,z)? Or something else?
Joshua
on 4 Mar 2011
when using contourf for some reason it tells me that the 'index exceeds matrix dimensions'.
I tried scattering the x vs y to see whether the data was a problem, and that worked fine.
However scattering x vs z or y vs z results in an error. Can you think what I may have done wrong?
Walter Roberson
on 4 Mar 2011
Please double check that z is the same size and shape as x and y.
You could try scatter3(x,y,z) if the sizes are the same
Joshua
on 4 Mar 2011
>> whos x y z
Name Size Bytes Class
x 100x1 800 double array
y 100x1 800 double array
z 100x1 800 double array
Grand total is 300 elements using 2400 bytes
Joshua
on 4 Mar 2011
scatter(x,z) does now give a graph, sorry, possibly as a result of clearing some previous code.
Accepted Answer
Matt Tearle
on 4 Mar 2011
For an older version of MATLAB (without TriScatterdInterp):
x = rand(100,1)*4-2;
y = rand(100,1)*4-2;
z = x.*exp(-x.^2-y.^2);
[qx,qy] = meshgrid(linspace(min(x),max(x)),linspace(min(y),max(y)));
qz = griddata(x,y,z,qx,qy);
contourf(qx,qy,qz)
11 Comments
Walter Roberson
on 4 Mar 2011
Matt, that works if your existing data is gridded but the grid is irregular, but the OP's data is just scattered, not on a grid at all.
Joshua
on 4 Mar 2011
this forms a 4x4 ((-2 to 2)x(-2 to 2)) whereas the data would imply 4 x 12000 or so, also the filled colour data doesn't seem to represent the z data.
Matt Tearle
on 5 Mar 2011
@WR: I don't understand the objection. In my example, x & y are vectors of random points. I don't see how that's any different to Joshua's data, as he's described it. If I do scatter(x,y) I don't see any gridding beyond that which you'd get for any finite data set.
@Joshua: did you run what I posted as it's written? I have no idea where you're getting those numbers (4 & 12000) from. My example creates two vectors of random points, then generates a third vector of z values according to some formula. It then makes a regular grid of finely-spaced points (100-by-100) in the same range as the original data, interpolates the z data onto that grid, and plots the result. To see that the result makes sense, try this
figure
surfc(qx,qy,qz,'facealpha',0.8,'linestyle','none')
hold on
scatter3(x,y,z,'markerfacecolor','b')
Joshua
on 5 Mar 2011
the 4 & 12000 approx where referring to my data ranges, the plot shows a 3D cube whose axis are range -2 to 2. This gives me a 3D cube, but what i was trying to get is a flat interpretation of x,y and z. With x being the x axis, y the y axis, and z the colour fill. Hope that makes sense.
Matt Tearle
on 5 Mar 2011
Unless I misunderstand what you mean, that's what |contourf| does (a plot in x-y axes with colors filling between the z-value contours). I added the |surfc| and |scatter3| example because you said the filled color didn't seem to represent the data. Perhaps you're thinking more of something like
imagesc(qx,qy,qz)
As for the data ranges, MATLAB automatically scales the axes to the data range. Maybe you want to set the aspect ratio of the axes? I can't see that an aspect ratio of 4:12000 is going to give anything visually appealing, but if you want to mess with it so it's not square, use
get(gca,'DataAspectRatio')
to see the current setting, and
set(gca,'DataAspectRatio',[1 20 1])
to change it to something else.
Joshua
on 5 Mar 2011
referring to answer 1; I attempted to use the contourf function but it came out with 'index exceeds matrix dimensions' which is what I couldn't understand how to overcome.
Matt Tearle
on 5 Mar 2011
Can you please show the full sequence of commands that resulted in the error?
Joshua
on 5 Mar 2011
>>clear all
% input data variables x,y,z
>> contourf(x,y,z)
??? Index exceeds matrix dimensions.
Warning: Error occurred while evaluating listener callback.
> In contourf at 40
Joshua
on 5 Mar 2011
% x values
2333.96
2333.96
2333.96
2333.96
2333.96
2333.96
2333.96
2333.96
2333.96
2333.96
2333.96
2333.96
2333.96
2333.96
2333.96
2333.96
2333.96
2333.96
2333.96
2333.96
2333.96
2333.96
2333.96
2333.96
2333.96
2333.96
2333.96
2333.96
2333.96
2333.96
2333.96
3384.228
3384.228
3384.228
3384.228
3384.228
3384.228
3384.228
3384.228
3384.228
3384.228
3384.228
3384.228
3384.228
3384.228
3384.228
3384.228
3384.228
3384.228
3384.228
4689.437
4689.437
4689.437
4689.437
4689.437
4689.437
4689.437
4689.437
4689.437
4689.437
4689.437
4689.437
4689.437
4689.437
4689.437
4689.437
4689.437
4689.437
4689.437
4689.437
4689.437
4689.437
4689.437
4689.437
4689.437
4689.437
4689.437
4689.437
4689.437
4689.437
4689.437
4689.437
4689.437
4689.437
4689.437
4689.437
4689.437
4689.437
4689.437
5488.413
5488.413
5488.413
5488.413
5488.413
5488.413
5488.413
5488.413
5488.413
5488.413
5488.413
5488.413
5488.413
5488.413
5488.413
5488.413
5488.413
5488.413
5488.413
5488.413
5488.413
5488.413
5488.413
6539.973
6539.973
6539.973
6539.973
6539.973
6539.973
6539.973
6539.973
6539.973
6539.973
6539.973
6539.973
6539.973
6539.973
6539.973
6539.973
6539.973
6539.973
6539.973
6539.973
6539.973
7216.716
7216.716
7216.716
7216.716
7216.716
7216.716
7216.716
7216.716
7216.716
7216.716
7216.716
7216.716
7216.716
7216.716
7216.716
7216.716
7216.716
7216.716
7216.716
7216.716
7835.407
7835.407
7835.407
7835.407
7835.407
7835.407
7835.407
7835.407
7835.407
7835.407
7835.407
7835.407
7835.407
7835.407
7835.407
7835.407
7835.407
7835.407
8148.825
8148.825
8148.825
8148.825
8148.825
8148.825
8148.825
8148.825
8148.825
8148.825
8148.825
8148.825
8148.825
8148.825
8148.825
8148.825
8148.825
8148.825
8148.825
8148.825
8148.825
8148.825
8148.825
8679.832
8679.832
8679.832
8679.832
8679.832
8679.832
8679.832
8679.832
8679.832
8679.832
8679.832
8679.832
8679.832
8679.832
8679.832
8679.832
8679.832
8679.832
8679.832
8679.832
8679.832
8679.832
8679.832
8679.832
8679.832
8679.832
8679.832
9135.001
9135.001
9135.001
9135.001
9135.001
9135.001
9135.001
9135.001
9135.001
9135.001
9135.001
9135.001
9135.001
9135.001
9135.001
9355.322
9355.322
9355.322
9355.322
9355.322
9355.322
9355.322
9355.322
9355.322
9355.322
9355.322
9355.322
9355.322
9355.322
9355.322
9691.066
9691.066
9691.066
9691.066
9691.066
9691.066
9691.066
9691.066
9691.066
9691.066
9691.066
9691.066
9691.066
9691.066
9691.066
9691.066
9691.066
9691.066
9691.066
9691.066
10264.31
10264.31
10264.31
10264.31
10264.31
10264.31
10264.31
10264.31
10264.31
10264.31
10264.31
10264.31
10264.31
10264.31
10264.31
10264.31
10264.31
10264.31
10264.31
10264.31
10264.31
10264.31
10264.31
10264.31
10264.31
10264.31
10978.62
10978.62
10978.62
10978.62
10978.62
10978.62
10978.62
10978.62
10978.62
10978.62
10978.62
10978.62
10978.62
10978.62
10978.62
10978.62
10978.62
11585.55
11585.55
11585.55
11585.55
11585.55
11585.55
11585.55
11585.55
11585.55
11585.55
11585.55
11585.55
11585.55
11585.55
11585.55
11585.55
11585.55
11585.55
% y values
3.599
3.467
3.397
3.326
3.211
3.123
3.07
3.044
3.009
2.956
2.947
2.938
2.894
2.885
2.876
2.859
2.841
2.815
2.806
2.779
2.726
2.585
2.215
1.739
1.218
0.68
0.274
0.053
0.027
0.009
0.00001
3.122
2.911
2.867
2.814
2.734
2.664
2.646
2.629
2.576
2.593
2.558
2.426
2.161
1.755
1.244
0.732
0.326
0.061
0.00001
2.478
2.354
2.302
2.266
2.213
2.161
2.116
2.081
2.072
2.046
1.993
1.949
1.896
1.87
1.861
1.834
1.808
1.79
1.773
1.755
1.711
1.543
1.138
0.82
0.617
0.582
0.573
0.511
0.467
0.441
0.423
0.388
0.379
0.361
0.353
0.344
0.326
0.15
0.00001
1.199
0.988
0.917
0.891
0.794
0.688
0.662
0.626
0.609
0.582
0.565
0.485
0.459
0.441
0.415
0.406
0.335
0.282
0.247
0.08
0.027
0.009
0.00001
1.322
1.111
1.014
0.935
0.882
0.846
0.838
0.785
0.723
0.705
0.697
0.67
0.635
0.617
0.476
0.317
0.247
0.203
0.079
0.035
0.00001
1.605
1.288
1.076
0.979
0.953
0.935
0.917
0.9
0.882
0.865
0.856
0.847
0.785
0.688
0.538
0.371
0.274
0.159
0.044
0.00001
0.917
0.706
0.697
0.679
0.662
0.644
0.635
0.618
0.609
0.591
0.556
0.503
0.485
0.441
0.327
0.203
0.053
0.00001
1.155
0.953
0.732
0.653
0.6
0.582
0.565
0.556
0.521
0.45
0.432
0.388
0.344
0.327
0.309
0.3
0.282
0.274
0.221
0.141
0.044
0.018
0.00001
1.763
1.525
1.331
1.296
1.278
1.226
1.19
1.173
1.129
1.076
1.049
1.023
1.005
0.987
0.961
0.952
0.943
0.908
0.899
0.802
0.52
0.194
0.061
0.044
0.035
0.026
0.00001
1.146
0.802
0.591
0.485
0.467
0.441
0.414
0.379
0.37
0.291
0.088
0.053
0.044
0.026
0.00001
1.799
1.623
1.393
1.235
1.147
1.138
1.12
1.103
0.873
0.556
0.212
0.15
0.106
0.018
0.00001
1.464
1.341
1.129
0.918
0.829
0.812
0.785
0.75
0.724
0.706
0.697
0.688
0.679
0.653
0.53
0.318
0.265
0.186
0.036
0.00001
3.034
3.026
2.955
2.779
2.594
2.356
2.25
2.197
2.162
2.144
2.126
2.118
2.1
2.091
2.073
2.065
1.932
1.694
1.377
0.556
0.292
0.212
0.133
0.036
0.009
0.00001
1.428
1.331
1.252
1.128
1.04
0.926
0.793
0.776
0.767
0.749
0.688
0.652
0.52
0.326
0.255
0.114
0.00001
2.187
2.178
2.073
1.843
1.623
1.552
1.508
1.473
1.455
1.447
1.42
1.411
1.394
1.138
0.688
0.3
0.115
0.00001
% z values
33.431
33.42
33.409
33.398
33.41
33.398
33.406
33.406
33.398
33.397
33.398
33.391
33.389
33.387
33.389
33.382
33.384
33.388
33.391
33.382
33.403
33.406
33.404
33.39
33.382
33.377
33.375
33.375
33.373
33.37
33.374
24.883
25.163
25.297
25.482
25.197
25.223
26.802
26.739
26.737
26.395
26.501
26.614
28.404
29.424
29.633
29.632
29.646
29.651
29.659
19.708
19.669
19.599
19.418
19.696
19.713
19.72
19.735
19.741
19.744
19.74
19.606
19.701
19.622
19.653
19.605
19.553
19.563
19.593
19.644
19.197
19.445
19.84
20.056
20.04
20.07
20.033
20.053
20.071
20.075
20.098
20.109
20.126
20.13
20.116
20.141
20.067
19.871
19.908
4.9039
5.0859
4.7893
4.742
4.7973
4.7324
4.7231
4.7562
4.7279
4.7244
4.7289
4.7425
4.7182
4.7394
4.7151
4.7215
4.7128
4.7042
4.7051
4.7051
4.702
4.7032
4.7009
0.3186
0.3075
0.3245
0.3228
0.3219
0.3228
0.3309
0.3282
0.3373
0.349
0.3509
0.35
0.3508
0.3517
0.3482
0.3436
0.3454
0.3418
0.3391
0.3382
0.3373
0.1059
0.1051
0.106
0.1068
0.1068
0.1069
0.1069
0.1069
0.1069
0.1069
0.1094
0.1103
0.1094
0.1094
0.1154
0.118
0.118
0.118
0.1189
0.1189
0.1693
0.1711
0.1721
0.1722
0.1722
0.1696
0.1705
0.1679
0.1627
0.1601
0.1601
0.1619
0.1419
0.1488
0.1428
0.1384
0.1376
0.1376
0.0476
0.0474
0.0472
0.048
0.0472
0.0489
0.0472
0.0472
0.048
0.0489
0.0472
0.048
0.0472
0.0472
0.0472
0.048
0.048
0.048
0.048
0.0472
0.0472
0.0472
0.0472
0.0399
0.0399
0.0399
0.0407
0.0407
0.0407
0.0407
0.0407
0.0407
0.0399
0.0407
0.0407
0.0407
0.0407
0.0407
0.0407
0.0407
0.0407
0.0399
0.0399
0.0399
0.0399
0.0399
0.0399
0.0399
0.0399
0.0399
0.0342
0.0342
0.0342
0.0342
0.0342
0.0342
0.0342
0.0342
0.0342
0.0342
0.0342
0.0342
0.0342
0.0342
0.0342
0.0318
0.0326
0.0326
0.0326
0.0326
0.0318
0.0326
0.0318
0.0326
0.0326
0.0326
0.0326
0.0326
0.0326
0.0326
0.0318
0.0318
0.0318
0.0318
0.0318
0.0318
0.0318
0.0318
0.0318
0.0318
0.0318
0.0318
0.0318
0.0318
0.0318
0.0318
0.0318
0.0318
0.0318
0.0318
0.027
0.027
0.027
0.027
0.027
0.027
0.027
0.027
0.027
0.027
0.027
0.027
0.027
0.027
0.027
0.027
0.0278
0.0294
0.0294
0.0302
0.0302
0.0302
0.031
0.0302
0.0302
0.0302
0.0262
0.0262
0.0262
0.0262
0.0262
0.0262
0.0262
0.0262
0.0262
0.0262
0.0262
0.0262
0.0262
0.0262
0.0262
0.0262
0.0262
0.0254
0.0246
0.0254
0.0254
0.0254
0.0254
0.0254
0.0254
0.0254
0.0254
0.0254
0.0254
0.0254
0.0254
0.0254
0.0254
0.0254
0.0254
Matt Tearle
on 5 Mar 2011
??? Why are you using contourf on the original data?
% load x y z data
[qx,qy] = meshgrid(linspace(min(x),max(x)),linspace(min(y),max(y)));
qz = griddata(x,y,z,qx,qy);
contourf(qx,qy,qz)
Works fine for me, using the x, y, z values you've posted.
More Answers (1)
Matt Tearle
on 3 Mar 2011
Something like this, perhaps:
% invent some x,y,z data
x = rand(100,1)*4-2;
y = rand(100,1)*4-2;
z = x.*exp(-x.^2-y.^2);
% interpolate onto regular grid
F = TriScatteredInterp(x,y,z);
[qx,qy] = meshgrid(linspace(min(x),max(x)),linspace(min(y),max(y)));
qz = F(qx,qy);
% filled contour plot
contourf(qx,qy,qz)
5 Comments
Joshua
on 4 Mar 2011
I tried this but it just came out with 'Undefined command/function 'TriScatteredInterp'.'
Matt Tearle
on 4 Mar 2011
Ah, I'm guessing you have an old version of MATLAB? In that case, delete that line (obviously), and replace "qz = F(...)" with
qz = griddata(x,y,z,qx,qy);
Joshua
on 4 Mar 2011
it still says:
??? Undefined command/function 'TriScatteredInterp'.
Error in ==> a at 7
F = TriScatteredInterp(x,y,z);
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)