Clear Filters
Clear Filters

Only part of my plotted line will dash and how to add text to a line in the plot?

6 views (last 30 days)
Hi all,
This code:
load('z_means'); load ('z_diffs'); load ('z_CR_ofmean')
figure ('color','w');
plot(z_means,z_diffs,'sr', 'MarkerSize', 9)
hold on
%Plot lines
plot(z_means,zeros(1,length(z_means)),'r', 'LineWidth', 0.7); %%%plot zero
plot(z_means, ones(1,length(z_means)).*z_CR_ofmean(1),'r--', 'LineWidth', 0.6); %%%plot the upper CR
plot(z_means, ones(1,length(z_means)).*z_CR_ofmean(2),'r--', 'LineWidth', 0.6); %%%plot the lower CR
% Add text
text(z_CR_ofmean, [mynum2str(z_CR_ofmean(1)) ''],'HorizontalAlignment','left','VerticalAlignment','middle','fontsize',6);
text(z_means, [mynum2str(z_means,2) ''],'HorizontalAlignment','left','VerticalAlignment','middle','fontsize',6);
text(z_CR_ofmean, [mynum2str(z_CR_ofmean(2))''],'HorizontalAlignment','left','VerticalAlignment','middle','fontsize',6);
Produces this:
and the error:
Error using text
First two or three arguments must be numeric doubles.
Wheareas I want:
  1. Dashed lines to be dashed throughout the whole lenght of the lines
  2. Add text to each line correctly (can be any text as an example)
Data attached. Can you help please?

Accepted Answer

Johannes Hougaard
Johannes Hougaard on 26 Apr 2022
Hi Tomaszzz
The reason some of the line is full and not dotted is that is it not the line as such but more a connection between two points - this often occurs when the data are not ordered.
You can fix the issue simply by sorting your x data
load('z_means'); load ('z_diffs'); load ('z_CR_ofmean')
figure ('color','w');
plot(z_means,z_diffs,'sr', 'MarkerSize', 9)
hold on
%Plot lines
plot(sort(z_means),zeros(1,length(z_means)),'r--', 'LineWidth', 0.7); %%%plot zero
plot(sort(z_means), ones(1,length(z_means)).*z_CR_ofmean(1),'r--', 'LineWidth', 0.6); %%%plot the upper CR
plot(sort(z_means), ones(1,length(z_means)).*z_CR_ofmean(2),'r--', 'LineWidth', 0.6); %%%plot the lower CR

More Answers (1)

Voss
Voss on 26 Apr 2022
text requires 2 coordinates (x,y) or 3 coordinates (x,y,z) to specify the text location. You were supplying only one coordinate.
load('z_means'); load ('z_diffs'); load ('z_CR_ofmean');
figure ('color','w');
plot(z_means,z_diffs,'sr', 'MarkerSize', 9)
hold on
%Plot lines
z_means_sorted = sort(z_means);
plot(z_means_sorted([1 end]),[0 0],'r', 'LineWidth', 0.7); %%%plot zero
plot(z_means_sorted([1 end]),z_CR_ofmean([1 1]),'r--', 'LineWidth', 0.6); %%%plot the upper CR
plot(z_means_sorted([1 end]),z_CR_ofmean([2 2]),'r--', 'LineWidth', 0.6); %%%plot the lower CR
% Add text
text(z_means_sorted(1),z_CR_ofmean(1), [num2str(z_CR_ofmean(1)) ''],'HorizontalAlignment','left','VerticalAlignment','bottom','fontsize',10);
% text(z_means, [num2str(z_means,2) ''],'HorizontalAlignment','left','VerticalAlignment','middle','fontsize',6);
text(z_means_sorted(1),z_CR_ofmean(2), [num2str(z_CR_ofmean(2))''],'HorizontalAlignment','left','VerticalAlignment','top','fontsize',10);

Tags

Community Treasure Hunt

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

Start Hunting!