Home      |       Contents       |       About

Prev: Basic plotting with 'plot'       |       Next: Common plots

Customizing the plot

  • Here we learn how to set some other important plot properties to further customize the plot, such as:
    1. Setting axes limits
    2. Graph title and axes labels
    3. Graph legend
    4. Moving axes
    5. Annotating
  • Setting axes limits
    • use xlim() and ylim() functions
    • In the example below the y axis limits are set to [-1.2, 1.2] and x axis limits to [0,10]
In [1]:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

t = np.linspace(0, 2*np.pi, 100)
fsin = np.sin(t)

# Setting axes limits
plt.ylim(-1.2, 1.2)
plt.xlim(0, 10)

plt.plot(t,fsin)
Out[1]:
[<matplotlib.lines.Line2D at 0x76919e8>]
  • *Setting the graph title and axes labels
    • Set the title and xlabel, ylabel properties respectively
In [2]:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

t = np.linspace(0, 2*np.pi, 100)
fsin = np.sin(t)

# Defining axes limits
plt.ylim(-1.2, 1.2)
plt.xlim(0, 10)

# Setting the graph title and axes labels 
plt.xlabel('t values')
plt.ylabel('fsin values')
plt.title('Sine graph')
plt.plot(t,fsin)
Out[2]:
[<matplotlib.lines.Line2D at 0x792e550>]
  • Adding graph legend

    • Set the label property when calling 'plot'
    • Finally call legend and set the loc property to define where the legend should appear
  • Read more about the location codes of 'legend' here

In [3]:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

t = np.linspace(0, 2*np.pi, 100)
fsin = np.sin(t)
fcos = np.cos(t)

# Defining axes limits
plt.ylim(-1.2, 1.2)
plt.xlim(0, 10)

# Setting the graph title and axes labels
plt.xlabel('t values')
plt.ylabel('fsin-fcos values')
plt.title('Sine - Cosine graph')
plt.plot(t,fsin)

# Adding graph legend
plt.plot(t, fsin, 'b-', label='sine')
plt.plot(t, fcos, 'r--', label='cosine')
plt.legend(loc='lower right')
Out[3]:
<matplotlib.legend.Legend at 0x799e898>
  • Moving the axes
    • First, call gca() as in ax = plt.gca() to get the axes object of the active plot
    • Then call 'spines[].set_position()' to set the position of the specific axis
    • Call 'spines[].set_color()' to set the axis color ('None' for making the axis invisible)
In [4]:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

t = np.linspace(0, 2*np.pi, 100)
fsin = np.sin(t)
fcos = np.cos(t)

# Defining axes limits
plt.ylim(-1.2, 1.2)
plt.xlim(0, 10)

# Setting the graph title and axes labels
plt.xlabel('t values')
plt.ylabel('fsin-fcos values')
plt.title('Sine - Cosine graph')
plt.plot(t,fsin)

# Adding graph legend
plt.plot(t, fsin, 'b-', label='sine')
plt.plot(t, fcos, 'r--', label='cosine')
plt.legend(loc='lower right')

# Moving the axes
ax = plt.gca()
ax.spines['bottom'].set_position('center')
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
  • Annotating the graph
    • Use the annotate function to set properties relevant to annotating the plot
    • To understand how to include simple annotations in your plot see the example below
    • For more on plot annotation see:
In [5]:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

t = np.linspace(0, 2*np.pi, 100)
fsin = np.sin(t)
fcos = np.cos(t)

# Defining axes limits
plt.ylim(-1.2, 1.2)
plt.xlim(0, 10)

# Setting the graph title and axes labels
plt.xlabel('t values')
plt.ylabel('fsin-fcos values')
plt.title('Sine - Cosine graph')
plt.plot(t,fsin)

# Adding graph legend
plt.plot(t, fsin, 'b-', label='sine')
plt.plot(t, fcos, 'r--', label='cosine')
plt.legend(loc='lower right')

# Moving the axes
ax = plt.gca()
ax.spines['bottom'].set_position('center')
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')

# Annotating
ax.annotate('sine', xy=(3, 0.25), xytext=(4, 0.5),
            arrowprops=dict(arrowstyle='->'))
Out[5]:
<matplotlib.text.Annotation at 0x7a7eba8>

. Free learning material
. See full copyright and disclaimer notice