Seeking help to list all Yticks
5 views (last 30 days)
Show older comments
Maria Baeza
on 17 Apr 2023
Answered: Cris LaPierre
on 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).
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1358968/image.jpeg)
% 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 Comments
Image Analyst
on 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:
Accepted Answer
Cris LaPierre
on 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 Comments
More Answers (0)
See Also
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!