Skip to content

Commit

Permalink
Added axis name for unnamed Series visualization (#263)
Browse files Browse the repository at this point in the history
* rec series

* rec

* add changes back

* fix tests

* rm

* change

* set plotting_style

Co-authored-by: Caitlyn Chen <caitlynachen@berkeley.edu>
  • Loading branch information
caitlynachen and Caitlyn Chen committed Feb 17, 2021
1 parent dfb6ecf commit f745fb9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
16 changes: 10 additions & 6 deletions lux/vislib/altair/Histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,21 @@ def initialize_chart(self):
markbar = x_range / plot_range * 12

self.data = AltairChart.sanitize_dataframe(self.data)

axis_title = f"{msr_attr_abv} (binned)"
if msr_attr.attribute == " ":
axis_title = "Series (binned)"
if measure.channel == "x":
chart = (
alt.Chart(self.data)
.mark_bar(size=markbar)
.encode(
alt.X(
str(msr_attr.attribute),
title=f"{msr_attr.attribute} (binned)",
title=axis_title,
bin=alt.Bin(binned=True),
type=msr_attr.data_type,
axis=alt.Axis(labelOverlap=True, title=f"{msr_attr_abv} (binned)"),
axis=alt.Axis(labelOverlap=True, title=axis_title),
scale=alt.Scale(domain=(x_min, x_max)),
),
alt.Y("Number of Records", type="quantitative"),
Expand All @@ -79,9 +83,9 @@ def initialize_chart(self):
x=alt.X("Number of Records", type="quantitative"),
y=alt.Y(
str(msr_attr.attribute),
title=f"{msr_attr.attribute} (binned)",
title=axis_title,
bin=alt.Bin(binned=True),
axis=alt.Axis(labelOverlap=True, title=f"{msr_attr_abv} (binned)"),
axis=alt.Axis(labelOverlap=True, title=axis_title),
scale=alt.Scale(domain=(x_min, x_max)),
),
)
Expand All @@ -96,14 +100,14 @@ def initialize_chart(self):
if measure.channel == "x":
self.code += f"""
chart = alt.Chart(visData).mark_bar(size={markbar}).encode(
alt.X('{msr_attr.attribute}', title='{msr_attr.attribute} (binned)',bin=alt.Bin(binned=True), type='{msr_attr.data_type}', axis=alt.Axis(labelOverlap=True, title='{msr_attr_abv} (binned)'), scale=alt.Scale(domain=({x_min}, {x_max}))),
alt.X('{msr_attr.attribute}', title='{axis_title}',bin=alt.Bin(binned=True), type='{msr_attr.data_type}', axis=alt.Axis(labelOverlap=True, title='{axis_title}'), scale=alt.Scale(domain=({x_min}, {x_max}))),
alt.Y("Number of Records", type="quantitative")
)
"""
elif measure.channel == "y":
self.code += f"""
chart = alt.Chart(visData).mark_bar(size={markbar}).encode(
alt.Y('{msr_attr.attribute}', title='{msr_attr.attribute} (binned)',bin=alt.Bin(binned=True), type='{msr_attr.data_type}', axis=alt.Axis(labelOverlap=True, title='{msr_attr_abv} (binned)'), scale=alt.Scale(domain=({x_min}, {x_max}))),
alt.Y('{msr_attr.attribute}', title='{axis_title}',bin=alt.Bin(binned=True), type='{msr_attr.data_type}', axis=alt.Axis(labelOverlap=True, title='{axis_title}'), scale=alt.Scale(domain=({x_min}, {x_max}))),
alt.X("Number of Records", type="quantitative")
)
"""
Expand Down
7 changes: 5 additions & 2 deletions lux/vislib/matplotlib/Histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,15 @@ def initialize_chart(self):

x_label = ""
y_label = ""
axis_title = f"{msr_attr_abv} (binned)"
if msr_attr_abv == " ":
axis_title = "Series (binned)"
if measure.channel == "x":
x_label = f"{msr_attr.attribute} (binned)"
x_label = axis_title
y_label = "Number of Records"
elif measure.channel == "y":
x_label = "Number of Records"
y_label = f"{msr_attr.attribute} (binned)"
y_label = axis_title

self.ax.set_xlabel(x_label)
self.ax.set_ylabel(y_label)
Expand Down
17 changes: 17 additions & 0 deletions tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,20 @@ def test_series_recommendation():
df.plot_config = None
df = df["YearsAtCompany"] / df["TotalWorkingYears"]
assert len(df.recommendation["Distribution"]) > 0, "Recommendation property empty for LuxSeries"


def test_unnamed_column():
lux.config.plotting_backend = "matplotlib"
df = pd.read_csv("https://raw.githubusercontent.com/lux-org/lux-datasets/master/data/employee.csv")
lux.config.plotting_style = None
series = df["YearsAtCompany"] / df["TotalWorkingYears"]
series.__repr__()
axis_title = "Series (binned)"
exported_code_str = series.recommendation["Distribution"][0].to_matplotlib_code()
assert axis_title in exported_code_str, "Unnamed column should have 'Series' as placeholder"

lux.config.plotting_backend = "vegalite"
series = df["YearsAtCompany"] / df["TotalWorkingYears"]
series.__repr__()
exported_code_str = series.recommendation["Distribution"][0].to_Altair()
assert axis_title in exported_code_str, "Unnamed column should have 'Series' as placeholder"

0 comments on commit f745fb9

Please sign in to comment.