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

yanqi liu
yanqi liu 2022년 1월 11일
only at the border of every pixel
may be use meshgrid and mesh to plot over image
Chandan
Chandan 2024년 5월 12일
how to make the grid in crop field image
DGM
DGM 2024년 5월 13일
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.

댓글을 달려면 로그인하십시오.

 채택된 답변

DGM
DGM 2022년 1월 11일

2 개 추천

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

Rabih Sokhen
Rabih Sokhen 2022년 1월 11일
thank you DGM
Rabih Sokhen
Rabih Sokhen 2022년 1월 12일
편집: Rabih Sokhen 2022년 1월 12일
Hello DGM
how can i add some transparency to the grid in a way the grid will not hide the figure in the case of a large array?
thank you in advance
DGM
DGM 2022년 1월 13일
You can try doing
hm.EdgeAlpha = 0.2; % pick some value
which will make the grid lines transparent, but adding alpha to plot objects tends to make plot manipulation laggy (at least for me, it does). It'll still make large images darker though.
If you don't intend on zooming in on larger images, you might just conditionally disable the grid lines if the image is larger than some particular size.
Rabih Sokhen
Rabih Sokhen 2022년 1월 14일
hy Dgm
i still have one more question
your code work very well
But how can I stop interaction between the "brush/data" and the grid line wen i use the brush in the figure to see point?
DGM
DGM 2022년 1월 14일
I messed around with that for a while and I don't know if it's possible. As far as I know, there is no way to restrict the brush to a particular object in a figure. At least in the version I'm using (R2019b), the data brush doesn't even interact with the image object alone, so the only brushable thing is the mesh. Unless something has changed (or if you've added other objects to the figure), then I don't know what you're trying to use the brush on. Strangely, the datatip tool seems to only work on the image, but not the mesh.
If the brush does interact with the image in newer versions, you could always use the xg and yg vectors to set the xticks and yticks of the parent axes. That way you could use the axes grid instead of a mesh overlay. Like I mentioned though, your ticklabels will be a mess to straighten out if you do that.
Rabih Sokhen
Rabih Sokhen 2022년 1월 14일
okay deal
what should i write in my code to use the xg and yg vectors to set the xticks and yticks of the parent axes. ?
thank you in advance
You can try this
s = [4 5]; % [y x]
xrange = [-1 1];
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
set(gca,'xtick',xg,'ytick',yg)
grid on
Note that the ticklabels correspond to the ticks, and so are necessarily not aligned with the face centers anymore. Also, if you have a larger image, all the tick labels will end up overlapping and turning into a mess. You'll have to decide how you want to discard them. In this example, I'm discarding everything but every fourth label. This strategy obviously depends heavily on the size of the image, the label size, and the figure size.
figure % reset web-plot
s = [40 50]; % [y x]
xrange = [-1 1];
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
set(gca,'xtick',xg,'ytick',yg)
grid on
% just skip some labels
xtlold = get(gca,'xticklabels');
xtlnew = repmat({''},numel(xtlold),1);
xtlnew(1:4:end) = xtlold(1:4:end);
set(gca,'xticklabels',xtlnew);
ytlold = get(gca,'yticklabels');
ytlnew = repmat({''},numel(ytlold),1);
ytlnew(1:4:end) = ytlold(1:4:end);
set(gca,'yticklabels',ytlnew);
Rabih Sokhen
Rabih Sokhen 2022년 1월 14일
thank you DGM, it works
Kristoffer Walker
Kristoffer Walker 2024년 1월 23일
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 2025년 1월 3일
편집: Stephen23 2025년 1월 3일
"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:

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

질문:

2022년 1월 10일

편집:

2025년 1월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by