Matlab plot colors and Styles

As we have already stated here, by writing help plot or doc plot in Matlab you will be able to find the information we are about to give you down below.

Matlab plotting colors


The following are the letters you can add to your code to control the color of your plot while plotting in Matlab.

  • b     blue
  • g     green
  • r     red
  • c     cyan
  • m     magenta
  • y     yellow
  • k     black
  • w     white

Let’s try some variants on the following example.

The default code to plot is:

x=-100:0.5:100;
y=x.^5-x.^2;
plot(x,y)

And the following will is the corresponding plot

plotting-in-matlab

Let’s twist the code a little to change the plot color

For the following code

x=-100:0.5:100;
y=x.^5-x.^2;
plot(x,y,'r')

The plot will look like

plotting-in-matlab

You must surely have grasped how to add the color code to get your graph to the wanted color, and notice at the beginning of this post the different color and code you can make use of while using this technique

Matlab plotting line style

Just like it is to change the color of your plot in Matlab, the same goes to changing the line style, increasing the thickness of the line or some other aspect of it

Let’s go ahead a plot the following code

 x=-100:0.5:100;
y=x.^5-x.^2;
plot(x,y,'--r')

And the plot will be

plotting-in-matlab

or

x=-100:0.5:100;
y=x.^5-x.^2;
plot(x,y,'vr')

and the plot will be

plotting-in-matlab

Here are the code you can use to change the line style. (You can get those information with help plot)

.     point
o     circle
x     x-mark
+     plus
*     star
s     square
d     diamond
v     triangle (down)
^     triangle (up)
<     triangle (left)
>     triangle (right)
p     pentagram
h     hexagram
—    dashed
-.    dashdot
:     dotted
–     solid

Here is how to change the thickness of the line of your plot in Matlab

The code

x=-100:0.5:100;
y=x.^5-x.^2;
plot(x,y,'m','LineWidth',2)

The plot

plotting-in-matlab

Here is another example which you can learn a lot from

The code

x = -pi:pi/10:pi;
y = tan(sin(x));
plot(x,y,'--rs','LineWidth',2,...
                'MarkerEdgeColor','k',...
                'MarkerFaceColor','g',...
                'MarkerSize',5)

The Graph

plotting-in-matlab

Plotting multiple graphs on the same plot

One of the many ways to plot multiple functions on the same plot, is to use hold on or insert the corresponding equations in the plot code.

Here is a simple example

The code

x = -pi:pi/10:pi;
y1 = tan(sin(x));
y2 = tan(cos(x));
plot(x,y1,'--r',x,y2,'b','LineWidth',2)

The Plot

plotting-in-matlab

This can also be done the following way

The code

x = -pi:pi/10:pi;
y1 = tan(sin(x));
y2 = tan(cos(x));
plot(x,y1,'--r','LineWidth',2)
hold on
plot(x,y2,'b','LineWidth',2)

Matlab subplot

Subplot helps have plots side by side on the same sheet. Here is what Matlab says about it. (Use Help Subplot)

subplot Create axes in tiled positions.
H = subplot(m,n,p), or subplot(mnp), breaks the Figure window into an m-by-n matrix of small axes, selects the p-th axes for the current plot, and returns the axes handle.  The axes are counted along the top row of the Figure window, then the second row, etc.  For example,

subplot(2,1,1), PLOT(income)
subplot(2,1,2), PLOT(outgo)

Use the following code to try it out

 x = -pi:pi/10:pi;
y1 = tan(sin(x));
y2 = tan(cos(x));
subplot(2,1,1)
plot(x,y1,'--r','LineWidth',2)
subplot(2,1,2)
plot(x,y2,'b','LineWidth',2)

The Plot

plotting-in-matlab

Plot legend and labels

You can learn more about this topic at matwork.com

Plotting

Here are the different type of plots in Matlab

tchouken2

Related Posts