Clear Filters
Clear Filters

Seeking help to list all Yticks

5 views (last 30 days)
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);
Error using yticks
Invalid tick values. To set tick labels use yticklabels.
set(gca,'yticklabel', groupedData.sum_Song_Time);
xlabel('Total Time (Hours)');
title('Pareto chart of total time by Genre');
  2 Comments
Image Analyst
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:
Maria Baeza
Maria Baeza on 17 Apr 2023
Almost all of the genres are missing.
I have now attached the csv file as requested.
Ed

Sign in to comment.

Accepted Answer

Cris LaPierre
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")
sumTbl = 29×3 table
Genre GroupCount sum_Song_Time _______________________ __________ _____________ {0×0 char } 1 271 {'Comedy' } 8 1602 {'Salsa' } 12 3397 {'Patriotic' } 14 3436 {'Soundtrack' } 19 5265 {'Celtic' } 33 7173 {'Zydeco' } 33 7293 {'Hawaiian' } 35 8049 {'World' } 46 9575 {'Classical' } 43 10956 {'Punk' } 50 17874 {'Funk' } 78 23341 {'Disco & Dance Music'} 88 23557 {'Latin' } 112 26216 {'Folk' } 107 26841 {'Jazz' } 226 69074
barh(sumTbl.sum_Song_Time)
yticks(1:height(sumTbl));
yticklabels(sumTbl.Genre)

More Answers (0)

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Tags

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!