Old way
import prettyplotlib as ppl
from prettyplotlib import brewer2mpl
import matplotlib.pyplot as plt
from prettyplotlib.utils import remove_chartjunk
set2 = brewer2mpl.get_map('Set2', 'qualitative', 7).mpl_colors
fig, axes = plt.subplots(nrows=4, figsize=(6,8))
# from sRGB
colors = {'A':tuple(np.array([62, 169, 226])/255.0),
'C':tuple(np.array([239, 99, 76])/255.0),
'G':tuple(np.array([7, 198, 172])/255.0),
'T':tuple(np.array([247, 171, 17])/255.0)}
entire_strand = {'C':427419, 'G':413241, 'A':491488, 'T':491363}
reverse_half = {'C': 219518, 'G':201634, 'A':243963, 'T':246641}
forward_half = {'C': 207901, 'G':211607, 'A':247525, 'T':244722}
strands = {'Entire strand':entire_strand,
'Reverse half-strand':reverse_half,
'Forward half-strand':forward_half}
xticks = np.arange(4)
nucleotides = ['C', 'G', 'A', 'T']
annotate_yrange_factor = 0.03
for i, strand_pair in enumerate(strands.iteritems()):
title, strand = strand_pair
ax = axes[i]
ax.bar(left=xticks-0.4,
height=[strand[nucleotide] for nucleotide in nucleotides],
linewidth=0, color=[colors[nucleotide] for nucleotide in nucleotides])
ax.set_xlim(xticks[0]-0.5, xticks[-1]+0.5)
ax.grid(axis='y', color='white', linestyle='-', linewidth=0.5)
remove_chartjunk(ax, ['top', 'left', 'right']) #, remove_ticklabels='y')
ax.set_xticks(xticks)
ax.set_xticklabels(nucleotides, fontsize=14)
ax.set_title(title)
if i == 0:
# If this is the first plot, then save these ylims
ylim = ax.get_ylim()
# Add an extra 10% for padding
ylim = [ylim[0], ylim[1]*1.1]
# else:
ax.set_ylim(ylim)
# ylim = ax.get_ylim()
yrange = ylim[1] - ylim[0]
offset = yrange*annotate_yrange_factor
# add the actual numbers
for j, nucleotide in enumerate(nucleotides):
height = strand[nucleotide]
ax.annotate(r'%d' % height, (j, height+ offset),
verticalalignment='bottom',
horizontalalignment='center', fontsize=12)#, color='white')
prev_xlim = ax.get_xlim()
ax = axes[-1]
ax.hlines(y=0, xmin=prev_xlim[0], xmax=prev_xlim[-1], colors='grey')
deltas = [forward_half[nucleotide] - reverse_half[nucleotide] for nucleotide in nucleotides]
ax.bar(left=xticks-0.4,
height=deltas,
linewidth=0, color=[colors[nucleotide] for nucleotide in nucleotides])
ax.set_xlim(xticks[0]-0.5, xticks[-1]+0.5)
ax.grid(axis='y', color='white', linestyle='-', linewidth=0.5)
remove_chartjunk(ax, ['top', 'left', 'right', 'bottom']) #, remove_ticklabels='y')
ylim = ax.get_ylim()
max_abs_ylim = max(map(abs, ylim))
ylim = (-max_abs_ylim, max_abs_ylim)
ax.set_ylim(ylim)
yrange = ylim[1] - ylim[0]
offset_value = yrange*annotate_yrange_factor
print offset_value
for j, delta in enumerate(deltas):
# height = strand[nucleotide]
if delta >= 0:
offset = offset_value
verticalalignment = 'bottom'
else:
offset = -offset_value
verticalalignment = 'top'
ax.annotate(r'%d' % delta, (j, delta + offset),
verticalalignment=verticalalignment,
horizontalalignment='center', fontsize=12)#, color='white')
ax.set_xticks(xticks)
ax.set_xticklabels(nucleotides, fontsize=14)
ax.set_title('Difference in nucleotide counts: (Forward - Reverse)')
# Increase space between subplots
fig.subplots_adjust(hspace=0.75)
fig.savefig('bar_nucleotide_counts.png')
fig.savefig('bar_nucleotide_counts.pdf')
900.0
import prettyplotlib as ppl
import matplotlib.pyplot as plt
fig, axes = plt.subplots(nrows=4, figsize=(6,8))
# This is the order we want to plot them, too
nucleotides = ['C', 'G', 'A', 'T']
colors = {nucleotide: ppl.colors.set2[i] for i, nucleotide in enumerate(nucleotides)}
entire_strand = {'C':427419, 'G':413241, 'A':491488, 'T':491363}
reverse_half = {'C': 219518, 'G':201634, 'A':243963, 'T':246641}
forward_half = {'C': 207901, 'G':211607, 'A':247525, 'T':244722}
skew = {nucleotide: forward_half[nucleotide] - reverse_half[nucleotide] for nucleotide in nucleotides}
strands = {'Entire strand':entire_strand,
'Reverse half-strand':reverse_half,
'Forward half-strand':forward_half,
'Forward - Reverse':skew}
# The order that we want to plot the strand data in:
strand_names_ordered = ['Entire strand', 'Forward half-strand', 'Reverse half-strand', 'Forward - Reverse']
left = range(len(nucleotides))
for ax, strand_name in zip(axes, strand_names_ordered):
strand_data = strands[strand_name]
ppl.bar(ax, left=left,
height=[strand_data[nucleotide] for nucleotide in nucleotides],
annotate=True,
xticklabels=nucleotides,
grid='y',
color=[colors[nucleotide] for nucleotide in nucleotides])
ax.set_title(strand_name)
# Tell matplotlib to smartly lay out our figure
fig.tight_layout()
fig.savefig('prettyplotlib_bar_nucleotide_counts.png')
import prettyplotlib as ppl
import matplotlib.pyplot as plt
fig, axes = plt.subplots(nrows=4, figsize=(6,8))
# This is the order we want to plot them, too
nucleotides = ['C', 'G', 'A', 'T']
colors = {nucleotide: ppl.colors.set2[i] for i, nucleotide in enumerate(nucleotides)}
entire_strand = {'C':427419, 'G':413241, 'A':491488, 'T':491363}
reverse_half = {'C': 219518, 'G':201634, 'A':243963, 'T':246641}
forward_half = {'C': 207901, 'G':211607, 'A':247525, 'T':244722}
skew = {nucleotide: forward_half[nucleotide] - reverse_half[nucleotide] for nucleotide in nucleotides}
strands = {'Entire strand':entire_strand,
'Reverse half-strand':reverse_half,
'Forward half-strand':forward_half,
'Forward - Reverse':skew}
# The order that we want to plot the strand data in:
strand_names_ordered = ['Entire strand', 'Forward half-strand', 'Reverse half-strand', 'Forward - Reverse']
left = range(len(nucleotides))
for ax, strand_name in zip(axes, strand_names_ordered):
strand_data = strands[strand_name]
ax.bar(left=left,
height=[strand_data[nucleotide] for nucleotide in nucleotides],
color=[colors[nucleotide] for nucleotide in nucleotides])
ax.set_title(strand_name)
# Tell matplotlib to smartly lay out our figure
fig.tight_layout()
fig.savefig('matplotlib_bar_nucleotide_counts.png')