Prev: Basic plotting with 'plot' | Next: Common plots
- Setting axes limits
- Graph title and axes labels
- Graph legend
- Moving axes
- Annotating
- 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]
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)
- Set the title and xlabel, ylabel properties respectively
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)
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
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')
- 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)
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')
- 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:
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='->'))
. Free learning material
. See full copyright and disclaimer notice