Post-Modification of a Figure

Tutorial Video

Plotting a Basic Figure

Supposing we have the following code:

x=<span style="color: #ff0000;">[</span>1<span style="color: #ff00ff;">:</span>10<span style="color: #ff0000;">]</span><span style="color: #ff00ff;">'</span><span style="color: #ff00ff;">;</span>
y=2*x<span style="color: #ff00ff;">;</span>
z=x<span style="color: #ff0000;">.</span>^2<span style="color: #ff00ff;">;</span>
<span style="color: #0000ff;">figure</span><span style="color: #ff00ff;">;</span>
<span style="color: #0000ff;">plot(</span>x,y<span style="color: #0000ff;">)<span style="color: #ff00ff;">;</span></span>
<span style="color: #0000ff;">hold</span><span style="color: #0000ff;">(</span><span style="color: #800080;">'</span>on<span style="color: #800080;">'</span><span style="color: #0000ff;">)</span><span style="color: #ff00ff;">;</span>
<span style="color: #0000ff;">scatter(</span>x,y,z<span style="color: #0000ff;">)<span style="color: #ff00ff;">;</span></span>
<span style="color: #0000ff;">hold(</span><span style="color: #800080;">'</span>off<span style="color: #800080;">'</span><span style="color: #0000ff;">)</span><span style="color: #ff00ff;">;</span>
<span style="color: #0000ff;">figure</span><span style="color: #ff00ff;">;</span>

We generate the following bland looking figures below:

Selecting the Figure we Wish to Modify:

Now supposing we want to go back to figure 1 and modify the existing line and scatter plot. First we will need to select figure 1. To do this we can either select figure 1 with the mouse or type:

<span style="color: #0000ff;">figure(</span>1<span style="color: #0000ff;">)</span>

Assigning the Figure, Axes and Plots to Variable Names

Now we can assign the selected figure to a variable using get current figure gcf

MyFigure=<span style="color: #0000ff;">gcf</span>

If we expand all properties we can see Children: [1×1 Axes]

We can now assign the Axes to another variable using:

MyAxes=MyFigure<span style="color: #ff0000;">.</span>Children

If we expand all properties we can see Children: [2×1 Graphics]

We can look at this in more detail using:

MyGraphics=MyAxes<span style="color: #ff0000;">.</span>Children

We can see the first value in the MyGraphics Array is the Scatter Plot and the second value is the Line Plot.

We can assign the Scatter Plot to a variable:

MyScatter=<span style="color: #0000ff;">MyGraphics(</span>1<span style="color: #0000ff;">)</span>

If we expand all properties we can see

Annotation: [1×1 matlab.graphics.eventdata.Annotation]
BeingDeleted: 'off'
BusyAction: 'queue'
ButtonDownFcn: ''
CData: [1×3 double]
CDataSource: ''
Children: [0×0 GraphicsPlaceholder]
Clipping: 'on'
CreateFcn: ''
DeleteFcn: ''
DisplayName: ''
HandleVisibility: 'on'
HitTest: 'on'
Interruptible: 'on'
LineWidth: 0.5000
<span style="color: #ff0000;">Marker</span>: 'o'
<span style="color: #ff0000;">MarkerEdgeAlpha</span>: 1
<span style="color: #ff0000;">MarkerEdgeColor</span>: 'flat'
<span style="color: #ff0000;">MarkerFaceAlpha</span>: 1
<span style="color: #ff0000;">MarkerFaceColor</span>: 'none'
Parent: [1×1 Axes]
PickableParts: 'visible'
Selected: 'off'
SelectionHighlight: 'on'
<span style="color: #ff0000;">SizeData</span>: [1×10 double]
SizeDataSource: ''
Tag: ''
Type: 'scatter'
UIContextMenu: [0×0 GraphicsPlaceholder]
UserData: []
Visible: 'on'
<span style="color: #ff0000;">XData</span>: [1×10 double]
XDataSource: ''
<span style="color: #ff0000;">YData</span>: [1×10 double]
YDataSource: ''
ZData: [1×0 double]
ZDataSource: ''

