Seeking help to list all Yticks

1 Ansicht (letzte 30 Tage)
Maria Baeza
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);
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 Kommentare
Image Analyst
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:
Maria Baeza
Maria Baeza am 17 Apr. 2023
Almost all of the genres are missing.
I have now attached the csv file as requested.
Ed

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Cris LaPierre
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")
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)

Weitere Antworten (0)

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by