How to plot the bar graph in descending order?
조회 수: 68 (최근 30일)
이전 댓글 표시
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
채택된 답변
Image Analyst
2022년 10월 4일
편집: Image Analyst
2022년 10월 5일
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
Image Analyst
2022년 10월 5일
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)
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Distribution Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!