We can change these properties using dot indexing:

MyScatter<span style="color: #ff0000;">.</span>XData=<span style="color: #ff0000;">[</span>2<span style="color: #ff0000;">:</span>2<span style="color: #ff0000;">:</span>20<span style="color: #ff0000;">]</span><span style="color: #ff00ff;">;
</span>
MyScatter<span style="color: #ff0000;">.</span>YData=<span style="color: #ff0000;">[</span>4<span style="color: #ff0000;">:</span>4<span style="color: #ff0000;">:</span>40<span style="color: #ff0000;">]</span><span style="color: #ff00ff;">;
</span>
MyScatter<span style="color: #ff0000;">.</span>SizeData=<span style="color: #ff0000;">[</span>40<span style="color: #ff0000;">:</span>-4<span style="color: #ff0000;">:</span>4<span style="color: #ff0000;">]</span><span style="color: #ff00ff;">;
</span>
MyScatter<span style="color: #ff0000;">.</span>Marker=<span style="color: #800080;">'</span><span style="color: #ff9900;">d</span><span style="color: #800080;">'</span><span style="color: #ff00ff;">;
</span>
MyScatter<span style="color: #ff0000;">.</span>LineWidth=2<span style="color: #ff00ff;">;
</span>
MyScatter<span style="color: #ff0000;">.</span>MarkerEdgeColor=<span style="color: #ff0000;">[</span>0<span style="color: #808080;">,</span>1<span style="color: #808080;">,</span>1<span style="color: #ff0000;">]<span style="color: #ff00ff;">;
</span></span>
MyScatter<span style="color: #ff0000;">.</span>MarkerEdgeAlpha=0.2<span style="color: #ff00ff;">;
</span>
MyScatter<span style="color: #ff0000;">.</span>MarkerFaceColor=<span style="color: #800080;">'</span><span style="color: #ff9900;">m</span><span style="color: #800080;">'</span><span style="color: #ff00ff;">;
</span>
MyScatter<span style="color: #ff0000;">.</span>MarkerFaceAlpha=0.5<span style="color: #ff00ff;">;</span>

We can now assign the Line Plot to a variable:

MyLine=MyGraphics(2)

If we expand all properties we can see

AlignVertexCenters: 'off'
Annotation: [1×1 matlab.graphics.eventdata.Annotation]
BeingDeleted: 'off'
BusyAction: 'queue'
ButtonDownFcn: ''
Children: [0×0 GraphicsPlaceholder]
Clipping: 'on'
<span style="color: #ff0000;">Color</span>: [0 0.4470 0.7410]
CreateFcn: ''
DeleteFcn: ''
DisplayName: ''
HandleVisibility: 'on'
HitTest: 'on'
Interruptible: 'on'
LineJoin: 'round'
<span style="color: #ff0000;">LineStyle</span>: '-'
<span style="color: #ff0000;">LineWidth</span>: 0.5000
Marker: 'none'
MarkerEdgeColor: 'auto'
MarkerFaceColor: 'none'
MarkerIndices: [1 2 3 4 5 6 7 8 9 10]
MarkerSize: 6
Parent: [1×1 Axes]
PickableParts: 'visible'
Selected: 'off'
SelectionHighlight: 'on'
Tag: ''
Type: 'line'
UIContextMenu: [0×0 GraphicsPlaceholder]
UserData: []
Visible: 'on'
<span style="color: #ff0000;">XData</span>: [1 2 3 4 5 6 7 8 9 10]
XDataMode: 'manual'
XDataSource: ''
<span style="color: #ff0000;">YData</span>: [2 4 6 8 10 12 14 16 18 20]
YDataSource: ''
ZData: [1×0 double]
ZDataSource: ''

Modification Using Dot Indexing

We can change these properties using dot indexing:

