def to_Altair(self) -> str: """ Generate minimal Altair code to visualize the Vis Returns ------- str String version of the Altair code. Need to print out the string to apply formatting. """ from lux.vislib.altair.AltairRenderer import AltairRenderer renderer = AltairRenderer(output_type="Altair") self.code = renderer.create_vis(self) return self.code
def to_altair(self, standalone=False) -> str: """ Generate minimal Altair code to visualize the Vis Parameters ---------- standalone : bool, optional Flag to determine if outputted code uses user-defined variable names or can be run independently, by default False Returns ------- str String version of the Altair code. Need to print out the string to apply formatting. """ from lux.vislib.altair.AltairRenderer import AltairRenderer renderer = AltairRenderer(output_type="Altair") self._code = renderer.create_vis(self, standalone) if lux.config.executor.name == "PandasExecutor": function_code = "def plot_data(source_df, vis):\n" function_code += "\timport altair as alt\n" function_code += "\tvisData = create_chart_data(source_df, vis)\n" else: function_code = "def plot_data(tbl, vis):\n" function_code += "\timport altair as alt\n" function_code += "\tvisData = create_chart_data(tbl, vis)\n" vis_code_lines = self._code.split("\n") for i in range(2, len(vis_code_lines) - 1): function_code += "\t" + vis_code_lines[i] + "\n" function_code += "\treturn chart\n#plot_data(your_df, vis) this creates an Altair plot using your source data and vis specification" function_code = function_code.replace("alt.Chart(tbl)", "alt.Chart(visData)") if "mark_circle" in function_code: function_code = function_code.replace("plot_data", "plot_scatterplot") elif "mark_bar" in function_code: function_code = function_code.replace("plot_data", "plot_barchart") elif "mark_line" in function_code: function_code = function_code.replace("plot_data", "plot_linechart") elif "mark_rect" in function_code: function_code = function_code.replace("plot_data", "plot_heatmap") return function_code
def to_VegaLite(self, prettyOutput=True) -> Union[dict, str]: """ Generate minimal Vega-Lite code to visualize the Vis Returns ------- Union[dict,str] String or Dictionary of the VegaLite JSON specification """ import json from lux.vislib.altair.AltairRenderer import AltairRenderer renderer = AltairRenderer(output_type="VegaLite") self._code = renderer.create_vis(self) if (prettyOutput): return "** Remove this comment -- Copy Text Below to Vega Editor(vega.github.io/editor) to visualize and edit **\n" + json.dumps( self._code, indent=2) else: return self._code
def to_Altair(self, standalone=False) -> str: """ Generate minimal Altair code to visualize the Vis Parameters ---------- standalone : bool, optional Flag to determine if outputted code uses user-defined variable names or can be run independently, by default False Returns ------- str String version of the Altair code. Need to print out the string to apply formatting. """ from lux.vislib.altair.AltairRenderer import AltairRenderer renderer = AltairRenderer(output_type="Altair") self._code = renderer.create_vis(self, standalone) return self._code
def get_Altair_vis_code(self, standalone=False) -> str: """ Returns code specific to the visualization without styling Parameters ---------- standalone : bool, optional Flag to determine if outputted code uses user-defined variable names or can be run independently, by default False Returns ------- str String version of the Altair code. Need to print out the string to apply formatting. """ from lux.vislib.altair.AltairRenderer import AltairRenderer renderer = AltairRenderer(output_type="Altair") self._code = renderer.create_vis(self, standalone) return self._code
def create_vis(self, vis, standalone=True): """ Input Vis object and return a visualization specification Parameters ---------- vis: lux.vis.Vis Input Vis (with data) standalone: bool Flag to determine if outputted code uses user-defined variable names or can be run independently Returns ------- chart : altair.Chart Output Altair Chart Object """ # Lazy Evaluation for 2D Binning if vis.mark == "scatter" and vis._postbin: vis._mark = "heatmap" PandasExecutor.execute_2D_binning(vis) # If a column has a Period dtype, or contains Period objects, convert it back to Datetime if vis.data is not None: for attr in list(vis.data.columns): if pd.api.types.is_period_dtype( vis.data.dtypes[attr]) or isinstance( vis.data[attr].iloc[0], pd.Period): dateColumn = vis.data[attr] vis.data[attr] = pd.PeriodIndex( dateColumn.values).to_timestamp() if pd.api.types.is_interval_dtype( vis.data.dtypes[attr]) or isinstance( vis.data[attr].iloc[0], pd.Interval): vis.data[attr] = vis.data[attr].astype(str) fig, ax = matplotlib_setup(4.5, 4) if vis.mark == "histogram": chart = Histogram(vis, fig, ax) elif vis.mark == "bar": chart = BarChart(vis, fig, ax) elif vis.mark == "scatter": chart = ScatterChart(vis, fig, ax) elif vis.mark == "line": chart = LineChart(vis, fig, ax) elif vis.mark == "heatmap": chart = Heatmap(vis, fig, ax) elif vis.mark == "geographical": return AltairRenderer().create_vis(vis, False) else: chart = None return chart if chart: plt.tight_layout() if lux.config.plotting_style and ( lux.config.plotting_backend == "matplotlib" or lux.config.plotting_backend == "matplotlib_code"): chart.ax = lux.config.plotting_style(chart.fig, chart.ax) plt.tight_layout() tmpfile = BytesIO() chart.fig.savefig(tmpfile, format="png") chart.chart = base64.b64encode(tmpfile.getvalue()).decode("utf-8") plt.clf() plt.close("all") if self.output_type == "matplotlib": return {"config": chart.chart, "vislib": "matplotlib"} if self.output_type == "matplotlib_code": if lux.config.plotting_style: import inspect chart.code += "\n".join( inspect.getsource( lux.config.plotting_style).split("\n ")[1:-1]) chart.code += "\nfig" chart.code = chart.code.replace("\n\t\t", "\n") return chart.code