How to plot the bar graph in descending order?

104 visualizaciones (últimos 30 días)
Nannthini
Nannthini el 4 de Oct. de 2022
Comentada: Nannthini el 6 de Oct. de 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)']);

Respuesta aceptada

Image Analyst
Image Analyst el 4 de Oct. de 2022
Editada: Image Analyst el 5 de Oct. de 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 comentarios
Image Analyst
Image Analyst el 5 de Oct. de 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"
Nannthini
Nannthini el 6 de Oct. de 2022
Thank you so much for helping me.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Distribution Plots en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by