Skip to content

Commit dd380ca

Browse files
authored
Merge branch 'master' into features/sort_interface
2 parents 79e4a41 + 51d6c60 commit dd380ca

File tree

5 files changed

+86
-14
lines changed

5 files changed

+86
-14
lines changed

doc/source/reference/config.rst

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,4 +253,20 @@ In Lux, large scatterplots are displayed as heatmaps that are 40x40 by default.
253253
254254
lux.config.heatmap_bin_size = 100
255255
256-
This generates heatmap visualizations that are binned into a 100x100 grid.
256+
This generates heatmap visualizations that are binned into a 100x100 grid.
257+
258+
259+
260+
Change the maximum number of bars displayed
261+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
262+
263+
In Lux, we limit the maximum number of bars displayed in a bar chart to 10 bars to avoid cluttering.
264+
If you want Lux to display more bars in each chart, we can set the :code:`number_of_bars` to increase the maximum number of bars displayed.
265+
266+
.. code-block:: python
267+
268+
lux.config.number_of_bars = 20
269+
270+
.. image:: https://github.com/lux-org/lux-resources/blob/master/doc_img/config_bars.png?raw=true
271+
:width: 400
272+
:align: left

doc/source/reference/gen/lux._config.config.Config.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
~Config.default_display
3131
~Config.heatmap
3232
~Config.interestingness_fallback
33+
~Config.label_len
34+
~Config.number_of_bars
3335
~Config.pandas_fallback
3436
~Config.plotting_backend
3537
~Config.plotting_scale

lux/_config/config.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ def __init__(self):
3232
self._sort = True
3333
self._ordering = Ordering.interestingness
3434
self._ordering_actions = OrderingDict({})
35+
self._number_of_bars = 10 # max no of bars displayed (rest shown as "+ k more")
36+
self._label_len = 25 # max length of x and y axis labels
3537
self._pandas_fallback = True
3638
self._interestingness_fallback = True
3739
self.heatmap_bin_size = 40
@@ -50,6 +52,46 @@ def __init__(self):
5052
self.streaming = False
5153
self.render_widget = True
5254

55+
@property
56+
def number_of_bars(self):
57+
return self._number_of_bars
58+
59+
@number_of_bars.setter
60+
def number_of_bars(self, k: int) -> None:
61+
"""
62+
Parameters
63+
----------
64+
k : int
65+
Number of bars in output bar charts; rest are not displayed
66+
"""
67+
if type(k) == int:
68+
self._number_of_bars = k
69+
else:
70+
warnings.warn(
71+
"The number of bars must be an integer.",
72+
stacklevel=2,
73+
)
74+
75+
@property
76+
def label_len(self):
77+
return self._label_len
78+
79+
@label_len.setter
80+
def label_len(self, l: int) -> None:
81+
"""
82+
Parameters
83+
----------
84+
l : int
85+
Maximum length of string axis labels
86+
"""
87+
if type(l) == int:
88+
self._label_len = l
89+
else:
90+
warnings.warn(
91+
"The maximum length must be an integer.",
92+
stacklevel=2,
93+
)
94+
5395
@property
5496
def topk(self):
5597
return self._topk

lux/vislib/altair/BarChart.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from lux.vislib.altair.AltairChart import AltairChart
1616
import altair as alt
1717
import lux
18+
import math
1819

1920
alt.data_transformers.disable_max_rows()
2021
from lux.utils.utils import get_agg_title
@@ -41,17 +42,22 @@ def initialize_chart(self):
4142
x_attr = self.vis.get_attr_by_channel("x")[0]
4243
y_attr = self.vis.get_attr_by_channel("y")[0]
4344

45+
# Deal with overlong string axes labels
4446
x_attr_abv = str(x_attr.attribute)
4547
y_attr_abv = str(y_attr.attribute)
48+
label_len = lux.config.label_len
49+
prefix_len = math.ceil(3.0 * label_len / 5.0)
50+
suffix_len = label_len - prefix_len
51+
if len(x_attr_abv) > label_len:
52+
x_attr_abv = x_attr.attribute[:prefix_len] + "..." + x_attr.attribute[-suffix_len:]
53+
if len(y_attr_abv) > label_len:
54+
y_attr_abv = y_attr.attribute[:prefix_len] + "..." + y_attr.attribute[-suffix_len:]
4655

