凌绝顶 发表于 2014-7-18 19:58

ticklabel中可否插入Latex公式?

比如xticklabel为 X_1, X_2,。。。即X加数字下标。如果可以,怎么操作?谢谢

chybeyond 发表于 2014-7-18 20:07

本帖最后由 chybeyond 于 2014-7-19 00:50 编辑

function ht = my_xticklabels(varargin)

%MY_XTICKLABELS replaces XTickLabels with "normal" texts
%   accepting multiline texts and TEX interpreting
%   and shrinks the axis to fit the texts in the window
%
%    ht = my_xticklabels(Ha, xtickpos, xtickstring)
% or
%    ht = my_xticklabels(xtickpos, xtickstring)
%
%in:    xtickpos   XTick positions
%      xtickstring   Strings to use as labels {N*1} cell of cells
%
% Examples:
% plot(randn(20,1))
% xtl = {{'one';'two';'three'} '\alpha' {'\beta';'\gamma'}};
% h = my_xticklabels(gca,,xtl);
% % vertical
% h = my_xticklabels(,xtl, ...
%   'Rotation',-90, ...
%   'VerticalAlignment','middle', ...
%   'HorizontalAlignment','left');

% Pekka Kumpulainen 12.2.2008

textopts = {};
if length(varargin{1})==1 && ...
      ishandle(varargin{1}) && ...
      strcmpi(get(varargin{1},'Type'),'axes');
    Ha = varargin{1};
    xtickpos = varargin{2};
    xtickstring = varargin{3};
    if nargin > 3
      textopts = varargin(4:end);
    end
else
    Ha = gca;
    Hfig = get(Ha,'Parent');
    xtickpos = varargin{1};
    xtickstring = varargin{2};
    if nargin > 2
      textopts = varargin(3:end);
    end
end

set(Ha,'XTick',xtickpos, 'XTickLabel','')
h_olds = findobj(Ha, 'Tag', 'MUXTL');
if ~isempty(h_olds)
    delete(h_olds)
end

%% Make XTickLabels
NTick = length(xtickpos);
Ybot = min(get(gca,'YLim'));
ht = zeros(NTick,1);
for ii = 1:NTick
    ht(ii) = text('String',xtickstring{ii}, ...
      'Units','data', ...
      'VerticalAlignment', 'top', ...
      'HorizontalAlignment', 'center ', ...
      'Position',, ...
      'Tag','MUXTL');
end
if ~isempty(textopts)
    set(ht,textopts{:})
end

%% squeeze axis if needed

set(Ha,'Units','pixels')
Axpos = get(Ha,'Position');
% set(Hfig,'Units','pixels')
% Figpos = get(Hfig,'Position');

set(ht,'Units','pixels')
TickExt = zeros(NTick,4);
for ii = 1:NTick
    TickExt(ii,:) = get(ht(ii),'Extent');
end

needmove = -(Axpos(2) + min(TickExt(:,2)));

if needmove>0;
    Axpos(2) = Axpos(2)+needmove+2;
    Axpos(4) = Axpos(4)-needmove+2;
    set(Ha,'Position',Axpos);
end

set(Ha,'Units','normalized')
set(ht,'Units','normalized')Matlab图形中title、xlabel、ylabel、zlabel、textbox和legend等支持latex,xticklabel好像不行,可以用上面函数实现


页: [1]
查看完整版本: ticklabel中可否插入Latex公式?