Only part of my plotted line will dash and how to add text to a line in the plot?
3 次查看(过去 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:
- 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!