How to plot the bar graph in descending order?
78 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Nannthini
am 4 Okt. 2022
Kommentiert: Nannthini
am 6 Okt. 2022
I want to make this graph from largest to smallest. How can I do this?
Airpollution = readtable ('Location vs No2.xlsx');
x = categorical(Airpollution{:,1});
y = Airpollution{:,2};
bar(x,y)
xlabel('Cities');
ylabel('Concentration of No2');
title(['Cities in Andha Pradesh, India vs Maximum concentration of No2 (2010)']);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1145655/image.png)
0 Kommentare
Akzeptierte Antwort
Image Analyst
am 4 Okt. 2022
Bearbeitet: Image Analyst
am 5 Okt. 2022
Try
Airpollution = readtable ('Location vs No2.xlsx');
x = categorical(Airpollution{:,1});
y = Airpollution{:,2};
% Sort by decreasing y value.
[sortedY, sortOrder] = sort(y, 'descend');
% You must sort x the same way so you don't lose correspondences.
sortedX = x(sortOrder);
% Plot the bar chart from largest to smallest.
bar(sortedX, sortedY)
xlabel('Cities');
ylabel('Concentration of NO_2');
title(['Cities in Andha Pradesh, India vs Maximum concentration of No2 (2010)']);
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
5 Kommentare
Image Analyst
am 5 Okt. 2022
Looks like the problem was casting x to categorical. Try it this way (and please avoid Ramagundam!)
airPollution = readtable ('Location Vs No2 (2010).xlsx')
x = airPollution{:,1};
y = airPollution{:,2};
subplot(2, 1, 1);
bar(y)
xticklabels(x)
grid on;
xlabel('Cities');
ylabel('Concentration of NO_2');
title('Cities in Andha Pradesh, India vs Maximum concentration of NO_2 (2010)');
fontsize(gcf, 15, 'points')
% Sort by decreasing y value.
[sortedY, sortOrder] = sort(y, 'descend');
% You must sort x the same way so you don't lose correspondences.
sortedX = x(sortOrder);
% Plot the ba chart from largest to smallest.
subplot(2, 1, 2);
bar(sortedY)
xticklabels(sortedX)
grid on;
xlabel('Cities');
ylabel('Concentration of NO_2');
title('Cities in Andha Pradesh, India vs Maximum concentration of NO_2 (2010)');
fontsize(gcf, 15, 'points')
g = gcf;
g.WindowState = "maximized"
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1146610/image.png)
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Data Distribution Plots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!