Save plot or graphics content to file
exportgraphics(
saves the contents of the graphics object specified byobj
,filename
)obj
to a file. The graphics object can be any type of axes, a figure, a standalone visualization, a tiled chart layout, or a container within the figure. The resulting graphic is tightly cropped to a thin margin surrounding your content.
exportgraphics(
specifies additional options for saving the file. For example,obj
,filename
,Name,Value
)exportgraphics(gca,'myplot.jpg','Resolution',300)
saves the contents of the current axes as a 300-DPI image file.
Create a line plot and get the current axes. Then save the contents of the axes as a JPEG file.
plot(rand(5,5)) ax = gca; exportgraphics(ax,'LinePlot.jpg')
Display an image and get the current axes. Then save the contents of the axes as a 300-DPI JPEG file.
I = imread('peppers.png'); imshow(I) ax = gca; exportgraphics(ax,'Peppers300.jpg','Resolution',300)
Display a plot with an annotation that extends beyond the bounds of the axes. Save the contents of the figure as a PDF file.
plot(1:10) annotation('textarrow',[0.06 0.5],[0.73 0.5],'String','y = x ') f = gcf; exportgraphics(f,'AnnotatedPlot.pdf')
Display a bar chart and get the current axes. Then save the contents of the axes as a PDF containing only vector graphics.
bar([10 22 31 43]) ax = gca; exportgraphics(ax,'BarChart.pdf','ContentType','vector')
Display two plots in a tiled chart layout. Then save both plots as a PDF by passing theTiledChartLayout
object to theexportgraphics
function.
t = tiledlayout(2,1); nexttile plot([1 2 3]) nexttile plot([3 2 1]) exportgraphics(t,'Layout.pdf')
If you want to save just one of the plots in the layout, call thenexttile
函数与轴返回基于“增大化现实”技术gument. Then pass the axes to theexportgraphics
function.
Display a heatmap chart. Then save the chart as a PDF containing only vector graphics with a transparent background.
h = heatmap(rand(10,10)); exportgraphics(h,'Hmap.pdf','BackgroundColor','none','ContentType','vector')
Create a program file calledsaveapp.m
that displays a plot and a button for saving the axes content. In the callback function for the button, call theuiputfile
function to prompt the user for a file name and location. Then call theexportgraphics
function with the full path to the specified file.
functionsaveapp f = uifigure; ax = uiaxes(f,'Position',[25 25 400 375]); plot(ax,[0 0.3 0.1 0.6 0.4 1]) b = uibutton(f,'Position',[435 200 90 30],'Text','Save Plot'); b.ButtonPushedFcn = @buttoncallback;functionbuttoncallback(~,~) filter = {'*.jpg';'*.png';'*.tif';'*.pdf';'*.eps'}; [filename,filepath] = uiputfile(filter);ifischar(filename) exportgraphics(ax,[filepath filename]);endendend
Run the app by calling thesaveapp
function. When you click theSave Plotbutton in the app, a dialog box prompts you for a file name and location. Then the axes content is saved in the specified file. The area surrounding the axes, including the button, is not included in the file.
saveapp
obj
—Graphics objectGraphics object, specified as one of these objects:
Any type of axes: anAxes
,PolarAxes
, orGeographicAxes
object.
A standalone visualization such aheatmap
chart.
A tiled chart layout, which you create with thetiledlayout
function.
A container within a figure: aPanel
,Tab
, orButtonGroup
object.
exportgraphics
captures the contents of the object you specify. It does not capture UI components such as buttons or sliders.
It also does not capture adjacent containers or child containers. For example, consider a figure containing a line plot with an adjacent panel containing a heatmap:
f = figure; ax = axes(f,'Position',[0.1 0.1 0.4 0.8]); plot(ax,[0 1]) p = uipanel(f,'Position',[0.55 0.1 0.4 0.8]); heatmap(p,rand(10,5)) exportgraphics(f,'myfigure.png') exportgraphics(p,'mypanel.png')
When you run the preceding code,myfigure.png
contains the line plot, but not the heatmap. Similarly,mypanel.png
contains the heatmap, but not the line plot.
filename
—File nameFile name, specified as a character vector or a string scalar that includes the file extension. Iffilename
does not include a full path, MATLAB®saves the file in the current folder. You must have permission to write to the file.
以下table lists the supported file formats and the file extensions (which are not case sensitive).
File Format | File Extension |
---|---|
Joint Photographic Experts Group (JPEG) |
'jpg' or'jpeg' |
Portable Network Graphics (PNG) |
'png' |
Tagged Image File Format (TIFF) |
'tif' or'tiff' |
Portable Document Format (PDF) The PDF includes embeddable fonts when the |
'pdf' |
Enhanced Metafile for Windows®systems only (EMF) |
'emf' |
Encapsulated PostScript®(EPS) |
'eps' |
Example:exportgraphics(gca,'myfile.jpg')
saves the contents of the current axes to a JPEG file calledmyfile.jpg
.
Specify optional comma-separated pairs ofName,Value
参数。Name
is the argument name andValue
is the corresponding value.Name
must appear inside quotes. You can specify several name and value pair arguments in any order asName1,Value1,...,NameN,ValueN
.
exportgraphics(gca,'myplot.jpg','Resolution',300)
saves the contents of the current axes to a 300-DPI image file.
'ContentType'
—类型的内容'auto'
(default) |'vector'
|'image'
类型的内容to store when saving as an EMF, EPS, or PDF file. Specify the value as one of these options:
'auto'
— MATLAB controls whether the content is a vector graphic or an image.
'vector'
— Stores the content as a vector graphic that can scale to any size. If you are saving a PDF file, embeddable fonts are included in the file.
'image'
— Rasterizes the content into one or more images within the file.
Note
The'vector'
option is not supported for JPEG, TIFF, and PNG files.
'Resolution'
—Resolution (DPI)150
(default) |whole numberResolution in dots per inch (DPI), specified as a whole number that is greater than or equal to1
.
Specifying the resolution has no effect when theContentType
is'vector'
.
Data Types:single
|double
|int8
|int16
|int32
|int64
|uint8
|uint16
|uint32
|uint64
'BackgroundColor'
—Background color[1 1 1]
(default) |'current'
|'none'
|RGB triplet|'r'
|'g'
|'b'
| ...Background color, specified as'current'
,'none'
, an RGB triplet, a hexadecimal color code, or a color name. The background color controls the color of the margin that surrounds the axes or chart.
A value of'current'
sets the background color to the parent container's color.
A value of'none'
sets the background color to transparent or white, depending on the file format and the value ofContentType
:
Transparent — For files withContentType='vector'
White — For image files, or whenContentType='image'
WhenContentType='auto'
, MATLAB sets the background color according to the heuristic it uses to determine the type content to save.
Alternatively, specify a custom color or a named color.
RGB triplets and hexadecimal color codes are useful for specifying custom colors.
An RGB triplet is a three-element row vector whose elements specify the intensities of the red, green, and blue components of the color. The intensities must be in the range[0,1]
; for example,[0.4 0.6 0.7]
.
A hexadecimal color code is a character vector or a string scalar that starts with a hash symbol (#
)符合lowed by three or six hexadecimal digits, which can range from0
toF
. The values are not case sensitive. Thus, the color codes'#FF8800'
,'#ff8800'
,'#F80'
, and'#f80'
are equivalent.
Alternatively, you can specify some common colors by name. This table lists the named color options, the equivalent RGB triplets, and hexadecimal color codes.
Color Name | Short Name | RGB Triplet | Hexadecimal Color Code | Appearance |
---|---|---|---|---|
'red' |
'r' |
[1 0 0] |
'#FF0000' |
|
'green' |
'g' |
[0 1 0] |
'#00FF00' |
|
'blue' |
'b' |
[0 0 1] |
'#0000FF' |
|
'cyan' |
'c' |
[0 1 1] |
'#00FFFF' |
|
'magenta' |
'm' |
[1 0 1] |
'#FF00FF' |
|
'yellow' |
'y' |
[1 1 0] |
'#FFFF00' |
|
'black' |
'k' |
[0 0 0] |
'#000000' |
|
'white' |
'w' |
[1 1 1] |
'#FFFFFF' |
|
Here are the RGB triplets and hexadecimal color codes for the default colors MATLAB uses in many types of plots.
RGB Triplet | Hexadecimal Color Code | Appearance |
---|---|---|
[0 0.4470 0.7410] |
'#0072BD' |
|
[0.8500 0.3250 0.0980] |
'#D95319' |
|
[0.9290 0.6940 0.1250] |
'#EDB120' |
|
[0.4940 0.1840 0.5560] |
'#7E2F8E' |
|
[0.4660 0.6740 0.1880] |
'#77AC30' |
|
[0.3010 0.7450 0.9330] |
'#4DBEEE' |
|
[0.6350 0.0780 0.1840] |
'#A2142F' |
|
Hovering over theExportbuttonin the axes toolbar reveals a drop-down menu with options for exporting content:
: Save the content as a tightly cropped image or PDF.
:复制内容作为一个形象。
: Copy the content as a vector graphic.
A modified version of this example exists on your system. Do you want to open this version instead?
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select:.
Selectweb siteYou can also select a web site from the following list:
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.