How Do I Properly Format my Bar Graph?

14 ビュー (過去 30 日間)
Chloe McCarthy
Chloe McCarthy 2022 年 1 月 22 日
コメント済み: Chunru 2022 年 1 月 23 日
I am trying to plot 2 arrays using the bar() function. The plot successfully displays, but I am trying to change the format for readability.
The following is the code I currently have:
% Clean
close all
clear
clc
format short
dataPoints = 1000;
bars = 4;
data = generateData(dataPoints); % Generrate and store data
sortedData = sort(data); % Sort data in ascending order
[xData , yData] = sortedPlotData(sortedData,bars); % Obtain plotting data
figure
bar(xData,yData)
title('Unnamed Project Title')
xlabel('Number in the Thousands (ie 4,000)')
ylabel('Percent')
xtickformat('%.4f')
ytickformat('percentage')
Because I want help in formatting the plot, I'll ommit the functions, but the plot that comes out is:
First note the x data points are [379.5 , 1138.3 , 1897.2 , 2656] and I have no problem with the y-axis. I am trying to format the x-axis so that the ticks are in decimal form (not scientific), and that there is 1 tick per bar. A good example of what I'm trying to explain comes from the documentation center:
Any assistance in helping me format my x-axis is greatly appreciated!

回答 (2 件)

VBBV
VBBV 2022 年 1 月 22 日
編集済み: VBBV 2022 年 1 月 22 日
x = [379.5 , 1138.3 , 1897.2 , 2656]; % your values
y = rand(size(x));
bar(x, y)
xlabel('Number in the Thousands (ie 4,000)')
ylabel('Percent')
xticks(x); % use xticks same in count with your xvalues
xtickformat('%.1e')
You can use xticks to specify the x-values as an array which aligns with bars
  1 件のコメント
Chloe McCarthy
Chloe McCarthy 2022 年 1 月 22 日
編集済み: Chloe McCarthy 2022 年 1 月 22 日
This got me closer! This his how I modified my code:
% Clean
close all
clear
clc
dataPoints = 1000;
bars = 4;
data = generateData(dataPoints); % Generrate and store data
sortedData = sort(data); % Sort data in ascending order
[xData , yData] = sortedPlotData(sortedData,bars); % Obtain plotting data
figure
bar(xData,yData)
title('Unnamed Project Title')
xlabel('Number in the Thousands (ie 4,000)')
ylabel('Percent')
xticks(xData)
xtickformat('%.1e')
ytickformat('percentage')
And the result:
Now the main concern I have from here is converting 3.2*10^-1 x10^4 to 320, 1.6*10^0 x10^4 to 1,600, and so on.
EDIT: Just to give a clearer idea on what's stored in xData:
disp(xData)
1.0e+04 *
0.3218 0.9647 1.6077 2.2506

サインインしてコメントする。


Chunru
Chunru 2022 年 1 月 22 日
編集済み: Chunru 2022 年 1 月 22 日
How about this?
x = [10 30 36.7 50]*1e9;
y = rand(size(x))*30;
bar(x, y);
ax = gca;
ax.XTick = x;
ax.XAxis.Exponent = 9;
  2 件のコメント
Chloe McCarthy
Chloe McCarthy 2022 年 1 月 22 日
This is how I modified the code:
% Clean
close all
clear
clc
dataPoints = 1000;
bars = 4;
data = generateData(dataPoints); % Generrate and store data
sortedData = sort(data); % Sort data in ascending order
[xData , yData] = sortedPlotData(sortedData,bars); % Obtain plotting data
figure
bar(xData,yData)
title('Unnamed Project Title')
xlabel('Number in the Thousands (ie 4,000)')
ylabel('Percent')
ax = gca;
ax.XTick = x;
ax.XAxis.Exponent = 9;
ytickformat('percentage')
And unfortunately this is the result:
I also received the error:
Unrecognized function or variable 'x'.
Error in main (line 19)
ax.XTick = x;
I feel I was dishonest in the exact data stored in xData so I'll past exactly what's stored here using disp (numbers change, but this will show the general format):
disp(xData)
1.0e+04 *
0.4429 1.3286 2.2142 3.0999
Chunru
Chunru 2022 年 1 月 23 日
What you need to adapt:
ax.XTick = xData;
ax.XAxis.Exponent = 0;

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLine Plots についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by