필터 지우기
필터 지우기

How to force a plot to show trailing NaNs

조회 수: 4 (최근 30일)
AR
AR 2021년 9월 23일
댓글: Image Analyst 2021년 9월 27일
I have a simple set of values that I display on a bar plot or a line plot. Sometimes, depending on the outcome of steps in the code, some values will be set to NaN.
When they appear in the middle of the data series (ex: [1 2 3 NaN 8 9]), the plot shows 6 "data points", with the 4th not being plotted. However, there is a corresponding x-axis location for this missing NaN point.
However, when the NaN appears at the end of the data series (ex: [1 2 3 6 8 NaN]), the plot will onl show 5 "data points", with the 6th not only non being plotted, but completely missing a corresponding x-axis location. This creates problems, as I need the x-axis to be consistent, and will subsitute the x-axis locations with custom xtick labels that should show all the possible x-axis values. Also, I would like to subsitute a text box to appear where there is missing/NaN data, to add a note about why this is missing. Obviously all of this is not possible if the location for the data point is removed and the series is simply truncated.
Any pointers are appreciated. Thanks.

답변 (1개)

Image Analyst
Image Analyst 2021년 9월 23일
Try this:
% Demo by Image Analyst
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 25;
fprintf('Beginning to run %s.m ...\n', mfilename)
v = [1 2 nan 3 6 8 5.5 1.5 NaN]
bar(v, 1);
grid on;
xlim([1, length(v)+1]);
yl = ylim;
nanIndexes = find(isnan(v))
for k = 1 : length(v)
if isnan(v(k))
y = 0;
str = 'NaN';
else
y = v(k)
str = sprintf('%.1f', y);
end
text(k, y, str, ...
'Color', 'b', 'FontSize', 25, ...
'FontWeight', 'bold', ...
'VerticalAlignment', 'bottom', ...
'HorizontalAlignment', 'center');
end
g = gcf;
g.WindowState = 'maximized'
fprintf('Done running %s.m.\n', mfilename)
  댓글 수: 4
AR
AR 2021년 9월 27일
Sorry I haven't checked back in a couple of days. Yes, I do have two bars per "coordinate". I am really only concerned with having a text note where the data is NaN - so I can either have two notes for the missing two bars, or just one note at that coordinate (near the y=0 line). Thanks.
Image Analyst
Image Analyst 2021년 9월 27일
So just don't create str and call text if the number is a valid number. Attach your data if you need more help.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

태그

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by