color plot above and below y-axis points
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
monkey_matlab
le 15 Oct 2015
Réponse apportée : Star Strider
le 16 Oct 2015
From my simulation file below, I wanted to color the plot in red for sections of the graph that goes above 100, and below -100. I bascially wanted the plot to look like this:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/150360/image.png)
I have also attached the data file.
Here is my code:
%=======================================================================
% This Matlab Program reads in the data from a text file.
%=======================================================================
% Select file
clear;
clc;
[FileName,PathName] = uigetfile('*.txt','Select data file');
fid = fopen( strcat(PathName,FileName) ,'rt' );
% Read the file and store into matrix v
i = 0;
v = [0 0];
while feof(fid) == 0
buffer = fscanf(fid, '%f', 4);
buffer = buffer';
if i == 0;
v = buffer;
else
v = vertcat(v,buffer);
end
i = i + 1;
end
% Time Vector in ms
time = v(:,1);
time_ms = v(:,1).*1000;
% Freq Data
Freq = v(:,2);
% + Spec 1
Spec1 = v(:,3);
% + Spec 1
Spec2 = v(:,4);
plot(time_ms, Freq, time_ms, Spec1, 'r', time_ms, Spec2, 'r'); grid on; axis([0 7 -500 500]);
title('Zoomed-In Lock Time Response'); legend('Data 1', '+ Spec', '- Spec');
xlabel('Time (ms)'); ylabel('Frequency Offset (Hz)');
return
1 commentaire
dpb
le 15 Oct 2015
The plot background color property is a single value for an axis so can't do that with it on a single plot.
Could either use multiple axes or probably easier, draw a surface on the axes for the two regions and color it. I'd have to play at it, too...
Réponse acceptée
Star Strider
le 16 Oct 2015
This is one way to have the plot appear as you want it to. You will have to experiment to get it exactly the way you want:
x = linspace(0, 8, 800); % Create Data
y = 1000*randn(1,800).*exp(-0.7*x); % Create Data
figure(1)
patch([0 8 8 0], [100 100 500 500], 'r', 'FaceAlpha',0.25)
patch([0 8 8 0], -1*[100 100 500 500], 'r', 'FaceAlpha',0.25)
hold on
axis([0 8 -500 500])
plot(x, y)
hold off
grid on
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/178728/image.png)
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!