-
Notifications
You must be signed in to change notification settings - Fork 143
Examples with code
The basic idea for all of these plots is that you should be able to do ppl.{plot_command}(ax, x, y) instead of plt.{plot_command}(x, y). I haven't figured yet how to completely inherit Axes and such from matplotlib, because it is a huge, very complicated piece of code. But hopefully that functionality will be in future releases :)
For data where the x and y are ordered, like by time or distance, use ppl.plot, similar to how you'd use plt.plot or ax.plot from matplotlib. Except instead of plt.plot(x, y) or ax.plot(x, y), do ppl.plot(ax, x, y).
import prettyplotlib as ppl
# prettyplotlib imports
from prettyplotlib import plt
from prettyplotlib import mpl
from prettyplotlib import brewer2mpl
# Set the random seed for consistency
np.random.seed(12)
fig, ax = plt.subplots(1)
# Show the whole color range
for i in range(8):
y = np.random.normal(size=1000).cumsum()
x = np.arange(1000)
# For now, you need to specify both x and y :(
# Still figuring out how to specify just one
ppl.plot(ax, x, y, label=str(i), linewidth=0.75)
ppl.legend(ax)
fig.savefig('plot_prettyplotlib_default.png')If you want to move the legend to somewhere else, use the arguments you'd normally use for legend(), e.g. loc='lower right', ncol=4:
ppl.legend(ax, loc='lower left', ncol=4)The full code is here:
import prettyplotlib as ppl
# prettyplotlib imports
from prettyplotlib import plt
from prettyplotlib import mpl
from prettyplotlib import brewer2mpl
# Set the random seed for consistency
np.random.seed(12)
fig, ax = plt.subplots(1)
# Show the whole color range
for i in range(8):
y = np.random.normal(size=1000).cumsum()
x = np.arange(1000)
# For now, you need to specify both x and y :(
# Still figuring out how to specify just one
ppl.plot(ax, x, y, label=str(i))
ppl.legend(ax, loc='lower left', ncol=4)
fig.savefig('plot_prettyplotlib_legend_lower_left.png')This is for less structured data than plot, like if you have two samples X and Y, and gene expression for each one, and you want to see how the expression of the same gene is for the two samples. :)
import prettyplotlib as ppl
# This is "import matplotlib.pyplot as plt" from the prettyplotlib library
from prettyplotlib import plt
# This is "import matplotlib as mpl" from the prettyplotlib library
from prettyplotlib import mpl
# Set the random seed for consistency
np.random.seed(12)
fig, ax = plt.subplots(1)
# Show the whole color range
for i in range(8):
x = np.random.normal(loc=i, size=1000)
y = np.random.normal(loc=i, size=1000)
ppl.scatter(ax, x, y, label=str(i))
ppl.legend(ax)
ax.set_title('prettyplotlib `scatter` example\nshowing default color cycle and scatter params')
fig.savefig('scatter_prettyplotlib_default.png')If you really want to change all the parameters I worked so hard on, you can:
import prettyplotlib as ppl
from prettyplotlib import plt
from prettyplotlib import mpl
from prettyplotlib import brewer2mpl
# Set the random seed for consistency
np.random.seed(12)
fig, ax = plt.subplots(1)
#mpl.rcParams['axis.color_cycle'] = ['blue']
# Show the whole color range
for i in range(8):
x = np.random.normal(loc=i, size=1000)
y = np.random.normal(loc=i, size=1000)
ax.scatter(x, y, label=str(i), facecolor='blue', edgecolor='black', linewidth=1)
# Get back the top and right axes lines ("spines")
spines_to_remove = ['top', 'right']
for spine in spines_to_remove:
ax.spines[spine].set_visible(True)
# Get back the ticks. The position of the numbers is informative enough of
# the position of the value.
ax.xaxis.set_ticks_position('both')
ax.yaxis.set_ticks_position('both')
# For all the spines, make their line thicker and return them to be black
all_spines = ['top', 'left', 'bottom', 'right']
for spine in all_spines:
ax.spines[spine].set_linewidth(1.0)
ax.spines[spine].set_color('black')
# Change the labels back to black
ax.xaxis.label.set_color('black')
ax.yaxis.label.set_color('black')
# Change the axis title also back to black
ax.title.set_color('black')
# Remove the line around the legend box, and instead fill it with a light grey
# Also only use one point for the scatterplot legend because the user will
# get the idea after just one, they don't need three.
ax.legend()
ax.set_title('prettyplotlib `scatter` example\nshowing default color cycle and scatter params')
fig.savefig('scatter_prettyplotlib_back_to_matplotlib_default.png')This plot is great for categorical data, unrelated quantities such as number of cats, oranges, and computers per household.
import prettyplotlib as ppl
from prettyplotlib import plt
fig, ax = plt.subplots(1)
np.random.seed(14)
ppl.bar(ax, np.arange(10), np.abs(np.random.randn(10)))
fig.savefig('bar_prettyplotlib_default.png')import prettyplotlib as ppl
from prettyplotlib import plt
fig, ax = plt.subplots(1)
np.random.seed(14)
# 'y' for make a grid based on where the major ticks are on the y-axis
ppl.bar(ax, np.arange(10), np.abs(np.random.randn(10)), grid='y')
fig.savefig('bar_prettyplotlib_grid.png')If you say annotate=True in the ppl.bar command, then the value of the number on top of the bar plot will be added.
import prettyplotlib as ppl
from prettyplotlib import plt
import numpy as np
import string
fig, ax = plt.subplots(1)
np.random.seed(14)
n = 10
ppl.bar(ax, np.arange(n), np.abs(np.random.randn(n)), annotate=True, grid='y')
fig.savefig('bar_prettyplotlib_grid_annotated.png')If you happen to have negative values in your bar plot, the annotation on them will correctly appear underneath them. Also, the x-axis line will be moved up to the y=0 mark, instead of staying at the bottom.
import prettyplotlib as ppl
from prettyplotlib import 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.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()If you don't like the way prettyplotlib formats the numbers on top of the bars, or you just want to label with something else, you can supply your own numbers or strings by giving an iterable (like a list of strings or numbers) to annotate:
import prettyplotlib as ppl
from prettyplotlib import plt
import numpy as np
import string
fig, ax = plt.subplots(1)
np.random.seed(14)
n = 10
ppl.bar(ax, np.arange(n), np.abs(np.random.randn(n)),
annotate=range(n,2*n), grid='y', xticklabels=string.uppercase[:n])If you supply xticklabels as an argument to plt.bar, this will label each bar with this xlabel.
import prettyplotlib as ppl
from prettyplotlib import plt
import numpy as np
import string
fig, ax = plt.subplots(1)
np.random.seed(14)
n = 10
ppl.bar(ax, np.arange(n), np.abs(np.random.randn(n)), annotate=True, xticklabels=string.uppercase[:n], grid='y')
fig.savefig('bar_prettyplotlib_grid_annotated_labeled.png')








