Commit 01cf03fc authored by Mihai Baltac's avatar Mihai Baltac
Browse files

Mutifigure

parent 567a995b
Pipeline #10486 failed with stage
in 16 minutes and 10 seconds
......@@ -21,11 +21,11 @@ class Plotter(Loggable, object):
__metaclass__ = abc.ABCMeta
def __init__(self, interactive=False, path='.', stack_subplots=False,
color_scale):
color_scale=None):
self.interactive = interactive
self.path = path
self.stack_subplots = stack_subplots
self.color_scale = None
self.color_scale = color_scale
self.title = 'uiae'
@abc.abstractproperty
......@@ -94,7 +94,8 @@ class Plotter(Loggable, object):
if self.interactive:
ply.iplot(figure)
else:
# is there a use for ply.plot when one has no intereset in
pass
# is there a use for ply.plot when one has no interest in
# saving a file?
# -> check for different file types
......@@ -103,5 +104,3 @@ class Plotter(Loggable, object):
from figure import Figure
__all__ = ['Figure']
\ No newline at end of file
from figure import Figure, MultiFigure
__all__ = ['Figure', 'MultiFigure']
\ No newline at end of file
from nifty.plotting.plotly_wrapper import _PlotlyWrapper
from nifty.plotting.plots.private import _Plot2D, _Plot3D
from figure_internal import _2dFigure, _3dFigure, _MapFigure
from nifty.plotting.plots import HeatMap
from figure_internal import _2dFigure, _3dFigure, _MapFigure, _BaseFigure
from nifty.plotting.plots import HeatMap, MollweideHeatmap
from nifty.plotting.figures.util import validate_plots
from plotly.tools import make_subplots
class Figure(_PlotlyWrapper):
def __init__(self, data, title=None, width=None, height=None, xaxis=None, yaxis=None, zaxis=None):
kind, data = validate_plots(data)
class Figure(_BaseFigure):
def __init__(self, data, title=None, xaxis=None, yaxis=None, zaxis=None, width=None, height=None):
_BaseFigure.__init__(self, data, title, width, height)
kind, self.data = validate_plots(data)
if kind == _Plot2D:
if isinstance(data[0], HeatMap) and not width and not height:
x = len(data[0].data)
y = len(data[0].data[0])
if isinstance(self.data[0], HeatMap) and not width and not height:
x = len(self.data[0].data)
y = len(self.data[0].data[0])
if x > y:
width = 1000
height = int(1000*y/x)
width = 500
height = int(500*y/x)
else:
height = 1000
width = int(1000 * y / x)
self.internal = _2dFigure(data, title, width, height, xaxis, yaxis)
height = 500
width = int(500 * y / x)
if isinstance(self.data[0], MollweideHeatmap):
if not xaxis:
xaxis = False
if not yaxis:
yaxis = False
self.internal = _2dFigure(self.data, title, width, height, xaxis, yaxis)
elif kind == _Plot3D:
self.internal = _3dFigure(data, title, width, height, xaxis, yaxis, zaxis)
else:
self.internal = _MapFigure(data, title)
self.internal = _3dFigure(self.data, title, width, height, xaxis, yaxis, zaxis)
elif kind:
self.internal = _MapFigure(self.data, title)
def _to_plotly(self):
return self.internal._to_plotly()
class MultiFigure(_BaseFigure):
def __init__(self, rows, cols, title=None, width=None, height=None):
_BaseFigure.__init__(self, None, title, width, height)
self.cols = cols
self.rows = rows
self.subfigures = []
def get_subfigure(self, row, col):
for fig, r, c, _, _ in self.subfigures:
if r == row and c == col:
return fig
else:
return None
def add_subfigure(self, figure, row, col, row_span=1, col_span=1):
self.subfigures.append((figure, row, col, row_span, col_span))
def _to_plotly(self):
sub_titles = tuple([a[0].title for a in self.subfigures])
sub_specs = [[None]*self.cols for _ in range(self.rows)]
for fig, r, c, rs, cs in self.subfigures:
sub_specs[r][c] = dict(colspan=cs, rowspan=rs)
if isinstance(fig.internal, _3dFigure):
sub_specs[r][c]['is_3d'] = True
multi_figure_ply = make_subplots(self.rows,self.cols, subplot_titles=sub_titles, specs=sub_specs)
for fig, r, c, _, _ in self.subfigures:
for plot in fig.data:
multi_figure_ply.append_trace(plot._to_plotly(), r+1, c+1)
multi_figure_ply['layout'].update(height=self.height, width=self.width, title=self.title)
return multi_figure_ply
@staticmethod
def from_figures_2cols(figures, title=None, width=None, height=None):
multi_figure = MultiFigure((len(figures)+1)/2, 2, title, width, height)
for i in range(0, len(figures), 2):
multi_figure.add_subfigure(figures[i], i/2, 0)
for i in range(1, len(figures), 2):
multi_figure.add_subfigure(figures[i], i/2, 1)
return multi_figure
from abc import ABCMeta, abstractmethod
from nifty.plotting.plotly_wrapper import _PlotlyWrapper
class _BaseFigure(_PlotlyWrapper):
__metaclass__ = ABCMeta
......@@ -19,9 +20,9 @@ class _BaseFigure(_PlotlyWrapper):
scene = dict(
aspectmode='cube'
),
autosize=True,
autosize=False,
width=self.width,
height=self.height
height=self.height,
)
)
return ply_object
......@@ -39,10 +40,21 @@ class _2dFigure(_BaseFigure):
ply_object['layout']['scene']['aspectratio'] = dict()
if self.xaxis:
ply_object['layout']['xaxis'] = self.xaxis._to_plotly()
# ply_object['layout']['scene']['aspectratio']['x'] = self.xaxis.aspect_ratio
elif self.xaxis == False:
ply_object['layout']['xaxis'] = dict(
autorange=True,
showgrid=False,
zeroline=False,
showline=False,
autotick=True,
ticks='',
showticklabels=False
)
if self.yaxis:
ply_object['layout']['yaxis'] = self.yaxis._to_plotly()
# ply_object['layout']['scene']['aspectratio']['y'] = self.yaxis.aspect_ratio
elif self.yaxis == False:
ply_object['layout']['yaxis'] = dict(showline=False)
return ply_object
......@@ -57,13 +69,16 @@ class _3dFigure(_2dFigure):
ply_object['layout']['scene']['aspectratio'] = dict()
if self.xaxis:
ply_object['layout']['scene']['xaxis'] = self.xaxis._to_plotly()
# ply_object['layout']['scene']['aspectratio']['x'] = self.xaxis.aspect_ratio
elif self.xaxis == False:
ply_object['layout']['scene']['xaxis'] = dict(showline=False)
if self.yaxis:
ply_object['layout']['scene']['yaxis'] = self.yaxis._to_plotly()
# ply_object['layout']['scene']['aspectratio']['y'] = self.yaxis.aspect_ratio
elif self.yaxis == False:
ply_object['layout']['scene']['yaxis'] = dict(showline=False)
if self.zaxis:
ply_object['layout']['scene']['zaxis'] = self.zaxis._to_plotly()
# ply_object['layout']['scene']['aspectratio']['z'] = self.zaxis.aspect_ratio
elif self.zaxis == False:
ply_object['layout']['scene']['zaxis'] = dict(showline=False)
return ply_object
......
from nifty.plotting.plotly_wrapper import _PlotlyWrapper
from nifty.plotting.figures.util import validate_plots
class MultiFigure(_PlotlyWrapper):
def __init__(self, cols, rows, title=None, width=None, height=None):
self.cols = cols
self.rows = rows
self.title = title
self.width = width
self.height = height
def addSubfigure(self, data, title=None, width=None, height=None):
kind, data = validate_plots(data)
def _to_plotly(self):
pass
......@@ -2,9 +2,12 @@ from nifty.plotting.plots.private import _Plot2D, _Plot3D
from nifty.plotting.plots import ScatterGeoMap
def validate_plots(data):
def validate_plots(data, except_empty=True):
if not data:
raise Exception('Error: no plots given')
if except_empty:
raise Exception('Error: no plots given')
else:
return True
if type(data) != list:
data = [data]
......
......@@ -36,5 +36,4 @@ def _mollview(x, xsize=800):
extent = (0.02, 0.05, 0.96, 0.9)
ax = PA.HpxMollweideAxes(f, extent)
img = ax.projmap(x, nest=False, xsize=xsize)
return img
from nifty.plotting.plots.private import _Scatter2DBase, _Plot2D, _Plot3D
from nifty.plotting.plots.private import _Scatter2DBase
class Scatter2D(_Scatter2DBase, _Plot2D):
class Scatter2D(_Scatter2DBase):
def __init__(self, x=None, y=None, x_start=0, x_step=1,
label='', line=None, marker=None, webgl=True):
if y is None:
......@@ -21,7 +21,7 @@ class Scatter2D(_Scatter2DBase, _Plot2D):
return ply_object
class Scatter3D(_Scatter2DBase, _Plot3D):
class Scatter3D(_Scatter2DBase):
def __init__(self, x, y, z, label='', line=None, marker=None):
_Scatter2DBase.__init__(self, x, y, label, line, marker)
self.z = z
......
......@@ -5,11 +5,19 @@ import plotly.offline as ply_offline
import plotly.plotly as ply
def plot(figure, filename=None):
def plot(figure, filename=None, interactive=False):
if not filename:
filename = os.path.abspath('/tmp/temp-plot.html')
ply_offline.plot(figure._to_plotly(), filename=filename)
if interactive:
try:
__IPYTHON__
ply_offline.init_notebook_mode(connected=True)
ply_offline.iplot(figure._to_plotly(), filename=filename)
except NameError:
ply_offline.plot(figure._to_plotly(), filename=filename)
raise Warning('IPython not active! Running without interactive mode.')
else:
ply_offline.plot(figure._to_plotly(), filename=filename)
def plot_image(figure, filename=None, show=False):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment