def render_vis(self, query_raw): # type: (str) -> VegaLite response = self.analyze_query(query_raw) if len(response['visList']) == 0: print("No best Viz; please try again.") return VegaLite({}) return VegaLite(response['visList'][0]['vlSpec'])
def vgplot_bar(df, x, y): if x is None: if df.index.name is None: df.index.name = 'index' x = df.index.name df = df.reset_index() assert x in df.columns if y is not None: assert y in df.columns df = df[[x, y]] df = df.melt([x], var_name='variable', value_name='value') D = { "$schema": "https://vega.github.io/schema/vega-lite/v2.json", "mark": "bar", "encoding": { "x": { "field": x, "type": "ordinal" }, "y": { "field": "value", "type": "quantitative" }, "color": { "field": "variable", "type": "nominal" } } } return VegaLite(D, data=df)
def build_student_feedback_histogram(height, width, data, questions, question): return VegaLite({ "$schema": "https://vega.github.io/schema/vega-lite/v2.0.json", # "title": """", "height": height, "width": width, "mark": "bar", "encoding": { "x": { "field": "answer", "type": "nominal", "sort": None, "axis": { "title": "" } }, "y": { "field": "q{0}".format(question), "type": "quantitative", "axis": { "title": "count" } }, "color": { "field": "answer", "type": "nominal", "legend": None, } } }, data)
def build_grade_histogram(height, width, data): return VegaLite({ "$schema": "https://vega.github.io/schema/vega-lite/v2.0.json", "title": "Module attainment histogram", "height": height, "width": width, "layer": [ { "mark": "bar", "encoding": { "x": { "bin": { "step": 10 }, "field": "final_grade", "type": "quantitative", "axis": { "title": "Grade", "tickCount": 10 }, "scale": { "domain": [0, 100] } }, "y": { "aggregate": "count", "type": "quantitative", "axis": { "title": "Number of students" } }, "color": { "bin": { "step": 10 }, "field": "final_grade", "type": "quantitative", "legend": None } } }, { "mark": "rule", "encoding": { "x": { "value": width/100 * 40 }, "size": { "value": 2 }, "color": { "value": "#ccc" } } }, { "mark": "rule", "encoding": { "x": { "value": width/100 * 60 }, "size": { "value": 2 }, "color": { "value": "#ccc" } } } ] }, data)
def vegalite_graph(self, shape=(400, 300), simple=False): from vega import VegaLite import pandas idx = np.linspace(0, self.fpr.size - 1, 500, dtype=np.uint) return VegaLite( { "title": { "text": "AUC = %.3f " % self.auc, "anchor": "end" }, "width": shape[0], "height": shape[1], "mark": { "type": "area", "line": True, "point": True }, "encoding": { "y": { "type": "quantitative", "field": "tpr", "title": "True Positive Rate" }, "x": { "type": "quantitative", "field": "fpr", "title": "False Positive Rate" }, "tooltip": [{ "type": "quantitative", "field": "tpr", "title": "True Positive" }, { "type": "quantitative", "field": "fpr", "title": "False Positive" }, { "type": "quantitative", "field": "thresholds", "title": "Threshold" }] } }, pandas.DataFrame( dict(fpr=self.fpr[idx], tpr=self.tpr[idx], thresholds=self.thresholds[idx])))
def build_grade_comparison_plot(height, width, data): return VegaLite({ "$schema": "https://vega.github.io/schema/vega-lite/v2.0.json", "title": "Module performance comparison", "height": height, "width": width, "layer": [ { "mark": "circle", "encoding": { "x": { "field": "average_grade", "type": "quantitative", "axis": { "title": "Average Grade" }, "scale": { "domain": [0, 100] } }, "y": { "field": "final_grade", "typs": "quantitative", "axis": { "title": "Module grade" }, "scale": { "domain": [0, 100] } }, } }, { "mark": "rule", "encoding": { "x": { "value": 0 }, "y": { "value": height }, "size": { "value": 1 }, "color": { "value": "red" } } } ] }, data)
def vgplot_scatter(df, x, y): assert x in df.columns assert y in df.columns df = df[[x, y]] D = { "$schema": "https://vega.github.io/schema/vega-lite/v2.json", "mark": "circle", "encoding": { "x": { "field": x, "type": "quantitative" }, "y": { "field": y, "type": "quantitative" }, } } return VegaLite(D, data=df)
def static_vis(self, **kwargs): """This function is called inplace of `vis` for reactive cells, whose participation in the event loop is different from the others. Optional arguments (Midas will guess the rest) mark -- "bar" | "circle" | "line" x -- name of the column to be the x axis x_type -- "ordinal" | "quantitative" | "temporal" y -- name of the column to be the y axis y_type -- "ordinal" | "quantitative" | "temporal" sort -- "" | "x" | "y" | "-x" | "-y" """ spec = parse_encoding(kwargs, self) if spec: sanity_check_spec_with_data(spec, self) # do show me vg_spec = static_vega_gen(spec, self) # print(vg_spec) return VegaLite(vg_spec) return None
def see(self, *fields, anchor=None, name=None): if (name is not None): viewname = '\"{0}\"'.format(name) else: viewname = Chart.DEFAULT_NAME fields = list(fields) if (not fields): fields = self._recent_fields elif (all([x[0] == '+' for x in fields])): fields = [x[1:] for x in fields] + self._recent_fields elif (all([x[0] == '-' for x in fields])): fields = filter(lambda x : x not in [y[1:] for y in fields], self._recent_fields) self._recent_fields = fields partial, asp = self._query(fields, anchor, viewname) # print('\n'.join(asp)) self._sol = draco(asp) if (name is not None): self._anchors[viewname] = anchor_spec(partial, self._sol.props[viewname], viewname) return VegaLite(self._sol.as_vl(viewname), self._data)
def _ipython_display_(self): from IPython.display import display from vega import VegaLite display(VegaLite(self.to_dict()))
def _ipython_display_(self): """Use the vega package to display in the classic Jupyter Notebook.""" from IPython.display import display from vega import VegaLite display(VegaLite(self.to_dict()))
def _get_render(self): vegalite = self._get_vegalite() return VegaLite(vegalite, self._data)
def __init__(self, *params, **args): VegaLite.__init__(self, *params, **args) self.ID = uuid.uuid4() self.InitNeeded = True
def _get_render(self): return VegaLite(self._get_vegalite(), self._data)
def render_vegalite(vis): # Render a visualization using vegalite VegaLite(vis.to_vl_obj()).display()