need to to put input from user in the legend

4 ビュー (過去 30 日間)
Kelsey Pettrone
Kelsey Pettrone 2020 年 11 月 15 日
編集済み: Adam Danz 2020 年 11 月 17 日
I want the legend to say Austrailia smoking instead of just smoking. The country is what the user puts in. How would i do this for the second figure. Also the bar graph has more bars than there are colors, is there any way to differenciate the bars other than colors?
function deliverable1(app)
data = readtable('RiskFactorAnalysis (1).csv');
userinput = app.CountryDropDown.Value;
userinput2 = app.CountryDropDown_2.Value;
useryear = app.EditField.Value
ind = strcmp(data.Entity,userinput);
year = data.Year(ind);
deaths = data.Total_Deaths_per_100000(ind);
obesity = data.Obesity_Deaths_per_100000(ind);
Drug = data.Drug_Deaths_per_100000(ind);
Alcohol = data.Alcohol_Deaths_per_100000(ind);
smoking = data.Smoking_Deaths_per_100000(ind);
% Extract all info for userinput2
ind2 = strcmp(data.Entity,userinput2);
year2 = data.Year(ind2);
deaths2 = data.Total_Deaths_per_100000(ind2);
obesity2 = data.Obesity_Deaths_per_100000(ind2);
Drug2 = data.Drug_Deaths_per_100000(ind2);
Alcohol2 = data.Alcohol_Deaths_per_100000(ind2);
smoking2 = data.Smoking_Deaths_per_100000(ind2);
% Create the 3 figures
grid(app.Axes,'on')
plot(app.Axes,year,deaths,year2,deaths2); % plot deaths2 against year2, not year
xlabel(app.Axes,'year')
ylabel(app.Axes,'deaths per 100000')
legend(app.Axes,userinput,userinput2)
grid(app.Axes2,'on')
bpcombined = [obesity, Drug, Alcohol, smoking, obesity2, Drug2, Alcohol2, smoking2];
indY2 = year2==useryear;

回答 (1 件)

Adam Danz
Adam Danz 2020 年 11 月 16 日
編集済み: Adam Danz 2020 年 11 月 17 日
Instead of setting the legend string from the legend function use the DisplayName property in the plot function (see demo).
plot(app.Axes,year,deaths,year2,deaths2,'DisplayName',['Austrailia ',userinput]); % plot deaths2 against year2, not year
Then just call legend without defining the strings,
legend(app.Axes)

カテゴリ

Help Center および File ExchangeLegend についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by