Seeking help to list all Yticks
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Maria Baeza
am 17 Apr. 2023
Beantwortet: Cris LaPierre
am 17 Apr. 2023
I have created a file with Python containing all the metadata of my music collection. I am trying to graph my music in hours by Genre using a Pareto chart, which seems to be working but I have not been able to figure out how to show all Genres (YTicks).
% Load the table
data = readtable('My_Music.csv');
% Convert the Song_Time column to hours
data.Song_Time = data.Song_Time / 3600;
% Create a grouped table by Genre and sum the Song_Time for each Genre
groupedData = varfun(@sum, data, 'GroupingVariables', 'Genre', 'InputVariables', 'Song_Time', 'OutputFormat', 'table');
% groupedData.Properties.VariableNames{'GroupingVariables'} = 'Genre';
% Sort the table by descending order of total time
groupedData = sortrows(groupedData,2);
% Calculate cumulative percentage
total = groupedData.sum_Song_Time / sum(groupedData.sum_Song_Time) * 100;
cumulative_percent = cumsum(total);
% Create horizontal bar chart
figure;
barh(groupedData.sum_Song_Time);
hold on;
yticklabels(groupedData.Genre);
yticks(groupedData.Genre);
set(gca,'yticklabel', groupedData.sum_Song_Time);
xlabel('Total Time (Hours)');
title('Pareto chart of total time by Genre');
2 Kommentare
Image Analyst
am 17 Apr. 2023
You forgot to attach 'D:\Music\My_Music.csv'.
Why are all genres not showing in the y tick labels? Which ones are missing?
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
Akzeptierte Antwort
Cris LaPierre
am 17 Apr. 2023
You need to set yticks first, then assign a label for each tick.
Something like this (updated code a little).
data = readtable('My_Music.csv');
sumTbl = groupsummary(data,"Genre","sum","Song_Time");
sumTbl = sortrows(sumTbl,"sum_Song_Time")
barh(sumTbl.sum_Song_Time)
yticks(1:height(sumTbl));
yticklabels(sumTbl.Genre)
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Data Type Conversion 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!