Skip to content

Instantly share code, notes, and snippets.

@jimpea
Created July 11, 2019 09:44
Show Gist options
  • Save jimpea/2f6ea64f90878110510d0469979ad317 to your computer and use it in GitHub Desktop.
Save jimpea/2f6ea64f90878110510d0469979ad317 to your computer and use it in GitHub Desktop.
Use matplotlib to plot two data series on separate y-axes.
from matplotlib import pyplot as plt
fractions = range(1, 16)
mcgs = [0,0,0,0,0,190,145,10,0,0,0,0,0,0,0]
dpms = [0,0,0,0,0,290,390,0,0,0,0,5,20,80,350]
#check lists for length
for list in [dpms, mcgs]:
assert len(list) == len(fractions), "lists should be same length"
fig, ax1 = plt.subplots(figsize=(6,4))
ax2 = ax1.twinx()
def my_plot(ax, fractions, alist, color = "red", ylabel = "ylabel"):
ax.scatter(fractions, alist, color=color, label=ylabel, alpha=0.5)
ax.plot(fractions, alist, color=color, ls=":")
ax.set_ylabel(ylabel)
my_plot(ax1,
fractions,
dpms,
ylabel=r"Total Radioactivity count (DPM) $10^{3}$",
color="orange")
my_plot(ax2,
fractions,
mcgs,
ylabel= r"Total Protein $\mu$g",
color="blue")
fig.legend(bbox_to_anchor=(1, 1), loc='upper left', borderaxespad=0.)
ax1.set_title(r"Incorporation of ${}^{14}$C into X")
ax1.set_ylim(0, 450)
ax2.set_ylim(0, 200)
ax1.set_xlabel("fraction")
fig.tight_layout()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment