Positional bars grouped line chart matlab

In the following chart

y = [2 2 3; 2 5 6; 2 8 9; 2 11 12];
bar(y)

enter image description here

How can I get the position of each bar to super superimpose a marker?

For example, I would like to put a star on top of the 2nd (2nd row of the first group) and the 5th (2nd bar of the second group).

I would prefer a solution that allows me to modify the plot after creation. (taking into account the picture). Thanks

+4
source share
1 answer

You can use Xdata and Ydata for this:

y = [2 2 3; 2 5 6; 2 8 9; 2 11 12];
h=bar(y);

% getting xdata and ydata from second bar in each group
xdata= get (h(2),'XData');
ydata= get (h(2),'YData');

% plot a * on second bar from second group
hold on;
offset=0.25;
plot(xdata(2),ydata(2)+offset,'-*');

enter image description here

If you want to mark the panel in the center of the group, this method works, but if you want to mark, for example, the first of one group, you must adjust the position * with the offset value on the x axis.

, :

y = [2 2 3; 2 5 6; 2 8 9; 2 11 12];
h=bar(y);

% getting xdata and ydata from second bar in each group
xdata= get (h(3),'XData');
ydata= get (h(3),'YData');

% plot a * on second bar from second group
hold on;
offset=0.25;
xoffset = 0.23; % manual set of get from properties of bar handle
plot(xdata(2)+xoffset,ydata(2)+offset,'-*');

enter image description here

+1

Source: https://habr.com/ru/post/1608773/


All Articles