MATLAB subplot title and axes labels

萝らか妹 提交于 2019-12-03 03:24:11
gnovice

For the axis labels, Matt is correct about them having to be placed after the call to BAR. That will take care of one axis label problem. However, you'll likely notice that your y-axis labels in particular may end up being written over one another if they are too long. You have a couple of options to fix this. First, you can adjust the font size in your call to YLABEL:

ylabel('Number of Occurrences','FontSize',7);

Second, you can convert one long label into a multi-line label by using a cell array of strings instead of just a single string:

ylabel({'Number of' 'Occurrences'});

To add a title to the entire figure, the best option is probably to make a UICONTROL static text object and adjust its position so it is placed near the top of the figure. You can get the size and the position of the figure first to help you place the text box near the top and center:

figureSize = get(gcf,'Position');
uicontrol('Style','text',...
          'String','My title',...
          'Position',[(figureSize(3)-100)/2 figureSize(4)-25 100 25],...
          'BackgroundColor',get(gcf,'Color'));

This will create a static text box of width 100 pixels and height 25 pixels placed at the center of the top of the figure and with the same background color as the figure.

George Skoptsov

Here's a solution I saw on a MATLAB exchange forum a while back and that worked for me pretty well. After creating the figure, execute the following sequence of commands:

set(gcf,'NextPlot','add');
axes;
h = title('Intended Figure Title');
set(gca,'Visible','off');
set(h,'Visible','on');

suptitle is what you are looking for.

It places the title centered above all plots.

SUPTITLE Puts a title above all subplots.
    SUPTITLE('text') adds text to the top of the figure
    above all subplots (a "super title"). Use this function
    after all subplot commands.

As far as I know the title function places text relative to a set of axes, so there is no such thing as a figure title. Possible workarounds include using title for a well placed subplot only (such as the first one, or the middle one of the top row), or manually creating a set of axes in the location where you want your title.

As for the axis labels, try putting the label commands after the bar command.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!