필터 지우기
필터 지우기

How to plot the bar graph in descending order?

조회 수: 68 (최근 30일)
Nannthini
Nannthini 2022년 10월 4일
댓글: Nannthini 2022년 10월 6일
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)']);

채택된 답변

Image Analyst
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
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"
Nannthini
Nannthini 2022년 10월 6일
Thank you so much for helping me.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by