Skip to content

Commit

Permalink
Update plot_config to plotting_style and documentation (#239)
Browse files Browse the repository at this point in the history
* update export tutorial to add explanation for standalone argument

* minor fixes and remove cell output in notebooks

* added contributing doc

* fix bugs and uncomment some tests

* remove raise warning

* remove unnecessary import

* split up rename test into two parts

* fix setting warning, fix data_type bugs and add relevant tests

* remove ordinal data type

* add test for small dataframe resetting index

* add loc and iloc tests

* fix attribute access directly to dataframe

* add small changes to code

* added test for qcut and cut

* add check if dtype is Interval

* added qcut test

* fix Record KeyError

* add tests

* take care of reset_index case

* small edits

* add data_model to column_group Clause

* small edits for row_group

* fixes to row group

* basic matplotlib chart example

* add config for start and cap for samples

* finish sampling config and tests

* black formatting

* add documentation for sampling config

* remove small added issues

* minor changes to docs

* migrate register default action to init

* config class

* oop png

* implement heatmap flag and add tests

* black formatting and documentation edits

* all except hist

* hist works

* heatmap

* only axis

* added code

* heatmap works

* heatmap works

* bar chart lim

* everything works

* add global var

* ui refine

* adjust comments

* black reformat

* add pd.io equalities for DataFrames

* black reformat

* matplotlib tests

* reformat and req

* add one default test

* black format

* removed .coverage; fixed typos; add nan import; add fig display statement

* doc and font size

* remove ipy

* color and check code not yet

* remove print

* all outputs

* update docs

* color channels work

* add titles

* rm

* all exports work

* add tests

* increase size

* leg size

* reformat

* abbrev axis

* remove bin

* no warning

* improve ui

* init for runtime

* merge

* plot config matplotlib

* add documentation

* close fig to save mem

* small change

* mem

* split tests

* reformat

* font

* ui changes

* make scatter narrow

* matplotlib abbrev

* matplotlib abbrev

* change to plotting_style

* update docs

* revert

* revert merged changes

* minor change

* fix doc

* update doc

* add fig

* rem

* minor fixes to docs

Co-authored-by: Kunal Agarwal <kagarwal2@berkeley.edu>
Co-authored-by: Doris Lee <dorisjunglinlee@gmail.com>
Co-authored-by: Kunal Agarwal <32151899+westernguy2@users.noreply.github.com>
Co-authored-by: Caitlyn Chen <caitlynachen@berkeley.edu>
Co-authored-by: Ujjaini Mukhopadhyay <ujjaini@berkeley.edu>
  • Loading branch information
6 people committed Jan 25, 2021
1 parent d4fbae3 commit 5e7d5c9
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 51 deletions.
2 changes: 1 addition & 1 deletion doc/source/guide/FAQ.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ How do I change the plotting library used for visualization?
I want to change the opacity of my chart, add title, change chart font size, etc. How do I modify chart settings?
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
To add custom plot settings to the recommendations, you can set the :code:`lux.config.plot_config` property. See `this tutorial <https://lux-api.readthedocs.io/en/latest/source/guide/style.html>`__ on how to configure chart properties. Lux currently only support chart modifications in Altair.
To add custom plot settings to the recommendations, you can set the :code:`lux.config.plotting_style` property. See `this tutorial <https://lux-api.readthedocs.io/en/latest/source/guide/style.html>`__ on how to configure chart properties. Lux supports chart modifications in Altair and Matplotlib.

How do I change aggregation functions, binning, or axis channels to non-default values?
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Expand Down
42 changes: 27 additions & 15 deletions doc/source/guide/style.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Styling Custom Plot Settings

.. note:: You can follow along this tutorial in a Jupyter notebook. [`Github <https://github.com/lux-org/lux-binder/blob/master/tutorial/4-chart-settings.ipynb>`_] [`Binder <https://mybinder.org/v2/gh/lux-org/lux-binder/master?urlpath=tree/tutorial/4-chart-settings.ipynb>`_]

In the last tutorial, we saw how :code:`Vis` objects could be exported into visualization code for further editing. What if we want to change the chart settings for *all* the visualizations displayed in the widget. In Lux, we can change the chart settings and aesthetics by inputting global custom plot settings the :code:`plot_config`.
In the last tutorial, we saw how :code:`Vis` objects could be exported into visualization code for further editing. What if we want to change the chart settings for *all* the visualizations displayed in the widget. In Lux, we can change the chart settings and aesthetics by inputting global custom plot settings the :code:`plotting_style`.

Example #1 : Changing Color and Title of all charts
---------------------------------------------------
Expand All @@ -25,18 +25,18 @@ To change the plot configuration in Altair, we need to specify a function that t
Let's say that we want to change all the graphical marks of the charts to green and add a custom title. We can define this `change_color_add_title` function, which configures the chart's mark as green and adds a custom title to the chart.

.. code-block:: python
lux.config.plotting_backend = "altair" # or 'vegalite'
def change_color_add_title(chart):
chart = chart.configure_mark(color="green") # change mark color to green
chart.title = "Custom Title" # add title to chart
return chart
lux.config.plotting_backend = "altair"
def change_color_add_title(chart):
chart = chart.configure_mark(color="green") # change mark color to green
chart.title = "Custom Title" # add title to chart
return chart
We then set the global plot configuration of the dataframe by changing the :code:`plot_config` property. With the added plot_config, Lux runs this user-defined function after every `Vis` is rendered to a chart, allow the user-defined function to override any existing default chart settings.
We then set the global plot configuration of the dataframe by changing the :code:`plotting_style` property. With the added plotting_style, Lux runs this user-defined function after every `Vis` is rendered to a chart, allow the user-defined function to override any existing default chart settings.

.. code-block:: python
lux.config.plot_config = change_color_add_title
lux.config.plotting_style = change_color_add_title
We now see that the displayed visualizations adopt these new imported settings.

Expand All @@ -45,18 +45,30 @@ We now see that the displayed visualizations adopt these new imported settings.
:align: center

Similarly, we can change the plot configurations for Matplotlib charts as well.
The plot_config attribute for Matplotlib charts takes in both the figure and axis as parameters.
The plotting_style attribute for Matplotlib charts takes in both the `fig` and `ax` as parameters.
`fig` handles figure width and other plot size attributes. `ax` supports changing the chart title and other plot labels and configurations.

.. code-block:: python
lux.config.plotting_backend = "matplotlib" # or 'matplotlib_code'
def add_title(fig, ax):
ax.set_title("Test Title")
lux.config.plotting_backend = "matplotlib"
def change_width_add_title(fig, ax):
fig.set_figwidth(7)
ax.set_title("Custom Title")
return fig, ax
.. code-block:: python
lux.config.plot_config = add_title
lux.config.plotting_style = change_width_add_title
Moreover, we can set the color and other figure styles using the rcParams attribute of pyplot.

.. code-block:: python
import matplotlib.pyplot as plt
import matplotlib
plt.rcParams['axes.prop_cycle'] = matplotlib.cycler(color='g')
We now see that the displayed visualizations adopt these new imported settings.

Expand Down Expand Up @@ -129,7 +141,7 @@ We want to decrease the opacity of scatterplots, but keep the opacity for the ot
.. code-block:: python
lux.config.plot_config = changeOpacityScatterOnly
lux.config.plotting_style = changeOpacityScatterOnly
df
.. image:: ../img/style-6.png
Expand Down
42 changes: 17 additions & 25 deletions doc/source/reference/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ In Lux, users can customize various global settings to configure the behavior of
df # recommendations generated for the first time with config
Change the default display of Lux
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Change the default display
~~~~~~~~~~~~~~~~~~~~~~~~~~~

We can set the :code:`default_display` to change whether the Pandas table or Lux widget is displayed by default. In the following block, we set the default display to 'lux', therefore the Lux widget will display first.

Expand Down Expand Up @@ -101,8 +101,8 @@ Lux currently only support Vega-Lite and matplotlib, and we plan to add support
:width: 700
:align: center

Change the sampling parameters of Lux
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Change the sampling parameters
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

To speed up the visualization processing, by default, Lux performs random sampling on datasets with more than 10000 rows. For datasets over 30000 rows, Lux will randomly sample 30000 rows from the dataset.

Expand Down Expand Up @@ -130,10 +130,11 @@ We can disable this feature and revert back to using a scatter plot by running t
lux.config.heatmap = False
Changing the plot styling
~~~~~~~~~~~~~~~~~~~~~~~~~~
Changing the plotting style
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Altair supports plot configurations to be applied on top of the generated graphs. To set a default plot configuration, first write a function that can take in a `chart` and returns a `chart`. For example:
In Lux, we can change the chart settings and aesthetics by inputting global custom plot settings the :code:`plotting_style`.
For charts rendered in Altair (default), we can change the plotting style by writing a function that takes a `AltairChart <https://altair-viz.github.io/user_guide/generated/toplevel/altair.Chart.html>`_ object as input and output. For example:

.. code-block:: python
Expand All @@ -142,37 +143,29 @@ Altair supports plot configurations to be applied on top of the generated graphs
chart.title = "Custom Title" # add title to chart
return chart
Then, set the `plot_config` to this function so that this function is applied to every plot generated.
Then, set the `plotting_style` to this function so that this function is applied to every plot generated.

.. code-block:: python
lux.config.plot_config = change_color_add_title
lux.config.plotting_style = change_color_add_title
The above results in the following changes:

.. image:: https://github.com/lux-org/lux-resources/blob/master/doc_img/style-2.png?raw=true
:width: 600
:align: center

See `this page <https://lux-api.readthedocs.io/en/latest/source/guide/style.html>`__ for more details.

Matplotlib also supports plot configurations to be applied on top of the generated graphs. To set a default plot configuration, first write a function that can take in a `fig` and 'ax' and returns a `fig` and 'ax. For example:
Matplotlib also supports plot configurations to be applied on top of the generated graphs. To set a default plot configuration, first write a function that can take in a `fig` and `ax` and returns a `fig` and `ax`.
`fig` handles figure width and other plot size attributes. `ax` supports changing the chart title and other plot labels and configurations. For example:

.. code-block:: python
def add_title(fig, ax):
ax.set_title("Test Title")
def change_width_add_title(fig, ax):
fig.set_figwidth(7) # change figure width
ax.set_title("Custom Title") # add title to chart
return fig, ax
.. code-block:: python
lux.config.plot_config = add_title
The above results in the following changes:

.. image:: https://github.com/lux-org/lux-resources/blob/master/doc_img/style-7.png?raw=true
:width: 600
:align: center
lux.config.plotting_style = change_width_add_title
See `this page <https://lux-api.readthedocs.io/en/latest/source/guide/style.html>`__ for more details.

Expand Down Expand Up @@ -207,5 +200,4 @@ If you would like to turn off the selection criteria completely and display ever
lux.config.topk = False
Beware that this may generate large numbers of visualizations (e.g., for 10 quantitative variables, this will generate 45 scatterplots in the Correlation action!)

Beware that this may generate large numbers of visualizations (e.g., for 10 quantitative variables, this will generate 45 scatterplots in the Correlation action!)
2 changes: 2 additions & 0 deletions doc/source/reference/gen/lux._config.config.Config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ lux.\_config.config.Config

~Config.default_display
~Config.heatmap
~Config.interestingness_fallback
~Config.pandas_fallback
~Config.plotting_backend
~Config.sampling
~Config.sampling_cap
Expand Down
2 changes: 2 additions & 0 deletions doc/source/reference/gen/lux.core.frame.LuxDataFrame.rst
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ lux.core.frame.LuxDataFrame
~LuxDataFrame.sem
~LuxDataFrame.set_SQL_table
~LuxDataFrame.set_axis
~LuxDataFrame.set_data_type
~LuxDataFrame.set_index
~LuxDataFrame.set_intent
~LuxDataFrame.set_intent_as_vis
Expand Down Expand Up @@ -246,6 +247,7 @@ lux.core.frame.LuxDataFrame
~LuxDataFrame.axes
~LuxDataFrame.columns
~LuxDataFrame.current_vis
~LuxDataFrame.data_type
~LuxDataFrame.dtypes
~LuxDataFrame.empty
~LuxDataFrame.exported
Expand Down
2 changes: 1 addition & 1 deletion lux/_config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Config:

def __init__(self):
self._default_display = "pandas"
self.plot_config = None
self.plotting_style = None
self.SQLconnection = ""
self.executor = None
# holds registered option metadata
Expand Down
2 changes: 1 addition & 1 deletion lux/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class LuxSeries(pd.Series):
"_rec_info",
"_pandas_only",
"_min_max",
"plot_config",
"plotting_style",
"_current_vis",
"_widget",
"_recommendation",
Expand Down
8 changes: 4 additions & 4 deletions lux/vislib/altair/AltairRenderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ def create_vis(self, vis, standalone=True):
chart = None

if chart:
if lux.config.plot_config and (
if lux.config.plotting_style and (
lux.config.plotting_backend == "vegalite" or lux.config.plotting_backend == "altair"
):
chart.chart = lux.config.plot_config(chart.chart)
chart.chart = lux.config.plotting_style(chart.chart)
if self.output_type == "VegaLite":
chart_dict = chart.chart.to_dict()
# this is a bit of a work around because altair must take a pandas dataframe and we can only generate a luxDataFrame
Expand All @@ -101,9 +101,9 @@ def create_vis(self, vis, standalone=True):
elif self.output_type == "Altair":
import inspect

if lux.config.plot_config:
if lux.config.plotting_style:
chart.code += "\n".join(
inspect.getsource(lux.config.plot_config).split("\n ")[1:-1]
inspect.getsource(lux.config.plotting_style).split("\n ")[1:-1]
)
chart.code += "\nchart"
chart.code = chart.code.replace("\n\t\t", "\n")
Expand Down
8 changes: 4 additions & 4 deletions lux/vislib/matplotlib/MatplotlibRenderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ def create_vis(self, vis, standalone=True):
return chart
if chart:
plt.tight_layout()
if lux.config.plot_config and (
if lux.config.plotting_style and (
lux.config.plotting_backend == "matplotlib"
or lux.config.plotting_backend == "matplotlib_code"
):
chart.fig, chart.ax = lux.config.plot_config(chart.fig, chart.ax)
chart.ax = lux.config.plotting_style(chart.fig, chart.ax)
plt.tight_layout()
tmpfile = BytesIO()
chart.fig.savefig(tmpfile, format="png")
Expand All @@ -100,11 +100,11 @@ def create_vis(self, vis, standalone=True):
if self.output_type == "matplotlib":
return {"config": chart.chart, "vislib": "matplotlib"}
if self.output_type == "matplotlib_code":
if lux.config.plot_config:
if lux.config.plotting_style:
import inspect

chart.code += "\n".join(
inspect.getsource(lux.config.plot_config).split("\n ")[1:-1]
inspect.getsource(lux.config.plotting_style).split("\n ")[1:-1]
)
chart.code += "\nfig"
chart.code = chart.code.replace("\n\t\t", "\n")
Expand Down

0 comments on commit 5e7d5c9

Please sign in to comment.