I have tried to combine these two graphics in many ways but nothing works for me, just as the creation of the legends gives me an error. I need help for this because neither the combination nor the legends want to serve me, Thank you!
b1=bar(app.CasosUIAxes,data1.dateRep,data1.cases);
hold on
%grafica de barras 2
b2=bar(app.CasosUIAxes,data2.dateRep,data2.cases);
hold off
legend(app.CasosUIAxes,[b1 b2],'Bar Chart 1','Bar Chart 2')

댓글 수: 2

VBBV
VBBV 2021년 1월 24일
Do you mean bar plots don't appear at all ?
Diego Monsave
Diego Monsave 2021년 1월 24일
Only the data of the second graph is shown but not the first. The idea is that the two graphics appear in one.

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

 채택된 답변

Benjamin Kraus
Benjamin Kraus 2021년 2월 8일

4 개 추천

I could be misunderstanding your question, but I suspect the problem you are facing is that your bar graphs are perfectly overlapping one another. If you create two bar graphs with the same x-values, they will overlap each other and one could be hidden behind the other. The better approach is to create both bars simulteneously with a single call to the bar command.
Consider these three blocks of code. The second approach is preferred.
Overlapping Bars
x = 1:5;
y1 = 1:5;
y2 = 2:6;
bar(x,y1);
hold on
bar(x,y2); % Second bar ignores first bar and will overlap the first bar
Grouped Bars (Preferred Approach)
x = 1:5;
y1 = 1:5;
y2 = 2:6;
bar(x,[y1;y2]); % Creating both bars at once, they are aware of one another and will not overlap
This approach can be used if the x-data for both bars is different as well, either by padding the x-values and y-values with NaNs, or passing to matrices into the bar command. If you pass two matrices into the bar command, be careful of the orientation of the matrices, or you will get unexpected results.
x1 = 1:5;
x2 = 3:7;
y1 = 1:5;
y2 = 2:6;
bar([x1; x2]',[y1;y2]');
Manually Shifted Bars
x = 1:5;
y1 = 1:5;
y2 = 2:6;
bar(x-0.25,y1,0.5) % Manually shift the bar and make it narrower so the other bar fits.
hold on
bar(x+0.25,y2,0.5) % Manually shift the bar and make it narrower so the other bar fits.

댓글 수: 6

hxen
hxen 2023년 6월 10일
편집: hxen 2023년 6월 10일
Hi Benjamin,
I was going to first post my question here related to this thread but thought it best to have it as a stand alone question and linking it here as well. In following your helpful thread, I ran into an issue with logical consistency between seemingly exact values that has stymied my progress. Any help would be so much appreciated. Thank you in advance to you or anyone that might have encountered this or knows what is going on:
https://www.mathworks.com/matlabcentral/answers/1980954-glitch-in-the-matrix-unable-to-get-logical-consistency-between-matrices-with-same-values
hxen
hxen 2023년 6월 11일
Following up here. It seems that high precision fraction x-data is not well tolerated by the use of MATLAB's bar function. My x-data was the the center of the bin widths for bins that lied along a range less than unity. For some of the values, there was were many trailing zeroes followed by a non-zero units at the 15 to 16th decimal. I ended up using the symbolic math toolbox, using digits and vpa, setting to precision 5 to avoid the high place precision. The bars then were graphed. So for whatever reason bar cannot handle such high precision nor intrinscicaly restricts the precision to avoid the issue I ran into. I cannot find any details about this with the description for bar however. Other than the high precission fractional values being the cause, it is not fully clear (if at all) why this becomes problematic. Is there any resource I can learn about this further? Thanks again in advance.
Just to clarify, the issue @hxen is having is that the bar function requires exact equality before it will consider two x-values to be part of the same group.
Consider these two examples:
x = [1 1; 2 2; 3 3];
y = x;
figure
bar(x,y)
x2 = x;
x2(2) = x2(2)+eps(2); % Intentionally make a difference of just eps
figure
bar(x2,y)
xlim([0.5 3.5])
hxen
hxen 2023년 6월 12일
이동: Adam Danz 2023년 6월 13일
Thank you Benjamin for the helpful feedback!
Artjom Vartanyan
Artjom Vartanyan 2024년 5월 30일
How can I do this, but with 2 y-axis?
@Artjom Vartanyan: The "Grouped Bars (Preferred Approach)" above won't work with yyaxis. You could leverage the "Manually Shifted Bars" approach, but there is another option which is a bit more complicated.
For yyaxis, you need to create 4 different bar objects using two separate calls to the bar command:
  1. A bar object with the data for the left side.
  2. A bar object with all NaN values as a placeholder on the left side.
  3. A bar object with all NaN values as a placeholder on the right side.
  4. A bar object with the data for the right side.
Consider this example:
x = 1:5;
y1 = 1:5;
y2 = (1:5)*100;
yleft = [y1' NaN(5,1)];
yright = [NaN(5,1) y2'];
yyaxis left
bar(x,yleft);
yyaxis right
bar(x,yright);

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

제품

릴리스

R2020b

질문:

2021년 1월 23일

편집:

2024년 5월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by