MyLine<span style="color: #ff0000;">.</span>Color=<span style="color: #800080;">'</span><span style="color: #ff9900;">r</span><span style="color: #800080;">'</span><span style="color: #ff00ff;">;</span>
MyLine<span style="color: #ff0000;">.</span>LineStyle=<span style="color: #800080;">'</span><span style="color: #ff9900;">:</span><span style="color: #800080;">'</span><span style="color: #ff00ff;">;</span>
MyLine<span style="color: #ff0000;">.</span>LineWidth=2<span style="color: #ff00ff;">;</span>
MyLine<span style="color: #ff0000;">.</span>XData=<span style="color: #ff0000;">[</span>2<span style="color: #ff0000;">:</span>2<span style="color: #ff0000;">:</span>20<span style="color: #ff0000;">]</span><span style="color: #ff00ff;">;</span>
MyLine<span style="color: #ff0000;">.</span>YData=<span style="color: #ff0000;">[</span>4<span style="color: #ff0000;">:</span>4<span style="color: #ff0000;">:</span>40<span style="color: #ff0000;">]<span style="color: #ff00ff;">;</span></span>

We can now look at the full properties of MyAxes.

MyAxes

If we expand these properties we can see:

ALim: [0 1]
ALimMode: 'auto'
ActivePositionProperty: 'outerposition'
AlphaScale: 'linear'
Alphamap: [1×64 double]
AmbientLightColor: [1 1 1]
BeingDeleted: 'off'
<span style="color: #ff0000;">Box</span>: 'on'
BoxStyle: 'back'
BusyAction: 'queue'
ButtonDownFcn: ''
CLim: [0 1]
CLimMode: 'auto'
CameraPosition: [11 20 17.3205]
CameraPositionMode: 'auto'
CameraTarget: [11 20 0]
CameraTargetMode: 'auto'
CameraUpVector: [0 1 0]
CameraUpVectorMode: 'auto'
CameraViewAngle: 6.6086
CameraViewAngleMode: 'auto'
Children: [2×1 Graphics]
Clipping: 'on'
ClippingStyle: '3dbox'
<span style="color: #ff0000;">Color</span>: [1 1 1]
ColorOrder: [7×3 double]
ColorOrderIndex: 3
ColorScale: 'linear'
Colormap: [64×3 double]
CreateFcn: ''
CurrentPoint: [2×3 double]
DataAspectRatio: [9 20 1]
DataAspectRatioMode: 'auto'
DeleteFcn: ''
<span style="color: #ff0000;">FontAngle</span>: 'normal'
<span style="color: #ff0000;">FontName</span>: 'Helvetica'
<span style="color: #ff0000;">FontSize</span>: 10
FontSizeMode: 'auto'
FontSmoothing: 'on'
FontUnits: 'points'
FontWeight: 'normal'
<span style="color: #000000;">GridAlpha:</span> 0.1500
GridAlphaMode: 'auto'
<span style="color: #000000;">GridColor</span>: [0.1500 0.1500 0.1500]
GridColorMode: 'auto'
GridLineStyle: '-'
HandleVisibility: 'on'
HitTest: 'on'
Interruptible: 'on'
LabelFontSizeMultiplier: 1.1000
Layer: 'bottom'
Legend: [0×0 GraphicsPlaceholder]
LineStyleOrder: '-'
LineStyleOrderIndex: 1
<span style="color: #ff0000;">LineWidth</span>: 0.5000
MinorGridAlpha: 0.2500
MinorGridAlphaMode: 'auto'
<span style="color: #ff0000;">MinorGridColor</span>: [0.1000 0.1000 0.1000]
MinorGridColorMode: 'auto'
<span style="color: #ff0000;">MinorGridLineStyle</span>: ':'
NextPlot: 'replace'
OuterPosition: [0 0 1 1]
Parent: [1×1 Figure]
PickableParts: 'visible'
PlotBoxAspectRatio: [1 0.7896 0.7896]
PlotBoxAspectRatioMode: 'auto'
Position: [0.1300 0.1100 0.7750 0.8150]
Projection: 'orthographic'
Selected: 'off'
SelectionHighlight: 'on'
SortMethod: 'childorder'
Tag: ''
TickDir: 'in'
TickDirMode: 'auto'
TickLabelInterpreter: 'tex'
TickLength: [0.0100 0.0250]
TightInset: [0.0369 0.0540 0.0137 0.0206]
Title: [1×1 Text]
TitleFontSizeMultiplier: 1.1000
TitleFontWeight: 'bold'
Toolbar: [1×1 AxesToolbar]
Type: 'axes'
UIContextMenu: [0×0 GraphicsPlaceholder]
Units: 'normalized'
UserData: []
View: [0 90]
Visible: 'on'
XAxis: [1×1 NumericRuler]
XAxisLocation: 'bottom'
<span style="color: #ff0000;">XColor</span>: [0.1500 0.1500 0.1500]
XColorMode: 'auto'
XDir: 'normal'
<span style="color: #ff0000;">XGrid</span>: 'off'
XLabel: [1×1 Text]
<span style="color: #ff0000;">XLim</span>: [2 20]
XLimMode: 'auto'
<span style="color: #ff0000;">XMinorGrid</span>: 'off'
XMinorTick: 'off'
<span style="color: #ff0000;">XScale</span>: 'linear'
XTick: [2 4 6 8 10 12 14 16 18 20]
XTickLabel: {10×1 cell}
XTickLabelMode: 'auto'
XTickLabelRotation: 0
XTickMode: 'auto'
YAxis: [1×1 NumericRuler]
<span style="color: #ff0000;">YAxisLocation</span>: 'left'
YColor: [0.1500 0.1500 0.1500]
YColorMode: 'auto'
YDir: 'normal'
YGrid: 'off'
YLabel: [1×1 Text]
<span style="color: #ff0000;">YLim</span>: [0 40]
YLimMode: 'auto'
<span style="color: #ff0000;">YMinorGrid</span>: 'off'
<span style="color: #000000;">YMinorTick</span>: 'off'
<span style="color: #ff0000;">YScale</span>: 'linear'
YTick: [0 5 10 15 20 25 30 35 40]
YTickLabel: {9×1 cell}
YTickLabelMode: 'auto'
YTickLabelRotation: 0
YTickMode: 'auto'
ZAxis: [1×1 NumericRuler]
ZColor: [0.1500 0.1500 0.1500]
ZColorMode: 'auto'
ZDir: 'normal'
ZGrid: 'off'
ZLabel: [1×1 Text]
ZLim: [-1 1]
ZLimMode: 'auto'
ZMinorGrid: 'off'
ZMinorTick: 'off'
ZScale: 'linear'
ZTick: [-1 0 1]
ZTickLabel: ''
ZTickLabelMode: 'auto'
ZTickLabelRotation: 0
ZTickMode: 'auto'

