10.7: Changing Graphics Properties
- Page ID
- 136716
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\dsum}{\displaystyle\sum\limits} \)
\( \newcommand{\dint}{\displaystyle\int\limits} \)
\( \newcommand{\dlim}{\displaystyle\lim\limits} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\(\newcommand{\longvect}{\overrightarrow}\)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)When MATLAB creates a plot, it creates graphics objects. Examples of graphics objects include the figure window, axes, plotted lines, bars, markers, labels, and text. Each object has properties that control how it looks and behaves.
For example, a plotted line has properties such as color, line width, marker type, and marker size. A bar chart has properties such as face color, edge color, and bar width. MATLAB lets us change these properties so that our plots are easier to read and better suited for reports or presentations.
Using Name-Value Pairs
Many plotting functions allow you to set properties directly using name-value pairs. A name-value pair has two parts: the name of the property and the value assigned to that property.
y = [75 91 105 123.5 131 150 179 203 226 249 281.5]; bar(y, 'FaceColor', 'green', 'EdgeColor', 'black', 'LineWidth', 1.5)
Solution

Here FaceColor controls the color inside the bars, EdgeColor controls the color of the bar outline, and LineWidth controls the thickness of the outline.
Name-value pairs are useful because they let us customize the plot at the same time we create it. Many MATLAB plotting functions support this style of customization.
Common Graphics Properties
The exact list of properties depends on the type of object, but some properties are very common.
|
Property |
Common use |
|
Color |
Changes the color of a line or marker. |
|
LineWidth |
Changes the thickness of a line. |
|
Marker |
Chooses the marker symbol for data points. |
|
MarkerSize |
Changes the size of markers. |
|
FaceColor |
Changes the filled color of bars or filled objects. |
|
EdgeColor |
Changes the outline color of bars or filled objects. |
|
FontSize |
Changes the size of text on axes, labels, or titles. |
In the following example we draw a red line with circular markers and a thicker line width.
x = 1:5; y = [2 5 6 8 11]; plot(x, y, 'o-', 'Color', 'red', 'LineWidth', 2, 'MarkerSize', 8)
Solution

Using RGB Color Values
Colors can also be specified using RGB values. RGB stands for red, green, and blue. In MATLAB, an RGB color is written as a three-element vector, where each value is between 0 and 1.
|
RGB value |
Color |
|
[1 0 0] |
red |
|
[0 1 0] |
green |
|
[0 0 1] |
blue |
|
[0 0 0] |
black |
|
[1 1 1] |
white |
|
[0.5 0.5 0.5] |
gray |
Selecting the line color using the RGB color values
plot(x, y, 'Color', [0.2 0.4 0.8], 'LineWidth', 2)
Solution

This gives you more control than the one-letter color shortcuts such as r, g, b, and k.
Using Object Handles
A handle is a reference to a graphics object. If we store the handle returned by a plotting function, we can later use it to inspect or change the object.
y = [75 91 105 123.5 131 150 179 203 226 249 281.5];
h = bar(y, 'FaceColor', 'green', 'EdgeColor', 'black', 'LineWidth', 1.5);
get(h) % display properties
h.FaceColor = [1 0 0]; % change the bar color to red
Solution

The command get(h) displays the properties of the object associated with handle h. The line h.FaceColor = [1 0 0] changes the bar color after the plot has already been created.
Object handles are especially helpful when you want to modify a plot after creating it, or when you want to store references to several plotted objects.
Good habit: Use simple formatting choices that make the plot clearer. Avoid changing properties just to make the graph look busy.

