声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 2080|回复: 1

[绘图技巧] ticklabel中可否插入Latex公式?

[复制链接]
发表于 2014-7-18 19:58 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
比如xticklabel为 X_1, X_2,。。。即X加数字下标。如果可以,怎么操作?谢谢
回复
分享到:

使用道具 举报

发表于 2014-7-18 20:07 | 显示全部楼层
本帖最后由 chybeyond 于 2014-7-19 00:50 编辑
  1. function ht = my_xticklabels(varargin)

  2. %MY_XTICKLABELS replaces XTickLabels with "normal" texts
  3. %   accepting multiline texts and TEX interpreting
  4. %   and shrinks the axis to fit the texts in the window
  5. %
  6. %    ht = my_xticklabels(Ha, xtickpos, xtickstring)
  7. % or
  8. %    ht = my_xticklabels(xtickpos, xtickstring)
  9. %
  10. %  in:    xtickpos     XTick positions [N*1]
  11. %        xtickstring   Strings to use as labels {N*1} cell of cells
  12. %
  13. % Examples:
  14. % plot(randn(20,1))
  15. % xtl = {{'one';'two';'three'} '\alpha' {'\beta';'\gamma'}};
  16. % h = my_xticklabels(gca,[1 10 18],xtl);
  17. % % vertical
  18. % h = my_xticklabels([1 10 18],xtl, ...
  19. %     'Rotation',-90, ...
  20. %     'VerticalAlignment','middle', ...
  21. %     'HorizontalAlignment','left');

  22. % Pekka Kumpulainen 12.2.2008

  23. textopts = {};
  24. if length(varargin{1})==1 && ...
  25.         ishandle(varargin{1}) && ...
  26.         strcmpi(get(varargin{1},'Type'),'axes');
  27.     Ha = varargin{1};
  28.     xtickpos = varargin{2};
  29.     xtickstring = varargin{3};
  30.     if nargin > 3
  31.         textopts = varargin(4:end);
  32.     end
  33. else
  34.     Ha = gca;
  35.     Hfig = get(Ha,'Parent');
  36.     xtickpos = varargin{1};
  37.     xtickstring = varargin{2};
  38.     if nargin > 2
  39.         textopts = varargin(3:end);
  40.     end
  41. end

  42. set(Ha,'XTick',xtickpos, 'XTickLabel','')
  43. h_olds = findobj(Ha, 'Tag', 'MUXTL');
  44. if ~isempty(h_olds)
  45.     delete(h_olds)
  46. end

  47. %% Make XTickLabels
  48. NTick = length(xtickpos);
  49. Ybot = min(get(gca,'YLim'));
  50. ht = zeros(NTick,1);
  51. for ii = 1:NTick
  52.     ht(ii) = text('String',xtickstring{ii}, ...
  53.         'Units','data', ...
  54.         'VerticalAlignment', 'top', ...
  55.         'HorizontalAlignment', 'center ', ...
  56.         'Position',[xtickpos(ii) Ybot], ...
  57.         'Tag','MUXTL');
  58. end
  59. if ~isempty(textopts)
  60.     set(ht,textopts{:})
  61. end

  62. %% squeeze axis if needed

  63. set(Ha,'Units','pixels')
  64. Axpos = get(Ha,'Position');
  65. % set(Hfig,'Units','pixels')
  66. % Figpos = get(Hfig,'Position');

  67. set(ht,'Units','pixels')
  68. TickExt = zeros(NTick,4);
  69. for ii = 1:NTick
  70.     TickExt(ii,:) = get(ht(ii),'Extent');
  71. end

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

  73. if needmove>0;
  74.     Axpos(2) = Axpos(2)+needmove+2;
  75.     Axpos(4) = Axpos(4)-needmove+2;
  76.     set(Ha,'Position',Axpos);
  77. end

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


评分

1

查看全部评分

您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-11-28 18:06 , Processed in 0.062159 second(s), 18 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表