How to rotate and display the values on top of the bar graph

43 次查看(过去 30 天)
Hi all, I would like to display the values of a bar graph on top of the figure rotated by 90 degree. I tried to implement it with the following lines of code but it did not work and did not throw any error. Can anyone please help me ? Here is my code:
g=randn(3,4);
iNoOfEntries=length(g(:));
labels=num2str(round(reshape(g.',iNoOfEntries,1)*100)/100);
figure;
hArray = bar(g);
%set(hArray(2),'LineWidth',2,'EdgeColor','red');
xlabel('number');
ylabel('quantity');
ax1 = gca;
pos=get(ax1,'Position');
pos(4) = pos(4)-0.1;
set(ax1,'Position',pos)
%%create a new axis on top of plot-end of comment
ax2 = axes('Position',get(ax1,'Position'),...
'XAxisLocation','top',...
'Color','none',...
'YTick', [0 1], 'YTickLabel', {'',''});
%make new tick labels (rotated)-end of comment
xlim(xlim(ax1)); % set axis 2 to same xlim
set(ax2, 'XTickLabel', []);
xpos= [0.71 0.9 1.1 1.28 ...
1.71 1.9 2.1 2.28 ...
2.71 2.9 3.1 3.28].';
th= text(xpos,...
repmat(max(max(g))+0.09,iNoOfEntries,1),...
labels,...
'HorizontalAlignment','left','rotation',90);

回答(2 个)

Image Analyst
Image Analyst 2013-7-17
You just need to set the y value of your text string properly. Here, try my demo:
% Demo macro plot a bar chart and give a different color to each bar.
% Also plots the value of the bar above the bar.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 30;
format compact
% Ask user for the number of bars.
defaultValue = 7;
titleBar = 'Enter an integer value';
userPrompt = 'Enter the number of bars';
caUserInput = inputdlg(userPrompt, titleBar, 1, {num2str(defaultValue)});
if isempty(caUserInput),return,end; % Bail out if they clicked Cancel.
integerValue = round(str2double(cell2mat(caUserInput)));
% Check for a valid integer.
if isnan(integerValue)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
integerValue = defaultValue;
message = sprintf('I said it had to be an integer.\nI will use %d and continue.', integerValue);
uiwait(warndlg(message));
end
% Define sample data in the range 20-80.
x = 1 : integerValue;
y = 20 + 80 * rand(integerValue)
numberOfBars = length(y);
button = menu('Use which colormap?', 'Custom', 'Random', 'Jet', 'Hot', 'Lines');
if button == 1
% Make up a custom colormap specifying the color for each bar series.
barColorMap(1,:) = [.2 .71 .3]; % Green Color for segment 1.
barColorMap(2,:) = [.25 .55 .79]; % Blue Color for segment 2.
barColorMap(3,:) = [.9 .1 .14]; % Red Color for segment 3.
barColorMap(4,:) = [.9 .9 .14]; % Yellow Color for segment 4.
% I have not defined any more than 4 colors in this demo.
% For any number of bars beyond 4, just make up random colors.
if numberOfBars > 4
barColorMap(5:numberOfBars, 1:3) = rand(numberOfBars-4, 3);
end
elseif button == 2
% Example of using colormap with random colors
barColorMap = rand(numberOfBars, 3);
elseif button == 3
% Example of using pre-defined jet colormap
barColorMap = jet(numberOfBars);
elseif button == 4
% Example of using pre-defined Hot colormap
barColorMap = hot(numberOfBars);
else
% Example of using pre-defined lines colormap
barColorMap = lines(numberOfBars);
end
% Plot each number one at a time, calling bar() for each y value.
for b = 1 : numberOfBars
% Plot one single bar as a separate bar series.
handleToThisBarSeries(b) = bar(x(b), y(b), 'BarWidth', 0.9);
% Apply the color to this bar series.
set(handleToThisBarSeries(b),'FaceColor', barColorMap(b,:));
% Place text atop the bar
barTopper = sprintf('y(%d) = %.3f', x(b), y(b));
text(x(b)-0.2, y(b)+3, barTopper, 'FontSize', 15);
hold on;
end
% Fancy up the graph.
grid on;
caption = sprintf('Data plotted in %d barseries, each with a different color', length(y));
title(caption, 'FontSize', fontSize);
xlabel('x', 'FontSize', fontSize);
ylabel('y', 'FontSize', fontSize);
% Restore the x tick marks.
set(gca, 'XTickMode', 'Auto');
% set(gca, 'XTickLabels', xTickLabels);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off');

Kekhra
Kekhra 2013-7-19
@image analyst Thank you. But i want the values at the top . I wanted to create a new axis on top and the values to be rotated and displayed vertically.
  1 个评论
Image Analyst
Image Analyst 2013-7-19
As is typical with demos, you use them as a starting point. Rather than assume they're the final solution you're looking for, you adapt them to your needs. I have a lot of demos already made up that I can offer, but I simply don't have time to create free, turn-key, customized solutions for everyone's problems.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Distribution Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by