How to use Bar to plot two separate groups in the same plot??

조회 수: 2 (최근 30일)
Apo
Apo 2022년 9월 14일
댓글: Robert U 2022년 9월 14일
So to make it easier to explain here is an image of what im trying to do:
I've tried to group them up with Bar() but it comes out all funky looking. The best I've reached is making them come out on separate plots, but not really what im looking for. What I want is to group the variable "Inkomst" to the left and "KPI" on the right as shown in the picture.
The code is below:
clear
KPI = [ 3809 3902 3986 4063 4078 4097 4153 4243]
Inkomst = [274.6 266.4 266.2 270.4 280.9 291.9 306.8 323.2]
subplot(2,1,1)
bar(KPI)
subplot(2,1,2)
bar(Inkomst)

채택된 답변

Robert U
Robert U 2022년 9월 14일
Hi Serhat Misto,
try to change XTickLabels in your axis:
KPI = [ 3809 3902 3986 4063 4078 4097 4153 4243];
Inkomst = [274.6 266.4 266.2 270.4 280.9 291.9 306.8 323.2];
fh = figure;
ah = axes(fh);
bar(ah,[Inkomst;KPI])
ah.XTickLabel = [{'Inkomst'};{'KPI'}];
Kind regards,
Robert
  댓글 수: 2
Apo
Apo 2022년 9월 14일
Hi Robert U.
Would it be possible for you to explain a little more in detail why you're doing what you did. I'm fairly new to this and wanted to learn.
Thanks.
Robert U
Robert U 2022년 9월 14일
Hi Serhat Misto,
changes to your code:
  1. Use handles to address the UI elements in a more robust way.
  2. bar() allows to plot structured data (in vectors) by supplying matrices.
  3. bar() allows to plot against several data types as x-axis values but not strings or char. Thus, change the integer written in XTickLabel Property of axis (which is anyway an array of char) to the desired array of char.
Kind regards,
Robert

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Grid Lines, Tick Values, and Labels에 대해 자세히 알아보기

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by