Only part of my plotted line will dash and how to add text to a line in the plot?
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Tomaszzz
el 26 de Abr. de 2022
Comentada: Voss
el 29 de Abr. de 2022
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 comentarios
Respuesta aceptada
Johannes Hougaard
el 26 de Abr. de 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
Más respuestas (1)
Voss
el 26 de Abr. de 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);
2 comentarios
Ver también
Categorías
Más información sobre Annotations en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!