47-
if len(x_attr_abv) > 25:
48-
x_attr_abv = x_attr.attribute[:15] + "..." + x_attr.attribute[-10:]
49-
if len(y_attr_abv) > 25:
50-
y_attr_abv = y_attr.attribute[:15] + "..." + y_attr.attribute[-10:]
5156
if isinstance(x_attr.attribute, str):
5257
x_attr.attribute = x_attr.attribute.replace(".", "")
5358
if isinstance(y_attr.attribute, str):
5459
y_attr.attribute = y_attr.attribute.replace(".", "")
60+
5561
# To get datetime to display correctly on bar charts
5662
if x_attr.data_type == "temporal":
5763
x_attr.data_type = "nominal"
@@ -99,7 +105,8 @@ def initialize_chart(self):
99105
if x_attr.sort == "ascending":
100106
x_attr_field.sort = "-y"
101107
x_attr_field_code = f"alt.X('{x_attr.attribute}', type= '{x_attr.data_type}', axis=alt.Axis(labelOverlap=True, title='{x_attr_abv}'),sort='-y')"
102-
k = 10
108+
109+
k = lux.config.number_of_bars
103110
self._topkcode = ""
104111
n_bars = len(self.data.iloc[:, 0].unique())
105112
plotting_scale = lux.config.plotting_scale

lux/vislib/matplotlib/BarChart.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616
from lux.utils.utils import get_agg_title
1717
import pandas as pd
1818
import numpy as np
19+
import math
1920
import matplotlib.pyplot as plt
2021
from lux.utils.utils import matplotlib_setup
2122
from matplotlib.cm import ScalarMappable
2223
from lux.utils.date_utils import compute_date_granularity
2324
from matplotlib.ticker import MaxNLocator
25+
import lux
2426

2527

2628
class BarChart(MatplotlibChart):
@@ -44,13 +46,16 @@ def initialize_chart(self):
4446
x_attr = self.vis.get_attr_by_channel("x")[0]
4547
y_attr = self.vis.get_attr_by_channel("y")[0]
4648

47-
x_attr_abv = x_attr.attribute
48-
y_attr_abv = y_attr.attribute
49-
50-
if len(x_attr.attribute) > 25:
51-
x_attr_abv = x_attr.attribute[:15] + "..." + x_attr.attribute[-10:]
52-
if len(y_attr.attribute) > 25:
53-
y_attr_abv = y_attr.attribute[:15] + "..." + y_attr.attribute[-10:]
49+
# Deal with overlong string axes labels
50+
x_attr_abv = str(x_attr.attribute)
51+
y_attr_abv = str(y_attr.attribute)
52+
label_len = lux.config.label_len
53+
prefix_len = prefix_len = math.ceil(3.0 * label_len / 5.0)
54+
suffix_len = label_len - prefix_len
55+
if len(x_attr_abv) > label_len:
56+
x_attr_abv = x_attr.attribute[:prefix_len] + "..." + x_attr.attribute[-suffix_len:]
57+
if len(y_attr_abv) > label_len:
58+
y_attr_abv = y_attr.attribute[:prefix_len] + "..." + y_attr.attribute[-suffix_len:]
5459

5560
if x_attr.data_model == "measure":
5661
agg_title = get_agg_title(x_attr)
@@ -61,7 +66,7 @@ def initialize_chart(self):
6166
measure_attr = y_attr.attribute
6267
bar_attr = x_attr.attribute
6368

64-
k = 10
69+
k = lux.config.number_of_bars
6570
n_bars = len(self.data.iloc[:, 0].unique())
6671
if n_bars > k: # Truncating to only top k
6772
remaining_bars = n_bars - k

0 commit comments

Comments
 (0)