Skip to content

Instantly share code, notes, and snippets.

@palnabarun
Created Aug 8, 2018
Embed
What would you like to do?
'''
Proposed top-level API structure for mplaltair
'''
from matplotlib import pyplot as plt
from mplaltair._marks import (
_handle_area, _handle_bar, _handle_circle,
_handle_geoshape, _handle_line, _handle_point,
_handle_rect, _handle_rule, _handle_square,
_handle_text, _handle_tick
)
def _check_for_facets(chart):
'''Check if the chart spec is a faceted one
'''
raise NotImplementedError
def _handle_facets(chart, ax):
'''Resolve the facet directives and call render_figure
'''
raise NotImplementedError
def _setup_globals():
plt.grid()
_MARK_HANDLERS = {
'area': _handle_area,
'bar': _handle_bar,
'circle': _handle_circle,
'geoshape': _handle_geoshape,
'line': _handle_line,
'point': _handle_point,
'rect': _handle_rect,
'rule': _handle_rule,
'square': _handle_square,
'text': _handle_text,
'tick': _handle_tick
}
def render_figure(chart, ax=None):
'''Converts an altair chart spec to matplotlib figure
TODO:
The function name can be discussed upon. I just put it randomly.
This can also be a IPython magic.
Parameters
----------
chart : altair.Chart
Altair chart specification
ax : matplotlib.Axes, optional
Matplotlib to draw the chart on
Returns
-------
fig : matplotlib.figure
Matplotlib figure if an axes was not passed on as a parameter
ax : matplotlib.axes
Matplotlib figure if an axes was not passed on as a parameter
Raises
------
ValidationError
'''
if ax is None:
fig, ax = plt.subplots()
# I propose the below function to have a recursive approach which calls
# this function with the facet resolved chart
if _check_for_facets(chart):
_handle_facets(chart, ax)
mark = chart.mark
_MARK_HANDLERS[mark](chart, ax)
_setup_globals()
return fig, ax
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment