Matlab bar plot grouped but in different y scales - matlab

I have two sets of data, and I want to plot using bar graph. But the problem is these two sets of data are at quite different scale. If I just use the bar(A), it would look like this: grouped but the second data set is barely visible because the scale.
However, if I use the plotyy(x,y1,x,y2), the plot will be like this: two sets of data are in different scale, but the bar graphs are not grouped, the second data sets overlaps to the first.
So I am wondering if there is a way to plot the bar graph grouped like the first figure, but the two datasets are using separate y scales? Or is there a way to adjust the horizontal offset of the bar graph in second plot so it looks like "grouped".
Thanks!

This uses the plotyy(x1,y1,x2,y2,fun1,fun2) variant of plotyy:
%// Set these three variables as desired
offset = (x(2)-x(1))/8;
width = (x(2)-x(1))/4;
colors = {'b','g'};
%// Do the plot
plotyy(x-offset,y1,x+offset,y2, #(x,y) bar(x,y,width,colors{1}), #(x,y) bar(x,y,width,colors{2}));
If you prefer x-ticks to appear only on used x values:
h = plotyy(x-offset,y1,x+offset,y2, #(x,y) bar(x,y,width,colors{1}), #(x,y) bar(x,y,width,colors{2}));
set(h,'xtick',x)

Related

How to rescale vector field onto a different grid?

I currently have a vector field that looks something like this, generated with the following basic structure, where Z is some matrix:
[X,Y] = meshgrid(x,y)
[grad_x, grad_y] = gradient(Z)
quiver(X,Y,grad_x,grad_y)
I would like for this plot to be rescaled, such that the x-axis ranges from 1.5 to 3.8 and the y-axis ranges from 100 to 250, but for the arrows themselves to look identical. The only difference in the figure should be the axes labels.
I have tried:
grad_x_rescaled = [(grad_x - min(grad_x))./(max(grad_x)-min(grad_x))].*(3.8-1.5);
grad_y_rescaled = [(grad_y - min(grad_y))./(max(grad_y)-min(grad_y))].*(250-100);
But the problem with this is that although the grad_x and grad_y get rescaled overall, the scaling of the arrows themselves relative to each other are not conserved, and results in below (note the thick black streaks are presumably arrowheads, but the important thing is that the direction and relative sizes of the arrows are not exactly like in the first case.
Is there a matlab function or an expression to renormalize data into a new range, but such that the renormalized data is scaled relative to itself (such as the arrows should be scaled the same relative to one another)?
To simply change the axes tick labels you could use Matlab's ability to specify tick marks and tick labels. Basically you would just tell Matlab where to put the ticks and what the labels should say like this:
xticks(linspace(0,1,6))
xticklabels(linspace(1.5,3.8,6))
yticks(linspace(0,1,6))
yticklabels(linspace(100,250,6))

How to plot same point with two distinct makers having different Markersize and colour in MATLAB?

I want to use two distinct markers of different colour and size in MATLAB plot on same point for illustration purpose.
plot(100,200,'b*');
plot(100,200,'go','MarkerSize',12);
Though above two statement works perfectly, but i want to use it on a large number of points. Can above these two statements be combined into a single plot?
just write a simple function yourself:
function emphasizePoint(X,Y)
hold on;
plot(X,Y,'b*');
plot(X,Y,'go','MarkerSize',12);
end
and use it like
X = rand(1,100);
Y = rand(1,100);
emphasizePoint(X,Y);

Matlab: How to use a suitable color bar?

I have a picture which shows the magnitude of some value in terms of color, like this one:
The higher the magnitude, the more it looks red. However there are only a few points at the edge has very high values and most of the points have much lower values. If a colorbar with equal intervals is used the picture just looks blue throughout and the values in most areas cannot be distinguished.
Is it possible to set a colorbar that has, say, exponentially increasing intervals (or others) so that the center part can also show different colors?
Here is one way, it's not quite right but it's close. The idea is to make your own log spaced colour map. I do it by using linear interpolation between log spaced break points. It could probably be improved to use logarithmic interpolation (and maybe have better end cases):
First I simulate some data (open to suggestions for simulating data that better illustrates this example):
M = exp(rand(50)*10)
Then plot it (ignore the figure(2), that's just to make this match the image later)
n = 64
figure(2)
imagesc(M)
colormap(jet(n))
colorbar
now create a log spaced colour map
linMap = jet(n);
breaks = round(0:n/5:n)';
breaks(1) = 1;
cBreaks = linMap(breaks,:);
idx = 2.^(6:-1:1)';
idx(end) = 0; %// this part is hacky
logMap = interp1(((idx)/64)*max(M(:)),flipud(cBreaks),linspace(0,max(M(:)),64)); %// based on http://stackoverflow.com/questions/17230837/how-to-create-a-custom-colormap-programatically
figure(1)
imagesc(M)
colormap(logMap)
colorbar
results in
as you can see, the data remain unchanged and the data inspector still gives you back the same values but the colour bar is pretty much on a log scale now. I'd be interested to see what this looks like on your data.

Making an accurate colorbar for a simple plot

I am trying to make a simple plot (for this example doing a plot of y=x^2 will suffice) where I want to set the colors of the points based on their magnitude given some colormap.
Following along my simple example say I had:
x = 1:10;
y = x.^2;
Use gscatter(x,y,jet(10)); legend hide; colorbar which produces a plot with the points colored but the colorbar does not agree with the colored values. (Can't post picture as this is my first post). Using a caxis([1,100]) command gives the right range but the colors are still off.
So I have two questions:
(1) How can I fix the colors to fit to a colorbar given a range? In my real data, I am looking at values that range from -50 to 50 in some instances and have many more data points.
(2) I want to create a different plot with the same points (but on different axes) and I want the colors of each point on this new plot to have the same colors as their counterparts in the previous plot. How can I, programmatically, extract the color from each point so I can plot it on two different sets of axes?
I would just move the points into a matrix and do an imagesc() command but they aren't spaced as integers or equally so simple scaling wouldn't work either.
Thanks for any help!
Regarding you first question, you need to interpolate the y values into a linear index to the colormap. Something like:
x = 1:10;
y = x.^4;
csize = 128;
cmap = jet(csize);
ind = interp1(linspace(min(y),max(y),csize),1:csize,y,'nearest');
scatter(x,y,14,cmap(ind,:),'filled')
colorbar
caxis([min(y) max(y)])
Using interp1 in this case is an overkill; you could calculate it directly. However, I think in this way it is clearer.
I think it also answers your 2nd question, since you have the index of the color of each data point, so you can use it again in the same way.

How do I plot data labels alongside my data in a bar graph

I have a bar graph in which i would like to plot data labels alongside my data points. I have looked at this documentation and they don't seem to have what i need. This is all done using MATLAB.
Below is an example of what i'd like, although for a bar graph instead of a scatter plot.
Use TEXT function to label the bars. STRCAT function can be used to create custom labels.
x = (1:5)';
y = rand(5,1);
bar(x,y)
%# show X and Y coordinates
text(x,y,strcat('(',num2str(x),',',num2str(y,2),')'),...
'horiz','center','vert','bottom')
You can also add some small gap to y coordinates to make text a little higher.
Use the code below and customize in your case.
for ii = 1:numel(X)
text(X(ii)+.02, Y(ii)+.02,textCell{ii},'FontSize',8)
end