105 views (last 30 days)
Show older comments
Alessandro Maria Laspina on 23 Nov 2021
Edited: Dave B on 24 Nov 2021
Accepted Answer: Dave B
- image_sc_eg.mat
Open in MATLAB Online
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.
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
Accepted Answer
Dave B on 23 Nov 2021
Edited: Dave B on 23 Nov 2021
Open in MATLAB Online
- image_sc_eg.mat
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)
load('image_sc_eg.mat')
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])
shading flat
10 Comments Show 8 older commentsHide 8 older comments
Show 8 older commentsHide 8 older comments
Alessandro Maria Laspina on 23 Nov 2021
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/1594184-how-can-i-add-x-and-y-ticks-to-my-imagesc-graph#comment_1849619
Edited: Alessandro Maria Laspina on 23 Nov 2021
This is great. Anyway I can make the cells be of equal size? I want the ticks to be at the center of the cells, so that they represent the values between ranges, for example, a value between 1e-1 and 2e-1. Making the cells of equal size would also avoid the overlap of the tick labels.
Dave B on 23 Nov 2021
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/1594184-how-can-i-add-x-and-y-ticks-to-my-imagesc-graph#comment_1849664
Open in MATLAB Online
- image_sc_eg.mat
Yes of course, I didn't even think of that version, and indeed you can do that with imagesc. I must have misread the question. What you want (I think) is to spearate out the ticks and the tick labels.
There are actually a few ways to do this (e.g. you could specify x limits when calling imagesc), but I think this is the easiest one to think about:
load('image_sc_eg.mat')
imagesc(n); % Can read this as: imagesc(1:size(n,2), 1:size(n,1), n)
set(gca, 'XTick', 1:size(n,2), ...
'XTickLabel', string(x), ...
'YTick', 1:size(n,1), ...
'YTickLabel', string(y), ...
'CLim',[0 100]);
Alessandro Maria Laspina on 23 Nov 2021
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/1594184-how-can-i-add-x-and-y-ticks-to-my-imagesc-graph#comment_1849669
Brlliant. Thank you. One last question- what about using scientific notation, or power of 10 notation only for the orders of magnitude? This would increase visibility- I'm attempting it right now but I can't think of how to skip tick labels- maybe just by leaving an empty string vector?
Dave B on 23 Nov 2021
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/1594184-how-can-i-add-x-and-y-ticks-to-my-imagesc-graph#comment_1849699
Open in MATLAB Online
I knew you were going to ask that!
If you want to do every other tick and ticklabel:
set(gca, 'XTick', 1:2:size(n,2), 'XTickLabel', string(x(1:2:end)))
If you want to keep the ticks but skip the labels, I think blank strings are needed. There's a tiny trick here that MATLAB will treat a multi-row set of tick labels as a vector:
xtl = string(x(1:2:end));
xtl(2,:) = "";
set(gca, 'XTick', 1:size(n,2), ...
'XTickLabel', xtl, ...
If you want to do scientific notation (I prefer compose for this):
compose("%0.0e",x)
And you can mix and match however you like:
[string(x(1:5)) compose("%0.0e",x(6:end))]
Alessandro Maria Laspina on 23 Nov 2021
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/1594184-how-can-i-add-x-and-y-ticks-to-my-imagesc-graph#comment_1849704
Thank you so much!
Alessandro Maria Laspina on 23 Nov 2021
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/1594184-how-can-i-add-x-and-y-ticks-to-my-imagesc-graph#comment_1849734
Edited: Alessandro Maria Laspina on 23 Nov 2021
Perhaps two more things- how can I show a colorbar with the labels set to the bin values and not the clim values? I.e. how many values fall within that cell.
Also, how can I extract the CLim values?- Does it just take the maximum, normalise it, and then partition colors based on a linear scale? I don't think its linear since there is a cell that is many orders of magnitude greater than the rest, which even with 100 I expect there to be a mostly blue space.
EDIT: perhaps these are a little far away from the original topic- I believe they warrant a new question.
Dave B on 23 Nov 2021
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/1594184-how-can-i-add-x-and-y-ticks-to-my-imagesc-graph#comment_1849764
Edited: Dave B on 23 Nov 2021
Open in MATLAB Online
For the first bit, I'm not sure I understand the question. colorbar is about what color means, so defining it as how many values fall into a cell would be tricky...it would mean (at the least) discretizing the data so that there are color bins? You could do this but it's a bit of work. On the other hand, setting ticks and tick labels on colorbar works pretty similarly to on an axis. You can set the ticks and tick labels to anything you like!
c=colorbar;
c.Ticks=[0 .2 .5 .9 1];
c.TickLabels=["zero" "point 2" ".5" ".9" "1^1"];
For the second question CLim is really what you described (by default), the color limits. But you should separate out thinking about color limits with color values. You can specify a log scale for color just like you can specify a log scale for an axis:
imagesc((tan(linspace(0,2*pi,100))'*tan(linspace(0,2*pi,100))))
set(gca, 'ColorScale', 'log')
colorbar
colormap turbo
Alessandro Maria Laspina on 23 Nov 2021
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/1594184-how-can-i-add-x-and-y-ticks-to-my-imagesc-graph#comment_1849774
Edited: Alessandro Maria Laspina on 23 Nov 2021
For the first question- thats exactly what I needed.
For the second question- i know we can use a log scale for the colors of Clim, however. For the example prior to the one you show with the log, we used clim between 0 and 100. This means that there are 100 unique identifiable colors on the graph, each cell taking up anyone of these colors, based on the value. How does clim know which color/which color id in the color limit to assign to a cell?
EDIT: for example, the top two values in n are 33200 and 833. CLim can definitely not use a linear scale to give the result you showed before because the color on the color limit 100, bright yellow, is displayed multiple times. In order for a value to display this color using a linear scale, it would have to be equal to or between 33200 and 33200*99/100 (32868)
EDIT 2: 38220 and 813 not 33200 and 833.
Alessandro Maria Laspina on 23 Nov 2021
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/1594184-how-can-i-add-x-and-y-ticks-to-my-imagesc-graph#comment_1849814
Edited: Alessandro Maria Laspina on 23 Nov 2021
Ill explain further why I want to this, perhaps there may be another solution. In short I want to extract multiple regions of interest (ROI) from the graph. These are fairly intuitive to notice when you have a scatter plot. Indeed, n, which I attached, is the result of collecting the total number of points in the respective x and y regions of a scatter plot. From here, I wanted to quantitatively describe where the ROI's are, in terms of cells of varying orders of magnitude. Maybe there are other ways I can do this without graphical means?
Dave B on 24 Nov 2021
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/1594184-how-can-i-add-x-and-y-ticks-to-my-imagesc-graph#comment_1849974
Edited: Dave B on 24 Nov 2021
Open in MATLAB Online
- image_sc_eg.mat
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"])
ylim padded
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)
load image_sc_eg.mat
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))
Sign in to comment.
More Answers (0)
Sign in to answer this question.
See Also
Categories
MATLABGraphicsFormatting and AnnotationColormapsBlue
Find more on Blue in Help Center and File Exchange
Tags
- imagesc
- pcolor
- tick labels
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office