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

7 ビュー (過去 30 日間)
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?

採用された回答

Johannes Hougaard
Johannes Hougaard 2022 年 4 月 26 日
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

その他の回答 (1 件)

Voss
Voss 2022 年 4 月 26 日
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);
  2 件のコメント
Tomaszzz
Tomaszzz 2022 年 4 月 26 日
@_ Many thanks, I have posted the question regarding the text here just in case you want to place your answer there https://uk.mathworks.com/matlabcentral/answers/1705685-how-to-add-text-to-a-line-in-the-plot
Voss
Voss 2022 年 4 月 29 日
You're welcome!

サインインしてコメントする。

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by