Grid overlay on "plots()"

500 vues (au cours des 30 derniers jours)
Articat
Articat le 15 Jan 2020
Commenté : Adam Danz le 15 Jan 2020
Can you overlay a grid on "plots()" in the same way "imagesc()" allows you to overaly a grid on an image?

Réponses (2)

Steven Lord
Steven Lord le 15 Jan 2020
You want to turn grid on? Use the grid function.

Adam Danz
Adam Danz le 15 Jan 2020
Modifié(e) : Adam Danz le 15 Jan 2020
Here are two ways of adding a grid to a plot.
Use the grid function
Use grid on to turn on the axis grid where a grid line will be drawn for every axis tick.
Use grid minor to turn on the minor grid lines between axis ticks.
To adjust the spacing of grid lines, change the major and minor axis ticks for each axis.
% Produce plot
cla()
ax = gca();
plot(ax, rand(1,200),rand(1,200),'bo','MarkerfaceColor','b')
% Set major tick marks and grid lines
grid(ax, 'on')
set(ax,'XTick',0:0.2:1,'YTick',0:0.2:1)
% Set minor tick marks and grid lines
grid(ax,'Minor')
set(ax.XAxis,'MinorTick','On','MinorTickValues',0:0.1:1)
set(ax.YAxis,'MinorTick','On','MinorTickValues',0:0.1:1)
Set grid properties (here's a full list). By default the grid is under the plotted data. The line below puts the grid on top, sets the color to red and the minor grid lines to violet.
set(gca,'Layer','top','GridColor','r','GridAlpha',1,...
'MinorGridLineStyle','-','MinorGridColor',[.92 .51 .93],'MinorGridAlpha',1)
Use xline and yline functions
Alternatively, you can use the xline and yline functions to create a grid that is independent of the axis ticks. Since neither of those function accept non-scalar inputs, they must be wrapped in an array function.
% Produce plot
cla()
ax = gca();
plot(ax, rand(1,200),rand(1,200),'bo','MarkerfaceColor','b')
% Define x and y grid
xgrid = 0:0.2:1;
ygrid = 0:0.2:1;
% plot grid lines with red lines and a width of 2
xl = arrayfun(@(x)xline(x,'r-','LineWidth',2),xgrid);
yl = arrayfun(@(y)yline(y,'r-','LineWidth',2),ygrid);
xline and yline produce constantLine objects. Here's a list of their properties. xl and yl will be an array of these objects, one element per line. Here's a demo how to make changes to their properties after plotting.
set(xl, 'LineWidth', 1, 'Alpha', 1)
set(yl, 'LineWidth', 1, 'Alpha', 1)
200114 204217-Figure 1.png
  2 commentaires
Articat
Articat le 15 Jan 2020
This is helpful but not exactly what I am looking for.
In "imagec()" you can do the following:
imagesc((xGrid2(1,:,1)),permute(yGrid2(1,1,:), [3 1 2]),velocity_data)
where "xGrid2"(137x145x19 matrix) and "yGrid2"(137x145x19 matrix) are matricies of points that allow you to define the location of the index values of "velocity_data"(137x145 matrix).
So for example, to plot velocity_data(1,1) on the grid I want, I would have to specify both xGrid(1,1,1) and yGrid(1,1,1) so matlab knows where to place the index value at that point.
Now, my question is, can I do the same for "plot()"? Maybe not the same concept as using index values(like that of "velocity_data") but something that allows me to define a grid and then plot my 2D values on that grid?
Adam Danz
Adam Danz le 15 Jan 2020
That's much different from what's asked in your original question.
I understand your description that imagesc(x,y,C) plots the colors C at the coordinates of (x,y). That doesn't necessarily mean x and y form a grid. They are just coordinates on the axes.
Are you trying to do the same by using a scatter plot? I'm not clear on what the goal is.
Check out this scatter plot example where (x,y) coordinates specify the location of colored points.

Connectez-vous pour commenter.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by