How to display the legend for data of an empty table (0 x 11)?

1 回表示 (過去 30 日間)
Giuseppe
Giuseppe 2022 年 1 月 30 日
コメント済み: Giuseppe 2022 年 1 月 30 日
Hi guys,
I've the need tho show the legend for an empty table in my plot. I try to explain better:
I read two files, one containing asteroid data and one containing comet data, using the readtable function. I then apply a filter to the data and the table variable of the asteroids is empty (0 rows by 11 columns). Then I make a plot of eccentricity vs inclination where each point represents an object (asteroids and comets). In my case I will have only points representing asteroids but I want to have in the legend also the row "comet".
How can I solve this problem?
Here my code and I've attached to the question the necessary files to reproduce it:
clear all; close all; clc;
%Data import from .CSV files
file_name_asteroids = 'NEOs_asteroids.csv';
file_name_comets = 'NEOs_comets.csv';
%Asteroid data reading
opts = delimitedTextImportOptions("NumVariables", 11);
% Specify range and delimiter
opts.DataLines = [2, Inf];
opts.Delimiter = ",";
% Specify column names and types
opts.VariableNames = ["pdes", "name", "epoch", "a", "e", "i", "om", "w", "ma", "q", "ad"];
opts.VariableTypes = ["string", "string", "double", "double", "double", "double", "double", "double", "double", "double", "double"];
% Specify file level properties
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
% Specify variable properties
opts = setvaropts(opts, ["pdes", "name"], "WhitespaceRule", "preserve");
opts = setvaropts(opts, ["pdes", "name"], "EmptyFieldRule", "auto");
% Import the data
Ast_data = readtable(file_name_asteroids,opts);
%Comets data reading
opts = delimitedTextImportOptions("NumVariables", 10);
% Specify range and delimiter
opts.DataLines = [2, Inf];
opts.Delimiter = ",";
% Specify column names and types
opts.VariableNames = ["pdes", "name", "epoch", "q", "e", "i", "om", "w", "tp", "ad"];
opts.VariableTypes = ["string", "string", "double", "double", "double", "double", "double", "double", "double", "double"];
% Specify file level properties
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
% Specify variable properties
opts = setvaropts(opts, ["pdes", "name"], "WhitespaceRule", "preserve");
opts = setvaropts(opts, ["pdes", "name"], "EmptyFieldRule", "auto");
% Import the data
Com_data = readtable(file_name_comets,opts);
%Data filtering
%Asteroids and Comets
i_max = 10; % (deg)
e_max = 0.1;
q_min = 0.9; %(AU)
ad_max = 1.1; % (AU)
Ast_criterion = Ast_data.i <= i_max & Ast_data.e <= e_max &...
Ast_data.q >= q_min & Ast_data.ad <= ad_max;
Ast_data_filtered = Ast_data(Ast_criterion,:);
disp(['Asteroids meeting the desired conditions: ',...
num2str(height(Ast_data_filtered)),' out of ',num2str(height(Ast_data))])
Asteroids meeting the desired conditions: 98 out of 28071
Com_criterion = Com_data.i <= i_max & Com_data.e <= e_max &...
Com_data.q >= q_min & Com_data.ad <= ad_max;
Com_data_filtered = Ast_data(Com_criterion,:);
disp(['Comets meeting the desired conditions: ',...
num2str(height(Com_data_filtered)),' out of ',num2str(height(Com_data))])
Comets meeting the desired conditions: 0 out of 192
%Filtered data plots
% Customized color loading
load Customized_colors.mat
fig_name = figure;
plot(Ast_data_filtered.i,Ast_data_filtered.e,'.','color',my_orange,'LineWidth',1.2);
hold on
plot(Com_data_filtered.i,Com_data_filtered.e,'.','color',my_blue,'LineWidth',1.2);
grid on;
xlabel('$inclination$ (deg)','interpreter','latex');
ylabel('$eccentricity$','interpreter','latex','fontsize',12);
% string = ['Epoch = ' (num2str(Asteroid_num_data.Epoch(1,1))) ' TDB' ];
% annotation('textbox',[0.15 0.1 0.2 0.1],'String',string,...
% 'FitBoxToText','on','Interpreter',"latex");
lgd = legend('$Asteroids$','$Comets$',...
'Orientation',"horizontal",'Location',"northeast");
Warning: Ignoring extra legend entries.
lgd.Interpreter = 'latex';
lgd.FontSize = 11;

