how to add grid to imagesc?

79 visualizaciones (últimos 30 días)
Rabih Sokhen
Rabih Sokhen el 10 de En. de 2022
Editada: Stephen23 el 3 de En. de 2025
clear all
clc
a=rand(20,50)
x=linspace(-1,1,20)
y=linspace(-1,1,50)
imagesc(x,x,a)
I would like to add grid only at the border of every pixel in a way wen I zoom in only the initial grid appear
can someone help me with this.
thank you in advance
  3 comentarios
Chandan
Chandan el 12 de Mayo de 2024
how to make the grid in crop field image
DGM
DGM el 13 de Mayo de 2024
What grid in what image?
If you have a question to ask, use the Ask link at the top of the page to ask a question instead of hiding vague comments in old threads.

Iniciar sesión para comentar.

Respuesta aceptada

DGM
DGM el 11 de En. de 2022
It depends how you want to visualize the data. Tools like imshow() or imagesc() represent the data points at the center of each facet in the displayed image. Tools like pcolor() represent the data at the vertices. If the latter is what you intend, then just use pcolor() instead of imagesc().
If you actually want the matrix represented as an image, then you'll have to create the grid. One way would be to just overlay a mesh plot.
s = [4 5]; % [y x]
xrange = [-1 1]; % imagesc only needs the endpoints
yrange = [-1 1];
a = rand(s);
dx = diff(xrange)/(s(2)-1);
dy = diff(yrange)/(s(1)-1);
xg = linspace(xrange(1)-dx/2,xrange(2)+dx/2,s(2)+1);
yg = linspace(yrange(1)-dy/2,yrange(2)+dy/2,s(1)+1);
hi = imagesc(xrange,yrange,a); hold on
hm = mesh(xg,yg,zeros(s+1));
hm.FaceColor = 'none';
hm.EdgeColor = 'k';
You might try to do it by setting the grid properties of the axes, but you'll have to contend with the ticklabels if you do that.
As to how you make the added grid appear only at certain zoom levels, you'd have to create some sort of custom callback to do that. I'll leave that to someone else.
  10 comentarios
Kristoffer Walker
Kristoffer Walker el 23 de En. de 2024
This is a long-standing RSI issue. This seems like a low-hanging fruit that someone could easily create an API for to make it so much easier for those of us who use and love imagesc. Just add the property "grid", "on". How hard can it be?
Stephen23
Stephen23 el 3 de En. de 2025
Editada: Stephen23 el 3 de En. de 2025
"How hard can it be?"
Probably fairly hard: the axes object is underneath the image object, so in general its gridlines are not visible.
Resolving this is not trivial.
For example, we could create some graphics object that lies on top of the image object, which would display some gridlines (this is already what users can do using e.g. XLINE & YLINE). However, then come the complication of what order to use when the user plots other objects on top of their image: should the gridlines always float to the top (on top of eveything)? Or should the grid object be stuck "on top of" the image object somehow? What happens when the user reorders the graphics objects using e.g. UISTACK()? What happens to this grid object when the user deletes the image object but keeps all of their other objects? What is the mechanism by which that would work?
Another approach might be to create a special wrapper object which is derived from the image class but adds gridlines over the top of just the image object. Would this be a new class? Would it require its own documentation? Or create an entirely new image class to replace the old one? Is the amount of effort justified when the goal is already achieved using XLINE & YLINE?
Should the "gridlines" of this new object correspond to the gridines of the underlying axes? Should changing one update the other? What is the mechanism by which that would work? What if the user places multiple image objects into one axes?
TMW has to consider more than just your specific use-case.
In any case, you can make an enhancement request here:

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Exploration en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by