These settings can also be adjusted:

MyAxes<span style="color: #ff0000;">.</span>Color=[<span style="color: #ff0000;">165/255</span>,<span style="color: #339966;">165/255</span>,<span style="color: #0000ff;">165/255</span>]<span style="color: #ff00ff;">;</span>

If we change the color back to white and remove the box

MyAxes<span style="color: #ff0000;">.</span>Color=[<span style="color: #ff0000;">255/255</span>,<span style="color: #339966;">255/255</span>,<span style="color: #0000ff;">255/255</span>]<span style="color: #ff00ff;">;</span>
MyAxes<span style="color: #ff0000;">.</span>Box=0<span style="color: #ff00ff;">;</span>

We can change the font, the font size and make it italic:

MyAxes<span style="color: #ff0000;">.</span>FontName=<span style="color: #800080;">'<span style="color: #ff9900;">Calibri</span>'</span><span style="color: #ff00ff;">;</span>
MyAxes<span style="color: #ff0000;">.</span>FontSize=20<span style="color: #ff00ff;">;</span>
MyAxes<span style="color: #ff0000;">.</span>FontAngle=<span style="color: #800080;">'</span><span style="color: #ff9900;">italic</span><span style="color: #800080;">'</span><span style="color: #ff00ff;">;</span>

