How can a single line plot have two colors?
    8 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    Tunde Adubi
 el 24 de Jul. de 2023
  
    
    
    
    
    Comentada: Star Strider
      
      
 el 24 de Jul. de 2023
            using x and y variables to make a single graph or line plot, how can the plot have two colors or more than one color?
This plot comes out black. How can the interval in the plot attached be red?
figure('position',[100 100 900 550])
hold on
plot(range,s_attenuation,'black', 'LineWidth', 1.5)
ylabel('Specific Attenuation (dB/km)')
xlabel('Range (km)')
title('Line plot')
0 comentarios
Respuesta aceptada
  Star Strider
      
      
 el 24 de Jul. de 2023
        Use ‘logical indexing’: 
figure
imshow(imread('plot.JPG'))
title('Original Function')
range = linspace(0,100,500);
s_attenuation = exp(-(range-46).^2/5) + 2*exp(-(range-47).^2/50);
Lv = s_attenuation >= 1.1;                                          % Logical Vector
figure
plot(range, s_attenuation, 'b')
hold on
plot(range(Lv), s_attenuation(Lv), 'r')
hold off
grid
.
2 comentarios
  Star Strider
      
      
 el 24 de Jul. de 2023
				I assume you intend 1.1 on the left side and 1.5 on the right.  In this example, that requires splitting it initially into two segments, then combining them — 
range = linspace(0,100,500).';                                      % Assume Column Vectors
s_attenuation = exp(-(range-46).^2/5) + 2*exp(-(range-47).^2/50);
[~,locs] = max(s_attenuation);                                      % Use 'findpeaks' For More Than One Maximum 
locs = [1; locs; numel(range)];                                     % Augment 'locs' To Include First % Last Indices
v = [1.1; 1.5];                                                     % Values To Comopare
Lv = false(size(range));                                            % Preallocate
for k = 1:numel(locs)-1
    idxrng = locs(k) : locs(k+1);                                   % Index Range For Each Segment
    Lv(idxrng) = s_attenuation(idxrng) >= v(k);                     % Logical Vector
end
figure
plot(range, s_attenuation, 'b')
hold on
plot(range(Lv), s_attenuation(Lv), 'r')
hold off
grid
grid minor
I coded this to be generalisable to more peaks, so that should work, although with some modifications depending on the data.  Since each peak would have to be considered separately, creating ‘locs’ would need to take that into consideration.  Assuming one peak, this should work without modification.  
If I had your data, I could do it with your function.  
.
Más respuestas (1)
  Sam Chak
      
      
 el 24 de Jul. de 2023
        Hi @Tunde Adubi
If you have the data, and you can find exactly where the two intervals are from scrutizing the data, then you can make the first plot, picking the color hex you like, then retain current plot using 'hold on' when adding another plot. See example below.
You can also use findchangepts() command to find abrupt changes in the signal, if the data has too many points for you to manually scrutize.
% data
x  = linspace(0, 1, 1001);
y1 = sin(2*pi*x(1:500));
y2 = sin(2*pi*x(501:end));
% plots
plot(x(1:500),   y1, 'linewidth', 2, 'color', '#63c3de'), hold on
plot(x(501:end), y2, 'linewidth', 2, 'color', '#efb255'), hold off
xline(0.5, '--')
% labels
xlabel('x'), ylabel('y')
ylim([-1.5 1.5])
grid on
2 comentarios
  Sam Chak
      
      
 el 24 de Jul. de 2023
				@Tunde Adubi, Can you check whether the variables in your script are overshadowed by the variables having the same name in your workspace?
Ver también
Categorías
				Más información sobre Line Plots 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!






