# Plotting a Basic Figure

Supposing we have the following code:

`x=[1:10]';`
`y=2*x;`
`z=x.^2;`
`figure;`
`plot(x,y);`
`hold('on');`
`scatter(x,y,z);`
`hold('off');`
`figure;`

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:

`figure(1)`

# 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=gcf`

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

We can now assign the Axes to another variable using:

`MyAxes=MyFigure.Children`

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

We can look at this in more detail using:

`MyGraphics=MyAxes.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=MyGraphics(1)`

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 Marker: 'o' MarkerEdgeAlpha: 1 MarkerEdgeColor: 'flat' MarkerFaceAlpha: 1 MarkerFaceColor: 'none' Parent: [1×1 Axes] PickableParts: 'visible' Selected: 'off' SelectionHighlight: 'on' SizeData: [1×10 double] SizeDataSource: '' Tag: '' Type: 'scatter' UIContextMenu: [0×0 GraphicsPlaceholder] UserData: [] Visible: 'on' XData: [1×10 double] XDataSource: '' YData: [1×10 double] YDataSource: '' ZData: [1×0 double] ZDataSource: ''```

We can change these properties using dot indexing:

```MyScatter.XData=[2:2:20]; ``````MyScatter.YData=[4:4:40]; ``````MyScatter.SizeData=[40:-4:4]; ``````MyScatter.Marker='d'; ``````MyScatter.LineWidth=2; ``````MyScatter.MarkerEdgeColor=[0,1,1]; ``````MyScatter.MarkerEdgeAlpha=0.2; ``````MyScatter.MarkerFaceColor='m'; ````MyScatter.MarkerFaceAlpha=0.5;`

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' Color: [0 0.4470 0.7410] CreateFcn: '' DeleteFcn: '' DisplayName: '' HandleVisibility: 'on' HitTest: 'on' Interruptible: 'on' LineJoin: 'round' LineStyle: '-' LineWidth: 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' XData: [1 2 3 4 5 6 7 8 9 10] XDataMode: 'manual' XDataSource: '' YData: [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.Color='r'; MyLine.LineStyle=':'; MyLine.LineWidth=2; MyLine.XData=[2:2:20]; MyLine.YData=[4:4:40];```

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' Box: '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' Color: [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: '' FontAngle: 'normal' FontName: 'Helvetica' FontSize: 10 FontSizeMode: 'auto' FontSmoothing: 'on' FontUnits: 'points' FontWeight: 'normal' GridAlpha: 0.1500 GridAlphaMode: 'auto' GridColor: [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 LineWidth: 0.5000 MinorGridAlpha: 0.2500 MinorGridAlphaMode: 'auto' MinorGridColor: [0.1000 0.1000 0.1000] MinorGridColorMode: 'auto' MinorGridLineStyle: ':' 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' XColor: [0.1500 0.1500 0.1500] XColorMode: 'auto' XDir: 'normal' XGrid: 'off' XLabel: [1×1 Text] XLim: [2 20] XLimMode: 'auto' XMinorGrid: 'off' XMinorTick: 'off' XScale: '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] YAxisLocation: 'left' YColor: [0.1500 0.1500 0.1500] YColorMode: 'auto' YDir: 'normal' YGrid: 'off' YLabel: [1×1 Text] YLim: [0 40] YLimMode: 'auto' YMinorGrid: 'off' YMinorTick: 'off' YScale: '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.Color=[165/255,165/255,165/255];`

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

`MyAxes.Color=[255/255,255/255,255/255];`
`MyAxes.Box=0;`

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

```MyAxes.FontName='Calibri'; MyAxes.FontSize=20; MyAxes.FontAngle='italic';```

We can change the Axes Locations:

```MyAxes.XAxisLocation='top'; MyAxes.YAxisLocation='right';```

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

```MyAxes.XAxisLocation='origin'; MyAxes.YAxisLocation='origin'; MyAxes.XLim=[-30,30]; MyAxes.YLim=[-60,60];```

We can change the width and color of the Axes:

```MyAxes.LineWidth=2; MyAxes.XColor='m'; MyAxes.YColor='g';```

We can turn on the Major Grid:

```MyAxes.XGrid=1; MyAxes.YGrid=1;```

We can change the transparency of the grid:

`MyAxes.GridAlpha=1;`

We can also have a Minor Grid:

```MyAxes.XMinorGrid=1; MyAxes.YMinorGrid=1; MyAxes.MinorGridColor='c'; MyAxes.MinorGridLineStyle=':';```

For the Axes we have:

`MyAxes.XLabel`

We can change the String using:

```MyAxes.XLabel.String={'x'}; MyAxes.YLabel.String={'y'}; MyAxes.Title.String={'x vs y'};```

We can look at the Legend

`MyAxes.Legend`

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

```legend('A','B') MyAxes.Legend```

If we click all properties we can see

```AutoUpdate: 'on' BeingDeleted: 'off' Box: 'on' BusyAction: 'queue' ButtonDownFcn: @bdowncb Children: [0×0 GraphicsPlaceholder] Color: [1 1 1] CreateFcn: '' DeleteFcn: '' EdgeColor: [0.1500 0.1500 0.1500] FontAngle: 'italic' FontName: 'Calibri' FontSize: 18 FontWeight: 'normal' HandleVisibility: 'on' HitTest: 'on' Interpreter: 'tex' Interruptible: 'off' ItemHitFcn: @defaultItemHitCallback LineWidth: 2 Location: 'northeast' NumColumns: 1 NumColumnsMode: 'auto' Orientation: 'vertical' Parent: [1×1 Figure] PickableParts: 'visible' Position: [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.Legend.Location='SouthWest'; MyAxes.Legend.Box=1; MyAxes.Legend.Color=[1,1,0]; MyAxes.Legend.FontName='Arial'; MyAxes.Legend.FontAngle='normal'; MyAxes.Legend.FontSize=48; ```

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