4211 class 6 matplotlib and bokeh
how to get a line plot
fig, ax = plt.subplots(figsize=(8, 2)) ax.plot(avgs.index, avgs.matplotlib)
how to add a shaded region
fill_between()
two main matplotlib plotting interfaces
functional(implicit) and object-oriented(explicit)
p.legend.click_policy =
how to change what happens after clicking somewhere (in this case on the legend)
best ways to share a bokeh plot
html is best, but also png or svg
bokeh import
import bokeh.io import bokeh.plotting
import statement for pyplot
import matplotlib.pyplot as plt
3 pods of visualization
javascript, matplotlib, opengl
static python visualization packages
matplotlib, seaborn
if not using a jupyter notebook how do you make the visual show up? (code statement)
plt.show()
what does .plot() do?
returns a matplotlib axes object since pandas uses it as a plotting backend
barh()
add horizontal bar
ax.(x or y)axis.set_major_formatter()
adjust format of tick labels
ax.(x or y)axis.set_major_locator()
adjust where the ticks are located
style sheet
allows you to customize multiple settings at once and can be activated using plt.style.context()
how to annotate area plot
annotate()
box plot
ax.boxplot()
create a histogram
ax.hist()
bokeh 4 steps
1. create a figure for glyphs 2. define a data source 3. choose kind of glyph desired 4. annotate columns to determine how to use them to place glyphs
why use bokeh
1. generating plots is quick and straightforward 2. become familiar with a lower level to build on that knowledge 3. discuss higher level packages in short order
how to format to matplotlib dates
1. import matplotlib.dates as mdates 2. mdates.date2num()
how to add a best fit line
1. import numpy as np 2. np.polynomial.fit( x_axis_dates, stackoverflow_monthly.matplotlib, degree )
axvspan()
Add a shaded region between two axvlines
axvline()
Add a vertical line across the Axes.
setting labels
ax.set_xlabel(), ax.set_ylabel()
how to access the axes
ax.xaxis ax.yaxis
dynamic python visualization packages
bokeh, plotly
how to make plots visible in bokeh
bokeh.io.output_notebook()
creating a bokeh figure
bokeh.plotting.figure()
functional/implicit
call functions provides by the pyplot module
object-oriented/explicit
call methods on figure and axes objects
what libraries does matplotlib use as its backend?
cartopy, ggplot, holoviews, seaborn, yellowbrick
What is matplotlib?
comprehensive library for creating static, animated, and interactive visualizations in Python. [It] makes easy things easy and hard things possible
figure object
container for all components of the visualization - contains 1+ axes objects
create an area plot
stackplot()
how to create a staked bar plot
still use bar or barh, but must call this function multiple times specifying the starting place for each new bar
axes object
subplots and other components such as x and y axes, legend, lines, etc
how to set limits of axes
xlim = (xmin, lim), ylim =(min, lim)
should you avoid mixing implicit and explicit approaches
yes
is matplotlib flexible?
yes, we can use multiple data structures without having to convert to a pandas structure