How to make this plot

조회 수: 1 (최근 30일)
Stefano Di Vito
Stefano Di Vito 2018년 6월 11일
댓글: Stephen23 2018년 6월 12일
Hello everyone, thanks in advance for your time and help! I'm going to expose my situation:
I've got a logical matrix, of dimensions, let's say, (P,T). For istance:
P=4;
T=3;
M=[1 1 0 0 ;1 1 1 0; 1 0 0 0];
I should make a plot on the plan of axis T (x-axis) and P (y-axis), as it follows. For each fixed T, i should obtain a vertical bar which stands from P=1 to P=4. This bar should be a two-tone bar: it should be composed of a red-colour part, as extended as the number of values 1 occurring in the correspondant column of the matrix M. The remaining segment should be green, as extended as the number of values 0 occurring in the correspondant column of the matrix.
For istance, fixed T=1, i should get a red line from P=1 to P=2 and a green line for the remaining values of P; fixed T=2, i should have a red line from P=1 to P=3 and a green part for P=4, and so on.
Substantially, i need to repeat this for each T ( i fix the column of the matrix and plot P-values for that column) and put all the vertical lines in the same plot ( should i use hold on ?). The red lines' length should be equal to the number of 1 values occurring in the columns. The green lines' length should be equal to the number of 0-values (remaining values) occurring in the columns.
I've been trying a lot, but can't get which should be the right plot function to be used. Thank You for helping me!
  댓글 수: 3
Stefano Di Vito
Stefano Di Vito 2018년 6월 11일
That's perfect, thank you. To solve all my doubts, is there a way to get this image with the borders of each bar (without getting blank spaces between them)?
Stephen23
Stephen23 2018년 6월 12일
"is there a way to get this image with the borders of each bar"
Not really. You could with a lot of fiddling around, but it would not be worth it. That is why I put this as a comment, not an answer: it is mostly for interest.

댓글을 달려면 로그인하십시오.

채택된 답변

Sandro Lecci
Sandro Lecci 2018년 6월 11일
Hi Stefano,
Is this what you are trying to do?
if yes then this is the code:
P=4;
T=3;
M=[1 1 0 0 ;1 1 1 0; 1 0 0 0];
myData = sum(M,2);
dataToPlot = [myData, size(M,2)-myData];
figure();
handle_bar = bar(dataToPlot, 'stacked');
handle_bar(1).FaceColor = 'r';
handle_bar(2).FaceColor = 'g';
xlabel('T')
ylabel('P')
The trick is to create a matrix with T lines and 2 columns, the first column stores the number of ones in your M matrix and the second column stores the number of zeros in M (or, as in my code, the difference between the second dimension of M (which is 4) and the number of ones). You then plot everything using the bar function, specifying that you want the columns of "myData" to be stacked. Then you use the created handle (handle_bar) to access the FaceColor of the first column (in red) and the second column (in green).
Best, Sandro
  댓글 수: 4
Stefano Di Vito
Stefano Di Vito 2018년 6월 11일
Excuse me, i noticed you set the script in order to get a matrix of T lines and 2 columns, but i think i need a matrix of P lines. I'll explain in a better way. T are the time windows, so i should put these on the x-axis (like a time series graph). On the other hand, for each T (time window) i should plot a bar of length equal to P, which is two tone, as i explained in the previous message. Essentially, i need a graph like the one in the image (in which P=80, T=155), but with visible borders for each bar.
Sandro Lecci
Sandro Lecci 2018년 6월 12일
In my script the T are on the X axis, right? There are three time windows (T = 3). In the end I get a matrix of T lines (that go on the x axis) and 2 columns, identifying the size of each red-green part of that specific column).
I am not experienced with the solution of M. Cobeldick, using bars one should simply set the barwidth to 1 (default is 0.8 I guess).
handle_bar = bar(dataToPlot, 'stacked', 'barwidth', 1);
Best

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Discrete Data Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by