We can change the Axes Locations:

MyAxes<span style="color: #ff0000;">.</span>XAxisLocation=<span style="color: #800080;">'</span><span style="color: #ff9900;">top</span><span style="color: #800080;">'</span><span style="color: #ff00ff;">;</span>
MyAxes<span style="color: #ff0000;">.</span>YAxisLocation=<span style="color: #800080;">'</span><span style="color: #ff9900;">right</span><span style="color: #800080;">'</span><span style="color: #ff00ff;">;</span>

We can change the Axes Limits to show a negative value and set the Axes Origins to be at the origin:

MyAxes<span style="color: #ff0000;">.</span>XAxisLocation=<span style="color: #800080;">'<span style="color: #ff9900;">origin</span>'</span><span style="color: #ff00ff;">;</span>
MyAxes<span style="color: #ff0000;">.</span>YAxisLocation=<span style="color: #800080;">'</span><span style="color: #ff9900;">origin</span><span style="color: #800080;">'</span><span style="color: #ff00ff;">;</span>
MyAxes<span style="color: #ff0000;">.</span>XLim=[-30,30]<span style="color: #ff00ff;">;</span>
MyAxes<span style="color: #ff0000;">.</span>YLim=[-60,60]<span style="color: #ff00ff;">;</span>

We can change the width and color of the Axes:

MyAxes<span style="color: #ff0000;">.</span>LineWidth=2<span style="color: #ff00ff;">;</span>
MyAxes<span style="color: #ff0000;">.</span>XColor=<span style="color: #800080;">'</span><span style="color: #ff9900;">m</span><span style="color: #800080;">'<span style="color: #ff00ff;">;</span></span>
MyAxes<span style="color: #ff0000;">.</span>YColor=<span style="color: #800080;">'</span><span style="color: #ff9900;">g</span><span style="color: #800080;">'<span style="color: #ff00ff;">;</span></span>

We can turn on the Major Grid:

MyAxes<span style="color: #ff0000;">.</span>XGrid=1<span style="color: #ff00ff;">;</span>
MyAxes<span style="color: #ff0000;">.</span>YGrid=1<span style="color: #ff00ff;">;</span>

We can change the transparency of the grid:

MyAxes<span style="color: #ff0000;">.</span>GridAlpha=1<span style="color: #ff00ff;">;</span>

We can also have a Minor Grid:

MyAxes<span style="color: #ff0000;">.</span>XMinorGrid=1<span style="color: #ff00ff;">;</span>
MyAxes<span style="color: #ff0000;">.</span>YMinorGrid=1<span style="color: #ff00ff;">;</span>
MyAxes<span style="color: #ff0000;">.</span>MinorGridColor=<span style="color: #800080;">'</span><span style="color: #ff9900;">c</span><span style="color: #800080;">'</span><span style="color: #ff00ff;">;</span>
MyAxes<span style="color: #ff0000;">.</span>MinorGridLineStyle=<span style="color: #800080;">'</span><span style="color: #ff9900;">:</span><span style="color: #800080;">'</span><span style="color: #ff00ff;">;</span>

For the Axes we have:

MyAxes<span style="color: #ff0000;">.</span>XLabel

We can change the String using:

MyAxes<span style="color: #ff0000;">.</span>XLabel<span style="color: #ff0000;">.</span>String=<span style="color: #666699;">{</span><span style="color: #800080;">'</span><span style="color: #ff9900;">x</span><span style="color: #800080;">'</span><span style="color: #666699;">}</span><span style="color: #ff00ff;">;</span>
MyAxes<span style="color: #ff0000;">.</span>YLabel<span style="color: #ff0000;">.</span>String=<span style="color: #666699;">{</span><span style="color: #800080;">'</span><span style="color: #ff9900;">y</span><span style="color: #800080;">'</span><span style="color: #666699;">}</span><span style="color: #ff00ff;">;</span>
MyAxes<span style="color: #ff0000;">.</span>Title<span style="color: #ff0000;">.</span>String=<span style="color: #666699;">{</span><span style="color: #800080;">'</span><span style="color: #ff9900;">x vs y</span><span style="color: #800080;">'</span><span style="color: #666699;">}</span><span style="color: #ff00ff;">;</span>

