-
Notifications
You must be signed in to change notification settings - Fork 142
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')




