How can I put text on one specified bar with same size of bar and text?

9 views (last 30 days)
I am using the MATLAB bar3 plot. My view of the plot is from above, so I don't see the height of the bars. Because of that I colored the bars by height. The view will stay in future use the same, it won't change. I also set the plotboxaspectratiomode property to 'auto' to get rectangular bars, which I need to have. My aim is to put some text on one specified bar, wich has the same size as the bar. I use the function text from MATLAB so I can put the text on the position specified by the axis values x and y. But my problem is that the x- a and y-axis will change in future use, so also the size of the bars will change. How to implement that I create the textbox size specified by the bar size? Maybe I shouldn't use the function text?
I tried to get the bar width, but I can't get the bar width from the function bar3. I get it only from the function bar. Or I don't know how to get it.
Thank you for yor help in advance!

Accepted Answer

Yucheng Ma
Yucheng Ma on 18 Aug 2014
It is my understanding that you would like to determine the position of the text based on the position of the corresponding bar plotted by "bar3". You may do it as follows:
Suppose "Z" is an n-by-m matrix, and we use
h = bar3(Z')
to create a 3D bar plot, then "h" will be a 1-by-n row vector, in which each element is a handle of a row of bars corresponding to a row in "Z". Suppose we would like to get the position of the bar specified by "Z(i, j)". We can do this by referring to the "XData" and the "YData" properties of the ith handle.
xdata = get(h(i), 'XData');
ydata = get(h(i), 'YData');
The "xdata" and "ydata" are coordinate arrays specifying all the bars in the ith row of "Z". We can get their X coordinates as follows:
xdata = xdata(~isnan(xdata));
xcoor = unique(xdata(:));
xleft = xcoor(1);
xright = xcoor(2);
The "xleft" and "xright" are the X coordinates of the left and right boundaries of the bars, respectively.
We can get the Y coordinates of the bar corresponding to "Z(i, j)" as follows:
ydata = ydata(~isnan(ydata));
ycoor = unique(ydata(:));
ytop = ycoor(2 + 2 * (j - 1));
ybottom = ycoor(1 + 2 * (j - 1));
The "ybottom" and "ytop" are the Y coordinates of the bottom and top boundaries of the bar, respectively.
With "xleft", "xright", "ybottom" and "ytop", you can specify the four vertices of the top view of the bar "Z(i, j)" in the "bar3" plot. And then you can create the textbox using "text" as you intended.
For example, you can create the textbox as follows:
textX = (xleft + xright) / 2;
textY = (ybottom + ytop) / 2;
textZ = Z(i, j) + offset;
texth = text(textX, textY, textZ, 'SomeText', ...
'HorizontalAlignment', 'center', ...
'VerticalAlignment', 'middle');

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!