Plotting with Style
One of MATLAB's features is being able to customize plots. But some of the customization may not need to be done by hand and can instead be controlled programmatically. Let me show you a set of these having to do with plotting lines.
Contents
Plotting by Adding Lines
If you make a plot by successively adding lines, all the lines, by default, have the same color and linestyle.
m3 = magic(3); plot(m3(:,1)); hold on plot(m3(:,2)) plot(m3(:,3)) hold off
As you can see, it's very hard (impossible) to tell which line is which.
Manually Control Color and Style
I can distinguish the lines by using different colors and linestyles. Here's one way to do it.
plot(m3(:,1),'r'); hold on plot(m3(:,2),':b') plot(m3(:,3),'g--') hold off
Vectorizing Line Drawing
I can instead vectorize the line drawing.
plot(m3)
What you see here is the MATLAB cycles through colors to distinguish the plots. How many colors and which ones? Does MATLAB ever cycle through the linestyles? You can find out answers to these questions in the documentation on LineStyle and Color as well as some answers in this article.
Default Line Properties
Let's see what properties lines in MATLAB have. First I'll create a line and then get the properties.
h = plot(m3(:,1)); get(h)
DisplayName: '' Annotation: [1x1 hg.Annotation] Color: [0 0 1] EraseMode: 'normal' LineStyle: '-' LineWidth: 0.5000 Marker: 'none' MarkerSize: 6 MarkerEdgeColor: 'auto' MarkerFaceColor: 'none' XData: [1 2 3] YData: [8 3 4] ZData: [1x0 double] BeingDeleted: 'off' ButtonDownFcn: [] Children: [0x1 double] Clipping: 'on' CreateFcn: [] DeleteFcn: [] BusyAction: 'queue' HandleVisibility: 'on' HitTest: 'on' Interruptible: 'on' Selected: 'off' SelectionHighlight: 'on' Tag: '' Type: 'line' UIContextMenu: [] UserData: [] Visible: 'on' Parent: 161.0015 XDataMode: 'auto' XDataSource: '' YDataSource: '' ZDataSource: ''
To see what the default properties are, I can use set instead of get.
set(h)
ans = DisplayName: {} Color: {} EraseMode: {4x1 cell} LineStyle: {5x1 cell} LineWidth: {} Marker: {14x1 cell} MarkerSize: {} MarkerEdgeColor: {2x1 cell} MarkerFaceColor: {2x1 cell} XData: {} YData: {} ZData: {} ButtonDownFcn: {} Children: {} Clipping: {2x1 cell} CreateFcn: {} DeleteFcn: {} BusyAction: {2x1 cell} HandleVisibility: {3x1 cell} HitTest: {2x1 cell} Interruptible: {2x1 cell} Selected: {2x1 cell} SelectionHighlight: {2x1 cell} Tag: {} UIContextMenu: {} UserData: {} Visible: {2x1 cell} Parent: {} XDataMode: {2x1 cell} XDataSource: {} YDataSource: {} ZDataSource: {}
Notice two particular properties, Color and LineStyle. get shows me the current values.
get(h,'Color')
ans = 0 0 1
set allows me to see the choices for that property.
set(h,'LineStyle')
[ {-} | -- | : | -. | none ]
How Do axes Fit in?
Each line has a color and a linestyle, and they each have defaults, so what determines the behavior when I plotted multiple lines and got different colors? That's related to a property of axes which we can see here.
get(gca)
ActivePositionProperty = outerposition ALim = [0 1] ALimMode = auto AmbientLightColor = [1 1 1] Box = on CameraPosition = [2 5.5 17.3205] CameraPositionMode = auto CameraTarget = [2 5.5 0] CameraTargetMode = auto CameraUpVector = [0 1 0] CameraUpVectorMode = auto CameraViewAngle = [6.60861] CameraViewAngleMode = auto CLim = [0 1] CLimMode = auto Color = [1 1 1] CurrentPoint = [ (2 by 3) double array] ColorOrder = [ (7 by 3) double array] DataAspectRatio = [1 2.5 1] DataAspectRatioMode = auto DrawMode = normal FontAngle = normal FontName = Helvetica FontSize = [10] FontUnits = points FontWeight = normal GridLineStyle = : Layer = bottom LineStyleOrder = - LineWidth = [0.5] MinorGridLineStyle = : NextPlot = replace OuterPosition = [0 0 1 1] PlotBoxAspectRatio = [1 1 1] PlotBoxAspectRatioMode = auto Projection = orthographic Position = [0.13 0.11 0.775 0.815] TickLength = [0.01 0.025] TickDir = in TickDirMode = auto TightInset = [0.0392857 0.0404762 0.00892857 0.0190476] Title = [166.002] Units = normalized View = [0 90] XColor = [0 0 0] XDir = normal XGrid = off XLabel = [163.002] XAxisLocation = bottom XLim = [1 3] XLimMode = auto XMinorGrid = off XMinorTick = off XScale = linear XTick = [ (1 by 11) double array] XTickLabel = [ (11 by 3) char array] XTickLabelMode = auto XTickMode = auto YColor = [0 0 0] YDir = normal YGrid = off YLabel = [164.002] YAxisLocation = left YLim = [3 8] YLimMode = auto YMinorGrid = off YMinorTick = off YScale = linear YTick = [ (1 by 11) double array] YTickLabel = [ (11 by 3) char array] YTickLabelMode = auto YTickMode = auto ZColor = [0 0 0] ZDir = normal ZGrid = off ZLabel = [165.002] ZLim = [-1 1] ZLimMode = auto ZMinorGrid = off ZMinorTick = off ZScale = linear ZTick = [-1 0 1] ZTickLabel = ZTickLabelMode = auto ZTickMode = auto BeingDeleted = off ButtonDownFcn = Children = [162.004] Clipping = on CreateFcn = DeleteFcn = BusyAction = queue HandleVisibility = on HitTest = on Interruptible = on Parent = [1] Selected = off SelectionHighlight = on Tag = Type = axes UIContextMenu = [] UserData = [] Visible = on
Well, hmmmm -- so many properties. Let me point out the two that are the ones to focus on for now.
get(gca,'ColorOrder')
ans = 0 0 1.0000 0 0.5000 0 1.0000 0 0 0 0.7500 0.7500 0.7500 0 0.7500 0.7500 0.7500 0 0.2500 0.2500 0.2500
get(gca,'LineStyleOrder')
ans = -
Note: using gca is really only for debugging or illustration. Normally a handle should be gotten from the chosen axes and that handle specifically should be used.
axes Properties ColorOrder and LineStyleOrder
There are two axes properties that you can set to help you make the lines in your plots follow a pattern of color and linestyle. To see how these work, let's set the defaults for these properties at the root of the handle graphics hierarchy and experiment.
set(0,'DefaultAxesLineStyleOrder',{'--',':'}) set(0,'DefaultAxesColorOrder',[1 0 1; 0 1 1; 0 1 0]) yvals = [1:10;1:10] plot(yvals) axis([1 2 0 size(yvals,2)+1 ])
yvals = 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10
Now you see that MATLAB first cycles over the colors using the first linestyle, then again cycles over the colors with the second linestyle, etc., eventually cycling over the linestyles again as well.
Do You Override the Defaults?
Do you override the line plotting defaults? Do you do this manually or by setting the defaults as I have shown here? Let me know here how you avoid hand-updating your plots.
- Category:
- Graphics,
- Less Used Functionality