# How can I add x and y ticks to my imagesc graph?

44 views (last 30 days)
Alessandro Maria Laspina on 23 Nov 2021
Edited: Dave B on 24 Nov 2021
I have the graph with the x and y data given in the files attached. These values vary in order of magnitude from 10^1 to 10^5, and 10^0 to 10^4 respectively. I have used centered ticks for imagesc graphs below, but for some reason with this data it just does not want to do it. The variable n for imagesc is also attached.
imagesc(n)
ax = gca;
ax.XTick = x;
ax.YTick = y;
ax.YDir = 'normal';
What I get is this:
Clearly, it does not even put all the ticks and the ticks are not even correct. The data x and y varies between said magnitude and each succesive element in their vectors jump by an order of magnitude less than the previous value, for example: 1e-1 2e-1 ... 9e-1 1e1 2e1 3e1 and so on. I've tried using pcolor instead to no avail.

Dave B on 23 Nov 2021
Edited: Dave B on 23 Nov 2021
When you use image (or imagesc), the values in the matrix are distributed evenly. Changing the ticks just changes what's labels.
It sounds to me like pcolor is what you want. But you need to specify the x and y values when calling pcolor (and if you like, also) when specifying the ticks. Finally, you might consider a log scale for these data.
I've created all four combinations below (I set color limits just to make it easier to see)
pcolor(x,y,n);
caxis([0 100])
pcolor(x,y,n)
xticks(x)
yticks(y)
caxis([0 100])
pcolor(x,y,n)
set(gca,'XScale','log','YScale','log','CLim',[0 100])
caxis([0 100])
pcolor(x,y,n)
set(gca,'XScale','log','YScale','log','XTick',x,'YTick',y,'CLim',[0 100])
note that you can do 'shading flat' if you want to avoid all the lines:
pcolor(x,y,n)
set(gca,'XScale','log','YScale','log','XTick',x,'YTick',y,'CLim',[0 100])
Dave B on 24 Nov 2021
Edited: Dave B on 24 Nov 2021
When you set CLim to [0 100] it doesn't mean there are 100 colors. Instead, it means that values in your image data that are 100 or greater are mapped to the top of the colormap, values 0 or less are mapped to the bottom of the colormap, and values between 0 and 100 are mapped to the contents of the colormap. You might call this 'linear clamped'
x=linspace(-50,200,100);
y=max(min(x,90),10);
patch([x flip(x)],[y flip(y)],[y flip(y)],'FaceColor','none','EDgeColor','interp','LineWidth',3)
colormap jet
yticklabels(["CLim(1) == 5" repelem("",numel(yticks)-2) "CLim(2) == 15"])
It sounds like your process sort-of recreated the work of binscatter, but with a nonlinear bin size. I would think that a log scale with some color limits would help, but I'm not sure what else to suggest that would help you accomplish your goal. You could transform the values in n however you want, but I'm not sure what nonlinearity would be helpful. You could even bin them as you binned the x and y values (choosing an order of magnitude binning scheme as you like) and then display those bins (if you go this route you could use discretize to transform n into a binned version of n)
edges=[0 logspace(1,5,10)];
nb=discretize(n,edges);
imagesc(nb);
c=colorbar;
c.Ticks=1.5:9.5;
bincenters=edges(1:end-1)+diff(edges)/2;
c.TickLabels=bincenters;
colormap(hot(9))