We can look at the Legend

MyAxes<span style="color: #ff0000;">.</span>Legend

Okay it says Graphic place holder. We can however type in:

<span style="color: #0000ff;">legend(</span><span style="color: #800080;">'</span><span style="color: #ff9900;">A</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">B</span><span style="color: #800080;">'</span><span style="color: #0000ff;">)</span>
MyAxes<span style="color: #ff0000;">.</span>Legend

If we click all properties we can see

AutoUpdate: 'on'
BeingDeleted: 'off'
<span style="color: #ff0000;">Box</span>: 'on'
BusyAction: 'queue'
ButtonDownFcn: @bdowncb
Children: [0×0 GraphicsPlaceholder]
<span style="color: #ff0000;">Color</span>: [1 1 1]
CreateFcn: ''
DeleteFcn: ''
EdgeColor: [0.1500 0.1500 0.1500]
<span style="color: #ff0000;">FontAngle</span>: 'italic'
<span style="color: #ff0000;">FontName</span>: 'Calibri'
<span style="color: #ff0000;">FontSize</span>: 18
FontWeight: 'normal'
HandleVisibility: 'on'
HitTest: 'on'
Interpreter: 'tex'
Interruptible: 'off'
ItemHitFcn: @defaultItemHitCallback
LineWidth: 2
<span style="color: #ff0000;">Location</span>: 'northeast'
NumColumns: 1
NumColumnsMode: 'auto'
Orientation: 'vertical'
Parent: [1×1 Figure]
PickableParts: 'visible'
<span style="color: #ff0000;">Position</span>: [0.7696 0.7163 0.1161 0.1536]
Selected: 'off'
SelectionHighlight: 'on'
String: {'A' 'B'}
Tag: 'legend'
TextColor: [0 0 0]
Title: [1×1 Text]
Type: 'legend'
UIContextMenu: [1×1 ContextMenu]
Units: 'normalized'
UserData: []
Visible: 'on'

This shows a legend in a very inconvenient place:

Now that we have the Legend we can use Dot Indexing to change it's properties:

MyAxes<span style="color: #ff0000;">.</span>Legend<span style="color: #ff0000;">.</span>Location=<span style="color: #800080;">'</span><span style="color: #ff9900;">SouthWest</span><span style="color: #800080;">'</span><span style="color: #ff00ff;">;</span>
MyAxes<span style="color: #ff0000;">.</span>Legend<span style="color: #ff0000;">.</span>Box=1<span style="color: #ff00ff;">;</span>
MyAxes<span style="color: #ff0000;">.</span>Legend<span style="color: #ff0000;">.</span>Color=[<span style="color: #ff0000;">1</span>,<span style="color: #008000;">1</span>,<span style="color: #0000ff;">0</span>]<span style="color: #ff00ff;">;</span>
MyAxes<span style="color: #ff0000;">.</span>Legend<span style="color: #ff0000;">.</span>FontName=<span style="color: #800080;">'</span><span style="color: #ff9900;">Arial</span><span style="color: #800080;">'</span><span style="color: #ff00ff;">;</span>
MyAxes<span style="color: #ff0000;">.</span>Legend<span style="color: #ff0000;">.</span>FontAngle=<span style="color: #800080;">'</span><span style="color: #ff9900;">normal</span><span style="color: #800080;">'</span><span style="color: #ff00ff;">;</span>
MyAxes<span style="color: #ff0000;">.</span>Legend<span style="color: #ff0000;">.</span>FontSize=48<span style="color: #ff00ff;">;</span>

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.