How to display the legend for data of an empty table (0 x 11)?
조회 수: 2 (최근 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))])
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))])
%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");
lgd.Interpreter = 'latex';
lgd.FontSize = 11;
댓글 수: 0
채택된 답변
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))])
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))])
%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 Exchange에서 Graphics Object Programming에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!