Setting the bar positions / centers in bar3

4 次查看(过去 30 天)
Hello everybody,
I would like to know if someone figured out a smart way on how to overlay a 2D plot with a bar3 (top-down view) which are properly aligned in x-y position.
To give a quick motivation, I have a 2D space, e.g., x in [-100,100] and y in [-100 100] gridded with 10 bins each dimension. I have binned data given in a 10x10 matrix 'Z'. Now I would like to plot the values of the binned data on a 2D plane, with colored squares, where the size of the squares corresponds to the binsize, the bincenters to the 'true' location in the 2D space and the color to the value in Z.
0) It is not possible to use hist3 since the data is given as is (binned form). As far as I know a call like
hist3(Binned_Data,Bin_Centers),
is not possible (see also this question).
1) The straight forward idea, which I would favour to use, would be to simply adjust the bar-positions in x-y, somthing like:
bar3(Y_Bar_Positions,X_Bar_Positions,Binned_Data)
however bar3 does only allows to adjust the Y_bar_Positions
bar3(Y_Bar_Positons,Binned_Data),
which only enables me to align the bars to the bincenters in one dimension (in the example case above Y_Bar_Positons=[-90,-70,...,70,90]). Unfortunately in the other dimension the bars are placed at [0,1,...,9,10].
2) A second approach would be to use the surf()-function, which does not the job I want, since it places the Z value at the grid points (bin centers) and linear interpolates, therefore producing a spiky plot. This does not give a constant colors over the squares when seen from top-down view.
3) In the end there would be the option to construct this by hand using the patch function, but this seems to be a rather intensive solution, which I would like to avoid if someone has a better idea.
Thanks a lot for any suggestions.
Stephan

采纳的回答

Stephan M. H.
Stephan M. H. 2013-5-13
编辑:Stephan M. H. 2013-5-13
I actually found now a fast solution which works quite well for me. In case someone else is facing the same problem I post it here for sake of completeness.
Basically you display the value matrix Z as an image. Luckily the image/ imagesc function allows to locate each pixel on a given axis object. A square will be centered at the given xy-location with no gaps between the pixels. This is what it looks like:
Z = Binnned_Data % matrix containing the data
X = X_Pix_Positions % vector with x-positions, size(Z,1)
Y = Y_Pix_Positions % vector with y-positions, size(Z,2)
Transparency = ones(size(Z))*0.5 % setting the alpha value of each pixel(50%)
figure(999)
hold on;
plot(...) % above mentioned 2D plot
temp_img = imagesc(X,Y,Z,'AlphaData',Transparency);
Of course the transparency is an option but since I'm talking about an overlay ...
Another really usefull option here is to adjust the color range of the pixels by adding
clims = [a,b] % I chose a and b as min/max values in Z
imagesc(temp_img,clims);
That's pretty much it. Still, if someone already has defined a patch-object that does the same job I would be interested to see that.
best,
Stephan

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graphics Performance 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by