Matplotlib For Plotting And Data Visualization#

Matplotlib is a commonly used plotting library.

Exercise 32

Import matplotlib. Use convenient naming.

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 and y-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 and markersize), line (linestyle and linewidth), 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.

Exercise 33

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?

Exercise 34

The following equation represents the fraction of ligand, L, bound to its target protein, P, when \([P] <<< [L]\). \(K_{d}\) is the dissociation constant.

\[ bound = \frac{[L]}{K_{d} + [L]} \]
  1. Define a function that calculates the fraction of L bound to P using the equation provided.

  2. Create an array using a fixed number of points, e.g. 1000, between 0 and 100 \(mM\). This will serve as the X-values.

  3. 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.