雷锦志的个人博客分享 http://blog.sciencenet.cn/u/jzlei 乱七八糟的随想录

博文

输出 Matlab 图形的一个方法

已有 18872 次阅读 2012-5-7 08:58 |系统分类:科研笔记| figure, 输出图形, exportfig

经常看到有些人用 Matlab 画图后保存为 EPS 格式或者 PNG 格式的图时字体太小。不太美观。
这里把我常用的比较好方法和大家分享一下,也给自己和学生留个印记。

附件的 exportfig.m 程序可以很好地把 Matlab 画的图输出为很多格式的文件,并且可以设置字体的大小,颜色等,
使用方便灵活。

用法很简单, 把这个文件放在 Matlab 的搜索目录中(最简单就是当前的工作目录)。
然后:
fig=figure(1);  % 定义一个 fig 的图形句柄
..
..     % 这里用 plot 等函数画图
..
exportfig(fig, 'fig2.eps', 'FontMode', 'fixed','FontSize', 10, 'color', 'cmyk' );  % 把上面画的图(句柄为 fig )保存为 fig2.eps, 字号为 10,彩色。

运行以后就可以在当前目录看到一个名为 fig2.eps 的文件了。

这个 exportfig() 函数含有很多选项可以灵活设置,详看下面的说明:

%EXPORTFIG  Export a figure to Encapsulated Postscript.
%   EXPORTFIG(H, FILENAME) writes the figure H to FILENAME.  H is
%   a figure handle and FILENAME is a string that specifies the
%   name of the output file.
%
%   EXPORTFIG(...,PARAM1,VAL1,PARAM2,VAL2,...) specifies
%   parameters that control various characteristics of the output
%   file.
%
%   Format Paramter:
%     'Format'  one of the strings 'eps','eps2','jpeg','png','preview'
%          specifies the output format. Defaults to 'eps'.
%          The output format 'preview' does not generate an output
%          file but instead creates a new figure window with a
%          preview of the exported figure. In this case the
%          FILENAME parameter is ignored.
%
%     'Preview' one of the strings 'none', 'tiff'
%          specifies a preview for EPS files. Defaults to 'none'.
%
%   Size Parameters:
%     'Width'   a positive scalar
%          specifies the width in the figure's PaperUnits
%     'Height'  a positive scalar
%          specifies the height in the figure's PaperUnits
%
%     Specifying only one dimension sets the other dimension
%     so that the exported aspect ratio is the same as the
%     figure's current aspect ratio.
%     If neither dimension is specified the size defaults to
%     the width and height from the figure's PaperPosition.
%          
%   Rendering Parameters:
%     'Color'     one of the strings 'bw', 'gray', 'cmyk'
%         'bw'    specifies that lines and text are exported in
%                 black and all other objects in grayscale
%         'gray'  specifies that all objects are exported in grayscale
%         'cmyk'  specifies that all objects are exported in color
%                 using the CMYK color space
%     'Renderer'  one of the strings 'painters', 'zbuffer', 'opengl'
%         specifies the renderer to use
%     'Resolution'   a positive scalar
%         specifies the resolution in dots-per-inch.
%    
%     The default color setting is 'bw'.
%
%   Font Parameters:
%     'FontMode'     one of the strings 'scaled', 'fixed'
%     'FontSize'     a positive scalar
%          in 'scaled' mode multiplies with the font size of each
%          text object to obtain the exported font size
%          in 'fixed' mode specifies the font size of all text
%          objects in points
%     'FontEncoding' one of the strings 'latin1', 'adobe'
%          specifies the character encoding of the font
%
%     If FontMode is 'scaled' but FontSize is not specified then a
%     scaling factor is computed from the ratio of the size of the
%     exported figure to the size of the actual figure. The minimum
%     font size allowed after scaling is 5 points.
%     If FontMode is 'fixed' but FontSize is not specified then the
%     exported font sizes of all text objects is 7 points.
%
%     The default 'FontMode' setting is 'scaled'.
%
%   Line Width Parameters:
%     'LineMode'     one of the strings 'scaled', 'fixed'
%     'LineWidth'    a positive scalar
%          the semantics of LineMode and LineWidth are exactly the
%          same as FontMode and FontSize, except that they apply
%          to line widths instead of font sizes. The minumum line
%          width allowed after scaling is 0.5 points.
%          If LineMode is 'fixed' but LineWidth is not specified
%          then the exported line width of all line objects is 1
%          point.
%
%   Examples:
%     exportfig(gcf,'fig1.eps','height',3);
%       Exports the current figure to the file named 'fig1.eps' with
%       a height of 3 inches (assuming the figure's PaperUnits is
%       inches) and an aspect ratio the same as the figure's aspect
%       ratio on screen.
%
%     exportfig(gcf, 'fig2.eps', 'FontMode', 'fixed',...
%                'FontSize', 10, 'color', 'cmyk' );
%       Exports the current figure to 'fig2.eps' in color with all
%       text in 10 point fonts. The size of the exported figure is
%       the figure's PaperPostion width and height.

exportfig.m

特别声明,这个程序不是我编的,我只是用了感觉很好,与大家分享。



https://blog.sciencenet.cn/blog-267716-567996.html

上一篇:科研中的“拿来主义”要不得
下一篇:一篇令我感到为难的审稿(已更新)
收藏 IP: 166.111.93.*| 热度|

4 张鹏举 崔全顺 周素琴 dulizhi95

该博文允许注册用户评论 请点击登录 评论 (9 个评论)

数据加载中...
扫一扫,分享此博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-11-24 01:31

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部