採用された回答

Voss
Voss 2022 年 1 月 30 日
You can check if each table variable is empty and if it is plot a NaN point instead. The point won't show up, but the line object will exist with the right properties and will be used by the legend:
clear all; close all; clc;
%Data import from .CSV files
file_name_asteroids = 'NEOs_asteroids.csv';
file_name_comets = 'NEOs_comets.csv';
%Asteroid data reading
opts = delimitedTextImportOptions("NumVariables", 11);
% Specify range and delimiter
opts.DataLines = [2, Inf];
opts.Delimiter = ",";
% Specify column names and types
opts.VariableNames = ["pdes", "name", "epoch", "a", "e", "i", "om", "w", "ma", "q", "ad"];
opts.VariableTypes = ["string", "string", "double", "double", "double", "double", "double", "double", "double", "double", "double"];
% Specify file level properties
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
% Specify variable properties
opts = setvaropts(opts, ["pdes", "name"], "WhitespaceRule", "preserve");
opts = setvaropts(opts, ["pdes", "name"], "EmptyFieldRule", "auto");
% Import the data
Ast_data = readtable(file_name_asteroids,opts);
%Comets data reading
opts = delimitedTextImportOptions("NumVariables", 10);
% Specify range and delimiter
opts.DataLines = [2, Inf];
opts.Delimiter = ",";
% Specify column names and types
opts.VariableNames = ["pdes", "name", "epoch", "q", "e", "i", "om", "w", "tp", "ad"];
opts.VariableTypes = ["string", "string", "double", "double", "double", "double", "double", "double", "double", "double"];
% Specify file level properties
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
% Specify variable properties
opts = setvaropts(opts, ["pdes", "name"], "WhitespaceRule", "preserve");
opts = setvaropts(opts, ["pdes", "name"], "EmptyFieldRule", "auto");
% Import the data
Com_data = readtable(file_name_comets,opts);
%Data filtering
%Asteroids and Comets
i_max = 10; % (deg)
e_max = 0.1;
q_min = 0.9; %(AU)
ad_max = 1.1; % (AU)
Ast_criterion = Ast_data.i <= i_max & Ast_data.e <= e_max &...
Ast_data.q >= q_min & Ast_data.ad <= ad_max;
Ast_data_filtered = Ast_data(Ast_criterion,:);
disp(['Asteroids meeting the desired conditions: ',...
num2str(height(Ast_data_filtered)),' out of ',num2str(height(Ast_data))])
Asteroids meeting the desired conditions: 98 out of 28071
Com_criterion = Com_data.i <= i_max & Com_data.e <= e_max &...
Com_data.q >= q_min & Com_data.ad <= ad_max;
Com_data_filtered = Ast_data(Com_criterion,:);
disp(['Comets meeting the desired conditions: ',...
num2str(height(Com_data_filtered)),' out of ',num2str(height(Com_data))])
Comets meeting the desired conditions: 0 out of 192
%Filtered data plots
% Customized color loading
load Customized_colors.mat
fig_name = figure;
if isempty(Ast_data_filtered)
plot(NaN,NaN,'.','color',my_orange,'LineWidth',1.2);
else
plot(Ast_data_filtered.i,Ast_data_filtered.e,'.','color',my_orange,'LineWidth',1.2);
end
hold on
if isempty(Com_data_filtered)
plot(NaN,NaN,'.','color',my_blue,'LineWidth',1.2);
else
plot(Com_data_filtered.i,Com_data_filtered.e,'.','color',my_blue,'LineWidth',1.2);
end
grid on;
xlabel('$inclination$ (deg)','interpreter','latex');
ylabel('$eccentricity$','interpreter','latex','fontsize',12);
% string = ['Epoch = ' (num2str(Asteroid_num_data.Epoch(1,1))) ' TDB' ];
% annotation('textbox',[0.15 0.1 0.2 0.1],'String',string,...
% 'FitBoxToText','on','Interpreter',"latex");
lgd = legend('$Asteroids$','$Comets$',...
'Orientation',"horizontal",'Location',"northeast");
lgd.Interpreter = 'latex';
lgd.FontSize = 11;

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by