Filter löschen
Filter löschen

How Do I Properly Format my Bar Graph?

5 Ansichten (letzte 30 Tage)
Chloe McCarthy
Chloe McCarthy am 22 Jan. 2022
Kommentiert: Chunru am 23 Jan. 2022
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!

Antworten (2)

VBBV
VBBV am 22 Jan. 2022
Bearbeitet: VBBV am 22 Jan. 2022
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 Kommentar
Chloe McCarthy
Chloe McCarthy am 22 Jan. 2022
Bearbeitet: Chloe McCarthy am 22 Jan. 2022
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

Melden Sie sich an, um zu kommentieren.


Chunru
Chunru am 22 Jan. 2022
Bearbeitet: Chunru am 22 Jan. 2022
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 Kommentare
Chloe McCarthy
Chloe McCarthy am 22 Jan. 2022
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 am 23 Jan. 2022
What you need to adapt:
ax.XTick = xData;
ax.XAxis.Exponent = 0;

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Line Plots finden Sie in Help Center und File Exchange

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by