Matplotlib For Plotting And Data Visualization#
Matplotlib is a commonly used plotting library.
Import matplotlib. Use convenient naming.
Solution to Exercise 32
import matplotlib.pyplot as plt
To create a plot:
Use matplotlib.pyplot.figure to create a figure object. This is needed when we want to e.g. tweak the size of the figure.
Use matplotlib.pyplot.plot to produce a plot. The first and second arguments are for the
x-values
andy-values
, i.e. the independent and dependent variables. If no x-values are provided, the default is (0, 1, … n-1) with n the length of the y-values. This function has also several optional arguments define basic formatting properties like color (color
), marker (marker
andmarkersize
), line (linestyle
andlinewidth
), and names for legend items (label
).Color values can be color names or abbreviations, e.g. black = k, red = r, yellow = y, green = g, cyan = c, blue = b …
Markers can be circles = o, squares = s, triangle up = ^, triangle down = v, diamond = D, cross = x …
Line styles can be solid = -, dashed = –, dotted = : …
See the documentation for more information and examples.
Add features, such as title, axis labels, axis limits, and a legend to the plot using matplotlib.pyplot.title, matplotlib.pyplot.xlabel, matplotlib.pyplot.ylabel, matplotlib.pyplot.axis, and matplotlib.pyplot.legend.
Execute the matplotlib.pyplot.show command to show the plot.
See the documentation for more information and examples.
We constructed a Lineweaver-Burk plot for the kinetic data obtained for an enzyme in the absence and presence of inhibitor using the following code.
Subconc = np.array([1, 2, 4, 8, 12]) #the substrate concentrations, in mM
V0withoutI = np.array([2.1, 3.7, 6.6, 11.6, 15.1]) #the initial velocity in the absence of inhibitor for each substrate concentration, in mM/s
V0withI = np.array([1.1, 2.2, 4.1, 7.4, 10.2]) #the initial velocity in the presence of inhibitor for each substrate concentration, in mM/s
plt.figure(figsize=(7,5)) #start a figure object
plt.plot(1/Subconc, 1/V0withoutI, #plot a set of (x (=1/the concentrations), y (= 1/the measured velocities in the absence of inhibitor)) data
marker='o', color='b', markersize=8, #use a round, blue marker with size 8
linestyle='solid', linewidth='1', #use a solid line with width 1
label='Without inhibitor') #add a label for the legend
plt.plot(1/Subconc, 1/V0withI, #plot a set of (x (=1/the concentrations), y (= 1/the measured velocities in the presence of inhibitor)) data
marker='o', color='b', markersize=8, #use a round, blue marker with size 8
linestyle='solid', linewidth='1', #use a solid line with width 1
label='With inhibitor') #add a label for the legend
plt.title('Lineweaver-Burk plot', fontsize=18) #title of graph
plt.xlabel('$1/[S_{0}]$ ($mM^{-1}$)', fontsize=14) #X-axis label
plt.ylabel('$1/V_{0}$ ($s mM^{-1}$)', fontsize=14) #Y-axis label
plt.legend() #add a legend
plt.show() #show the figure object
Can you adapt the code so the two curves have a different color, marker style, and line style?
Solution to Exercise 33
Here’s one possible solution.
Subconc = np.array([1, 2, 4, 8, 12]) #the substrate concentrations, in mM
V0withoutI = np.array([2.1, 3.7, 6.6, 11.6, 15.1]) #the initial velocity in the absence of inhibitor for each substrate concentration, in mM/s
V0withI = np.array([1.1, 2.2, 4.1, 7.4, 10.2]) #the initial velocity in the presence of inhibitor for each substrate concentration, in mM/s
plt.figure(figsize=(7,5)) #start a figure object
plt.plot(1/Subconc, 1/V0withoutI, #plot a set of (x (=1/the concentrations), y (= 1/the measured velocities in the absence of inhibitor)) data
marker='o', color='b', markersize=8, #use a round, blue marker with size 8
linestyle='solid', linewidth='1', #use a solid line with width 1
label='Without inhibitor') #add a label for the legend
plt.plot(1/Subconc, 1/V0withI, #plot a set of (x (=1/the concentrations), y (= 1/the measured velocities in the presence of inhibitor)) data
marker='s', color='r', markersize=8, #use a red, square marker with size 8
linestyle='dashed', linewidth='1.5', #use a dashed line with width 1.5
label='With inhibitor') #add a label for the legend
plt.title('Lineweaver-Burk plot', fontsize=18) #title of graph
plt.xlabel('$1/[S_{0}]$ ($mM^{-1}$)', fontsize=14) #X-axis label
plt.ylabel('$1/V_{0}$ ($s mM^{-1}$)', fontsize=14) #Y-axis label
plt.axis([0, 1.2, 0, 1]) #adjust the axis limits to xmin=0, xmax=1.2, ymin=0, and ymax=1
plt.legend() #add a legend
plt.show() #show the figure object
The following equation represents the fraction of ligand, L, bound to its target protein, P, when \([P] <<< [L]\). \(K_{d}\) is the dissociation constant.
Define a function that calculates the fraction of L bound to P using the equation provided.
Create an array using a fixed number of points, e.g. 1000, between 0 and 100 \(mM\). This will serve as the X-values.
Plot the function for two different scenarios, \(K_{d}\) = 1 \(mM\) and \(K_{d}\) = 10 \(mM\). Use a different colour for each curve. Make sure to include a title, X-and Y-axes labels, and a legend.
Solution to Exercise 34
Here’s one possible solution.
#Define a function that calculates the the fraction of L bound to P using the equation provided
def fractionbound(ligconc, disconst):
"""
Evaluate the fraction of ligand, L, bound to its target protein, P, using [L]/(Kd + [L]).
Args:
ligconc = [L], the concentration of ligand (float) in mM
disconst = Kd, the dissociation constant (float) in mM
Returns:
the fraction of L bound to P (float) in %
"""
fracbound = ligconc / (disconst + ligconc)
return fracbound
#Create an array to serve as the X-values
arrayL = np.linspace(0, 100, 1000) #create an array using 1000 points between 0 and 100
#Plot the function for two different scenarios
plt.figure(figsize=(7,5)) #start a figure object
plt.plot(arrayL, fractionbound(arrayL, 1), #plot a set of (x (=the ligand concentrations),y (= the calculated fractions of L bound using Kd = 1 mM)) data
marker='o', color='gray', markersize=4, #use a round, gray marker with size 4
linestyle='solid', linewidth='0.5', #use a solid line with width 0.5
label='$K_{d}$ = 1 mM') #use a label for the legend
plt.plot(arrayL, fractionbound(arrayL, 10), #plot a set of (x (=the ligand concentrations),y (= the calculated fractions of L bound using Kd = 10 mM)) data
marker='o', color='red', markersize=4, #use a round, red marker with size 4
linestyle='solid', linewidth='0.5', #use a solid line with width 0.5
label='$K_{d}$ = 10 mM') #use a label for the legend
plt.title('Binding curve', fontsize=18) #title of graph
plt.xlabel('$[L]$ in $mM$', fontsize=14) #X-axis label
plt.ylabel('Fraction bound', fontsize=14) #Y-axis label
plt.legend() #add a legend
plt.show() #show the figure object