Skip to content

Commit

Permalink
prepping for phosphor grid, more work on right y axis for matplotlib
Browse files Browse the repository at this point in the history
  • Loading branch information
timkpaine committed May 29, 2018
1 parent 840f4c0 commit c9b89d1
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
1 change: 1 addition & 0 deletions lantern/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from .data import *
from .grids import grid
from .live import run, pipeline, LanternLive, Streaming, WebSocketSource, RandomSource, RandomSource2
from .utils import download


__all__ = ['plot', 'figure', 'grids', 'data', 'extensions']
Expand Down
5 changes: 4 additions & 1 deletion lantern/grids/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from .grid_plotly import plotly_grid
from .grid_qgrid import qgrid_grid
from .grid_psp import psp_grid
from .grid_phosphor import phosphor_grid


_BACKENDS = ['plotly', 'qgrid', 'psp']
_BACKENDS = ['plotly', 'qgrid', 'psp', 'phosphor']


def _backend_to_grid_foo(backend, theme=None):
Expand All @@ -13,6 +14,8 @@ def _backend_to_grid_foo(backend, theme=None):
return qgrid_grid
if backend == 'psp':
return psp_grid
if backend == 'phosphor':
return phosphor_grid


def grid(data, backend='psp', **kwargs):
Expand Down
28 changes: 16 additions & 12 deletions lantern/plotting/plot_matplotlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def show(self, title='', xlabel='', ylabel='', xaxis=True, yaxis=True, xticks=Tr
labels += label

if legend:
self._axes[-1].legend(lines, labels, loc='center left', bbox_to_anchor=(1 + .05*min(len(self._axes_by_side['right']), 1), 0.5), fancybox=True)
self._axes[-1].legend(lines, labels, loc='center left', bbox_to_anchor=(1 + .05*(min(len(self._axes_by_side['right']), 1)+1), 0.5), fancybox=True)

if xlabel:
self._axes[-1].set_xlabel(xlabel)
Expand Down Expand Up @@ -161,7 +161,7 @@ def area(self, data, color=None, y_axis='left', stacked=False, **kwargs):
color = get_color(i, col, color)
x = ax.plot(data.index, data[col], color=color, **kwargs)
ax.fill_between(data.index, data[col], alpha=.7, color=color)
self._legend.append((col, x[0]))
self._legend.append((col, x[0], y_axis))

def bar(self, data, color=None, y_axis='left', stacked=False, **kwargs):
for i, col in enumerate(data):
Expand All @@ -177,12 +177,11 @@ def _bar(self):
kwargses = []

left_count = 1
right_count = 2
right_count = 1

# ax = self._newAx(x=False, y=False, y_side='left', color=color)
ax = self._newAx(x=False, y=False, y_side='left')

# ax2 = self._newAx(x=False, y=True, y_side='right', color=color)
ax2 = self._newAx(x=False, y=True, y_side='right')

for d in self._bars:
data.append(d[0])
Expand All @@ -196,14 +195,19 @@ def _bar(self):
elif d[2] == 'right' and not d[3]:
right_count += 1

left_width = 1/left_count
left_count2 = 0
width = 1/(left_count+right_count)
count = 0

for d in self._bars:
for col in d[0].columns:
x = ax.bar(d[0].index+left_count2*((d[0].index[1]-d[0].index[0])/left_count), d[0][col], width=left_width, color=d[1], **d[4])
self._legend.append((col, x))
left_count2 += 1
if d[2] == 'right':
x = ax2.bar(d[0].index+count*((d[0].index[1]-d[0].index[0])/(left_count+right_count)), d[0][col], width=width, color=d[1], **d[4])
self._legend.append((col, x, d[2]))
count += 1
else:
x = ax.bar(d[0].index+count*((d[0].index[1]-d[0].index[0])/(left_count+right_count)), d[0][col], width=width, color=d[1], **d[4])
self._legend.append((col, x, d[2]))
count += 1

def _hist(self):
if not self._hists:
Expand Down Expand Up @@ -234,7 +238,7 @@ def line(self, data, color=None, y_axis='left', **kwargs):
ax = self._newAx(x=False, y=(y_axis == 'right'), y_side=y_axis, color=color)
for i, col in enumerate(data):
x = ax.plot(data.index, data[col], color=get_color(i, col, color), **kwargs)
self._legend.append((col, x[0]))
self._legend.append((col, x[0], y_axis))

def scatter(self, data, color=None, x=None, y=None, y_axis='left', **kwargs):
for i, col in enumerate(data):
Expand All @@ -249,4 +253,4 @@ def step(self, data, color=None, y_axis='left', **kwargs):
ax = self._newAx(x=False, y=(y_axis == 'right'), y_side=y_axis, color=color)
for i, col in enumerate(data):
x = ax.plot(data.index, data[col], drawstyle='steps', color=get_color(i, col, color), **kwargs)
self._legend.append((col, x[0]))
self._legend.append((col, x[0], y_axis))
8 changes: 8 additions & 0 deletions lantern/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,11 @@ def in_ipynb():
return True
return False
return False


def download(df):
from IPython.display import HTML
import base64
csv = base64.b64encode(df.reset_index().to_csv().encode()).decode()
html = '<a download="download.csv" href="data:text/csv;base64,{payload}" target="_blank">Download</a>'.format(payload=csv)
return HTML(html)

0 comments on commit c9b89d1

Please sign in to comment.