Only part of my plotted line will dash and how to add text to a line in the plot?
5 ビュー (過去 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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/979210/image.jpeg)
and the error:
Error using text
First two or three arguments must be numeric doubles.
Wheareas I want:
- Dashed lines to be dashed throughout the whole lenght of the lines
- Add text to each line correctly (can be any text as an example)
Data attached. Can you help please?
0 件のコメント
採用された回答
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
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);
参考
カテゴリ
Help Center および File Exchange で Annotations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!