How to plot the bar graph in descending order?
68 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
Image Analyst
le 4 Oct 2022
Modifié(e) : Image Analyst
le 5 Oct 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 commentaires
Image Analyst
le 5 Oct 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)
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Data Distribution Plots dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!