Reputation: 7
I want to show a grid in Matlab using Meshgrid with specify lines color ( or any Different method ) , My code is :
figure(1)
X = [-1:0.5:1];
Y= [-1:0.5:1];
[X,Y] = meshgrid(X,Y)
plot(X,Y,'k-')
hold on
plot(Y,X,'k-');
this code show all line with black color, but i want to show some lines with different color like this :
figure(1)
X = [-1:0.5:1]; % with black color
X = [-1:0.2:1]; % with red color
Y= [-1:0.5:1]; % with black color
Y= [-1:0.2:1]; % with red color
how to do that ?
Upvotes: 1
Views: 7064
Reputation: 60444
An alternative would be to use the built-in grid:
h=gca;
grid on % turn on major grid lines
grid minor % turn on minor grid lines
% Set limits and grid spacing separately for the two directions:
h.XAxis.Limits=[-1,1];
h.XAxis.TickValues=-1:0.5:1;
h.XAxis.MinorTickValues=-1:0.2:1;
h.YAxis.Limits=[-1,1];
h.YAxis.TickValues=-1:0.5:1;
h.YAxis.MinorTickValues=-1:0.2:1;
% Must set major grid line properties for both directions simultaneously:
h.GridLineStyle='-'; % the default is some dotted pattern, I prefer solid
h.GridAlpha=1; % the default is partially transparent
h.GridColor=[0,0,0]; % here's the color for the major grid lines
% Idem for minor grid line properties:
h.MinorGridLineStyle='-';
h.MinorGridAlpha=1;
h.MinorGridColor=[1,0,0]; % here's the color for the minor grid lines
Note that you can shorten the code above by setting multiple properties at once:
h=gca;
grid on
grid minor
set(h.XAxis,'Limits',[-1,1],'TickValues',-1:0.5:1,'MinorTickValues',-1:0.2:1)
set(h.YAxis,'Limits',[-1,1],'TickValues',-1:0.5:1,'MinorTickValues',-1:0.2:1)
set(h,'GridLineStyle','-','GridAlpha',1,'GridColor',[0,0,0])
set(h,'MinorGridLineStyle','-','MinorGridAlpha',1,'MinorGridColor',[1,0,0])
Upvotes: 0
Reputation: 4195
just use different color specification when calling plot
:
X = [-1:0.5:1]; % with black color
x = [-1:0.2:1]; % with red color
Y= [-1:0.5:1]; % with black color
y= [-1:0.2:1]; % with red color
[X,Y] = meshgrid(X,Y);
[x,y] = meshgrid(x,y);
plot(X,Y,'k-','Linewidth',2)
hold on
plot(Y,X,'k-','Linewidth',2);
plot(x,y,'r-');
plot(y,x,'r-');
Upvotes: 1