Plot columns of data in discrete datapoints?

Hello,
I am looking to create a plot that creates vertically-connected lines of data over discrete datapoints. So, for example, if I have a matrix such as
Z = [1 2 1;
2 2 1;
1 1 3]
then I would like to make a plot that looks something like this:
where the numbers at each vertex represent that "x" value for that discrete datapoint.
I have tried using "contour(Z)" however this does not produce the results I am looking for.
Any help is greatly appreciated.

 Accepted Answer

Try this —
Z = [1 2 1;
2 2 1;
1 1 3];
Y = size(Z,2):-1:1;
figure
hold on
for k = 1:numel(Y)
X = Z(k,:)+2*(k-1);
plot(X, Y, 'LineWidth',2)
text(X(end), Y(end), compose(' %.0f',Z(k,end)), 'Horiz','left', 'Vert','bottom')
text(X(1:end-1), Y(1:end-1), compose(' %.0f',Z(k,1:end-1)), 'Horiz','left', 'Vert','middle')
end
hold off
grid
xlim([-1 1]+xlim)
Experiment with other ‘Z’ matrices. The ‘Y’ vector should adapt to all of them if they are similar in structure to the one provided.
.

2 Comments

Wow this is very close to what I need, thank you very much! The only problem now is that my actual data is much more complex than the example I gave, and the lines are not quite behaving how I would expect.
For instance, the lower ends of each line are sloping off to the right, although the bottom column is not any bigger than the rest on average. Do you have any idea why this might be occuring? Here is the plot that I got with my data:
I've changed the 2* in the "for" loop to 20* so that the lines do not overlap, could this be causing the issue?
I've pasted my actual Z matrix below for reference. Once again any help is much appreciated!!
6.89802950000001 28.9361840000000 43.6304410000000 43.8531870000000 44.0390865000000 44.1954830000000 44.3385180000000 44.5337445000000 45.3732045000000 45.6237585000000 45.7038675000000 45.8610375000000 46.7153150000000 48.8962465000000 50.8511520000000 72.6838415000000
6.69302949999999 26.0815615000000 39.9511045000000 41.5139480000000 41.9944020000000 42.4394105000000 42.6730865000000 42.9532755000000 43.7100375000000 43.8761795000000 44.0134950000000 44.1593785000000 44.3030425000000 44.5025020000000 47.8452200000000 78.7887060000000
13.3380295000000 25.9993330000000 40.1603615000000 43.4413890000000 43.8530155000000 44.2218685000000 44.4520075000000 44.6838675000000 44.9680945000000 45.1764650000000 45.4546395000000 47.1048850000000 50.1790095000000 51.5719655000000 55.3720860000000 85.5064055000000
0.423921500000006 26.0737770000000 44.8096360000000 45.1080585000000 45.4921360000000 45.6683455000000 45.7844745000000 45.9245355000000 46.6472775000000 47.3289625000000 49.8861470000000 50.1371645000000 50.2870925000000 50.6038675000000 56.8766320000000 82.8717060000000
0 45.8109945000000 45.7428270000000 46.2208365000000 46.0566360000000 52.2291795000000 25.9593360000000 52.0406880000000 54.2925860000000 29.1180340000000 54.2943935000000 50.0449285000000 50.2411145000000 54.2495200000000 55.4246235000000 87.9383485000000
13.3610295000000 29.8860340000000 44.2388675000000 45.7055020000000 45.8657115000000 45.9151360000000 46.0402290000000 46.2501160000000 50.1017060000000 50.4220095000000 50.5893935000000 51.0051145000000 51.9436880000000 52.3129190000000 54.8636235000000 85.2997060000000
6.73502950000000 26.0761995000000 45.4503400000000 45.7652970000000 45.8496185000000 45.9142465000000 45.9892435000000 46.0471135000000 46.1339375000000 46.3167035000000 47.5945355000000 48.0745200000000 51.1480095000000 51.9409075000000 53.6576235000000 82.5318415000000
6.78402950000000 30.1551490000000 43.3768160000000 43.5401260000000 43.6071795000000 43.6804410000000 43.7337775000000 43.7976880000000 43.9057360000000 43.9769075000000 44.1627445000000 44.3444830000000 49.6495810000000 50.0704770000000 52.0529480000000 85.3387060000000
0.450921499999993 28.2946300000000 46.3818675000000 47.6524135000000 48.2848585000000 49.1199355000000 50.2550585000000 51.9273950000000 52.0262465000000 52.1092290000000 52.2141145000000 52.2942755000000 52.4315115000000 53.2938885000000 59.4984345000000 84.6607060000000
6.75992150000002 25.8729545000000 47.1207850000000 50.1513890000000 50.8393065000000 51.7940390000000 51.9363790000000 52.0784320000000 52.1702465000000 52.3188580000000 52.5160550000000 53.7917025000000 55.6950275000000 55.9688850000000 61.7377060000000 89.8827060000000
6.62002949999999 26.0271785000000 41.2913615000000 43.5608205000000 43.6786100000000 43.7722080000000 43.8103865000000 43.8854180000000 43.9496630000000 44.0600530000000 44.3379075000000 46.7531470000000 50.0065855000000 51.2508885000000 58.3680310000000 90.9347060000000
6.50502950000001 25.9091915000000 43.2387850000000 43.6036415000000 43.9661740000000 45.8286360000000 46.0692045000000 46.3352775000000 46.4713215000000 46.6159810000000 46.7600155000000 47.0298070000000 50.0361520000000 50.5105200000000 52.7421750000000 88.2633485000000
6.16407849999999 30.0050340000000 43.6514440000000 44.0151135000000 44.1037850000000 44.2153005000000 44.3032080000000 44.4465860000000 44.6302700000000 44.7453800000000 44.9564180000000 46.0801195000000 47.6639865000000 48.0279500000000 49.1156585000000 87.8467060000000
6.82602950000000 26.2551995000000 43.6138205000000 43.8570130000000 43.9678580000000 44.1253005000000 44.2520075000000 44.4699075000000 46.1990530000000 48.3000310000000 50.1409865000000 51.2848675000000 51.5292565000000 51.7471255000000 53.7812680000000 69.7527060000000
12.4669705000000 25.8391785000000 43.6289985000000 43.7495860000000 43.8192775000000 43.9273790000000 43.9911135000000 44.0459350000000 44.1342130000000 44.3426190000000 45.2698270000000 48.1712465000000 48.5640860000000 50.2355810000000 50.6806415000000 90.7257060000000
0.302921499999997 30.3441490000000 43.6727975000000 43.7870130000000 43.8730190000000 43.9167780000000 43.9608585000000 43.9998580000000 44.0590530000000 44.0884745000000 44.2328685000000 45.8243615000000 48.4002040000000 48.6644820000000 52.5781255000000 87.0477060000000
0.286921500000005 28.4366300000000 43.6681045000000 43.7574575000000 43.8138850000000 43.9009355000000 43.9743880000000 44.0138365000000 44.0743935000000 44.1754105000000 44.2926415000000 46.5787765000000 50.3582040000000 50.7298800000000 56.8046235000000 89.3317060000000
6.62402950000001 28.4511490000000 45.5114745000000 45.6670275000000 45.7511260000000 45.9385980000000 50.2952710000000 50.8637775000000 51.8370945000000 52.0716485000000 52.3349480000000 53.2237765000000 54.1958110000000 59.7508885000000 65.8241750000000 92.6047060000000
0.316921500000007 30.4031555000000 45.7129075000000 45.8487975000000 46.0590275000000 47.0816360000000 47.2272290000000 47.6106630000000 50.1872700000000 50.3531870000000 50.4410390000000 50.5894020000000 51.0924050000000 54.4152270000000 58.7797060000000 86.8947060000000
6.76302949999999 29.5281315000000 44.5431045000000 45.2171360000000 45.7313455000000 46.5159415000000 46.7651260000000 52.1048675000000 52.2097035000000 52.3133935000000 52.3986415000000 52.4973835000000 53.0732775000000 58.5386320000000 59.2410860000000 86.2513895000000
6.80302949999998 26.2631785000000 45.4156415000000 45.6322290000000 45.8319745000000 46.6802080000000 46.9213325000000 47.4068275000000 49.9080425000000 51.5807080000000 52.5006415000000 52.8969305000000 53.3220095000000 56.8446320000000 58.9505115000000 84.5507060000000
12.9259215000000 26.0361785000000 43.2347975000000 43.3693455000000 43.5644180000000 43.6710585000000 43.8503005000000 45.5878675000000 48.0918675000000 49.8932565000000 50.1730945000000 50.4894820000000 52.0802270000000 53.9436320000000 56.1043150000000 87.4934055000000
0.741029499999996 32.6043045000000 43.5229075000000 43.7979415000000 44.0208160000000 45.6583790000000 45.9200130000000 46.2884030000000 47.9218070000000 50.1323225000000 50.3971255000000 51.9259285000000 53.0777765000000 53.5002040000000 58.0618210000000 78.7958415000000
0.378921500000018 26.0231785000000 43.3048270000000 44.0567955000000 45.8057025000000 45.9910375000000 46.2079985000000 46.5089075000000 48.0358675000000 51.7270945000000 52.0004830000000 53.2525180000000 58.0160925000000 58.2769480000000 61.8468210000000 84.5223485000000
12.1241555000000 30.3400340000000 45.4226415000000 45.8909745000000 46.2618365000000 47.7343785000000 49.9934050000000 51.6310130000000 52.1032435000000 52.3577080000000 53.1764480000000 53.7676360000000 54.1727025000000 56.6001520000000 60.0549865000000 80.0737060000000
With respect to what’s causing the lines, the data are the data, and the code simply must adapt to them. For that reason, I created the ‘SepVal’ scalar to replace the current separation value (apparently 20 in your code, 2 in my original code) to be the maximum value in the ‘Z’ matrix. I later multiplied it by 2 so that the numbers are easier to read (otherwise they collide with the line to the right), since the numbers were part of the original request. (The numbers are rounded to the nearest integer.)
(I also created the ‘DG_Z.txt’ to avoid having to copy the ‘Z’ matrix in its entirety here. I also attached it.)
Z = readmatrix('DG_Z.txt')
Z = 25×16
6.8980 28.9362 43.6304 43.8532 44.0391 44.1955 44.3385 44.5337 45.3732 45.6238 45.7039 45.8610 46.7153 48.8962 50.8512 72.6838 6.6930 26.0816 39.9511 41.5139 41.9944 42.4394 42.6731 42.9533 43.7100 43.8762 44.0135 44.1594 44.3030 44.5025 47.8452 78.7887 13.3380 25.9993 40.1604 43.4414 43.8530 44.2219 44.4520 44.6839 44.9681 45.1765 45.4546 47.1049 50.1790 51.5720 55.3721 85.5064 0.4239 26.0738 44.8096 45.1081 45.4921 45.6683 45.7845 45.9245 46.6473 47.3290 49.8861 50.1372 50.2871 50.6039 56.8766 82.8717 0 45.8110 45.7428 46.2208 46.0566 52.2292 25.9593 52.0407 54.2926 29.1180 54.2944 50.0449 50.2411 54.2495 55.4246 87.9383 13.3610 29.8860 44.2389 45.7055 45.8657 45.9151 46.0402 46.2501 50.1017 50.4220 50.5894 51.0051 51.9437 52.3129 54.8636 85.2997 6.7350 26.0762 45.4503 45.7653 45.8496 45.9142 45.9892 46.0471 46.1339 46.3167 47.5945 48.0745 51.1480 51.9409 53.6576 82.5318 6.7840 30.1551 43.3768 43.5401 43.6072 43.6804 43.7338 43.7977 43.9057 43.9769 44.1627 44.3445 49.6496 50.0705 52.0529 85.3387 0.4509 28.2946 46.3819 47.6524 48.2849 49.1199 50.2551 51.9274 52.0262 52.1092 52.2141 52.2943 52.4315 53.2939 59.4984 84.6607 6.7599 25.8730 47.1208 50.1514 50.8393 51.7940 51.9364 52.0784 52.1702 52.3189 52.5161 53.7917 55.6950 55.9689 61.7377 89.8827
SepVal = 2*ceil(max(Z(:)))
SepVal = 186
Y = size(Z,2):-1:1;
figure
hold on
for k = 1:numel(Y)
X = Z(k,:)+SepVal*(k-1);
plot(X, Y, 'LineWidth',2)
text(X(end), Y(end), compose(' %.0f',Z(k,end)), 'Horiz','left', 'Vert','bottom', 'FontSize',8)
text(X(1:end-1), Y(1:end-1), compose(' %.0f',Z(k,1:end-1)), 'Horiz','left', 'Vert','middle', 'FontSize',8)
end
hold off
grid
xlim([-1 1]*20+xlim)
Experiment with various plot (actually axes)properties to get different results.
.

Sign in to comment.

More Answers (0)

Products

Release

R2019b

Community Treasure Hunt

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

Start Hunting!