bokeh
is the best for me, and it could be the best for you toobokeh
; I am simply a user, just like most of youbokeh
for the past ~3 yearsbokeh
matplotlib
bokeh
code, when compared to matplotlib
(personal opinion, of course):mpl
's docs are unreliablematplotlib
is still more popular because:
bokeh
) and has more features than current alternativesmatplotlib
:from bokeh.io import output_notebook, show
from bokeh.plotting import figure
output_notebook()
nquestions=[59322, 4355, 767, 127]
libs=['mpl', 'bokeh', 'altair', 'plotnine']
p = figure(plot_height=600, plot_width=800,
title='Histogram',
x_range=libs)
p.vbar(x=libs, top=nquestions, width=0.9)
p.yaxis.axis_label = 'Number of questions posted on SO'
show(p)
We start by some definitions to be used by multiple libraries:
import numpy as np
from types import SimpleNamespace
#data for line plots
dline = SimpleNamespace( x=[1,2,3,4,5,6,7,8,9],
y=[6,7,2,8,9,3,4,5,1],
size=15,
line_color='blue',
out_color='red',
fill_color='orange',
fill_alpha=1 )
#data for histograms
mu, sigma, npoints = 0, 0.5, 1000
nbins = 35
dhist = np.random.normal(mu, sigma, npoints)
hist_, edges_ = np.histogram(dhist, density=False, bins=nbins)
dhist = SimpleNamespace( data=dhist, hist=hist_, edges=edges_, nbins=nbins)
matplotlib
¶import matplotlib.pyplot as plt
%matplotlib inline
# the following requires ipympl
# %matplotlib widget
fig = plt.figure(figsize=(8,6))
ax = fig.add_subplot(111)
ax.set_ylabel('Y')
plt.title('Histogram')
plt_marker_options = dict(s=10*dline.size, color=dline.fill_color, marker='o',
edgecolor=dline.out_color,
alpha=dline.fill_alpha)
plt.plot(dline.x, dline.y, color=dline.line_color)
plt.scatter(dline.x, dline.y, **plt_marker_options)
plt.show()
plt.hist(dhist.data, bins=dhist.nbins)
plt.show()
#fig, ax = plt.subplots(figsize=(5,4))
fig = plt.figure()
ax = fig.add_subplot(111)
ax.hist(dhist.data, bins=dhist.nbins)
plt.show()
#we can create Figure and Axes instances explicitly
matplotlib
hard to use without constantly going back to the documentation, even for simple tasksmatplotlib
is more mature and complete, being the oldest. In addition, some wrappers on top of it provide additional convenient functionalities, such as mplhep
.matplotlib
, I would suggest using bokeh
for everything, including simple plots.bokeh
¶from bokeh.io import output_notebook, show
from bokeh.plotting import figure
output_notebook() # alternatively one could use output_file()
# create a new plot with default tools, using figure
pline = figure(plot_width=400, plot_height=400)
line_options = dict(line_width=2)
pline.line(dline.x, dline.y, **line_options)
marker_options = dict(size=dline.size, color=dline.out_color,
fill_color=dline.fill_color, fill_alpha=dline.fill_alpha)
circ = pline.circle(dline.x, dline.y, **marker_options)
show(pline)
hist_options = dict(fill_color="yellow", line_color="black", alpha=.8)
phist = figure(title='Bokeh Histogram', plot_width=600, plot_height=400,
background_fill_color="#2a4f32")
phist.quad(top=dhist.hist, bottom=0, left=dhist.edges[:-1], right=dhist.edges[1:], **hist_options)
phist.ygrid.grid_line_color = None
show(phist)
Figure and object properties can be very easily customised:
#set figure properties
pline.title = 'Line Plot'
pline.xgrid.grid_line_color = 'red'
pline.yaxis.axis_label = 'Y Axis'
pline.outline_line_width = 2
#set glyph properties
#recall: circ = p_line.circle(data.x, data.y, **marker_options)
circ.glyph.line_color = "indigo"
circ.glyph.line_dash = [3,1]
circ.glyph.line_width = 4
show(pline)
One can search for specific properties in the documentation or else do:
from bokeh.models import Axis
print([x for x in vars(Axis) if x[:1] != "_"])
The same idea can be applied to Title
, Legend
, Toolbar
, ... [more about models]
matplotlib
matplotlib
supports ): current feature requestbokeh
features not explored in this tutorial:¶