Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
height=600,
width=800,
):
"""Generate many scatterplots.
Based on several columns, pairwise.
"""
opt_args = choose_kwargs(locals(), ["color", "tooltip"])
assert group_by is None, "Long format not supported yet"
return (
alt.Chart(data, height=height // len(columns), width=width // len(columns))
.mark_point(size=1 / len(columns), opacity=opacity)
.encode(
alt.X(alt.repeat("column"), type="quantitative"),
alt.Y(alt.repeat("row"), type="quantitative"),
**opt_args
)
.repeat(row=columns, column=columns)
)
source: alt.Chart, subset: List[str], name: str, loess=True
) -> alt.Chart:
chart = source.transform_filter(
alt.FieldOneOfPredicate(field="airline", oneOf=subset)
)
highlight = alt.selection(
type="single", nearest=True, on="mouseover", fields=["airline"]
)
points = (
chart.mark_point()
.encode(
x="day",
y=alt.Y("rate", title="# of flights (normalized)"),
color=alt.Color("airline", legend=alt.Legend(title=name)),
tooltip=["day", "airline", "count"],
opacity=alt.value(0.3),
)
.add_selection(highlight)
)
lines = chart.mark_line().encode(
x="day",
y="rate",
color="airline",
size=alt.condition(~highlight, alt.value(1), alt.value(3)),
)
if loess:
lines = lines.transform_loess(
"day", "rate", groupby=["airline"], bandwidth=0.2
for correctness in data["counts"]:
for value, count in data["counts"][correctness]:
sorted_dict[value] += count
sorted_list = sorted(list(sorted_dict), key=lambda x: sorted_dict[x], reverse=True)
dtype = 'Q' if not is_discrete else 'N'
bin = alt.Bin(maxbins=10, extent=data["domain"]) if not is_discrete else None
compute_domain = []
stack = "normalize" if normalize else "zero"
for correctness in data["counts"]:
for value, count in data["counts"][correctness]:
if sorted_list and sorted_list.index(value) >= 15:
continue
compute_domain.append({self.name: value, "count": count, "correctness": correctness})
df = pd.DataFrame(compute_domain)
chart = alt.Chart(df).mark_bar().encode(
y=alt.Y('count:Q', stack=stack),
x=alt.X(f'{self.name}:{dtype}', bin=bin),
color=alt.Color('correctness:N', scale=alt.Scale(domain=["correct", "incorrect"])),
tooltip=[f'{self.name}:{dtype}', 'count:Q', 'correctness:N']
).properties(height=100, title=f'{self.name} on {model}')#.configure_facet(spacing=5)#
return chart
{'country': 'United States', 'animal': 'pigs'},
{'country': 'United States', 'animal': 'pigs'},
{'country': 'United States', 'animal': 'pigs'},
{'country': 'United States', 'animal': 'sheep'},
{'country': 'United States', 'animal': 'sheep'},
{'country': 'United States', 'animal': 'sheep'},
{'country': 'United States', 'animal': 'sheep'},
{'country': 'United States', 'animal': 'sheep'},
{'country': 'United States', 'animal': 'sheep'},
{'country': 'United States', 'animal': 'sheep'}
])
alt.Chart(source).mark_text(size=45, baseline='middle').encode(
alt.X('x:O', axis=None),
alt.Y('animal:O', axis=None),
alt.Row('country:N', header=alt.Header(title='')),
alt.Text('emoji:N')
).transform_calculate(
emoji="{'cattle': '🐄', 'pigs': '🐖', 'sheep': '🐏'}[datum.animal]"
).transform_window(
x='rank()',
groupby=['country', 'animal']
).properties(width=550, height=140)
def _build_depth_histogram(data):
"""Build histogram with depth (DP)."""
width = 200
height = 200
title = 'Depth'
depth_data = _integer_counts_to_histogram(data)
depth_histogram = _placeholder_for_empty_chart(
'No entries in VCF with DP', width=width, height=height, title=title)
if not depth_data.empty:
# s = bin_start, e = bin_end, c = count
depth_histogram = alt.Chart(depth_data).mark_bar(color=BAR_COLOR_DEPTH) \
.encode(x=alt.X('s', title='Depth'),
x2='e',
y=alt.Y('c', title='Count', stack=True, axis=alt.Axis(format='s'))) \
.properties(width=width, height=height, title=title) \
.interactive(bind_y=False)
return depth_histogram
# Create sample plot chart Vega-Lite spec using Altair.
sample_chart = (
alt.Chart(
sample_metadata,
title="Samples",
background="#FFFFFF",
autosize=alt.AutoSizeParams(resize=True),
)
.mark_circle()
.encode(
alt.X(
default_metadata_col,
type="nominal",
axis=alt.Axis(labelAngle=-45),
),
alt.Y(
"qurro_balance:Q",
title="Current Log-Ratio",
type="quantitative",
),
color=alt.Color(default_metadata_col, type="nominal"),
tooltip=["Sample ID:N", "qurro_balance:Q"],
)
.configure_range(
ramp=alt.SchemeConfig(scheme="blues"),
category=alt.SchemeConfig(scheme="tableau10"),
)
.configure_axis(labelBound=True)
.interactive()
)
# Replace the "mark": "circle" definition with a more explicit one. This
def _build_base_change_chart(data):
"""Create the base change chart."""
width = 100
height = 200
placeholder_width = (4 * width) + 80 # 4 charts, plus constant spacing
title = 'Biallelic base changes from reference'
base_change_data = pd.DataFrame(data, columns=['ref', 'alt', 'count'])
base_change_chart = _placeholder_for_empty_chart(
'No biallelic SNPs', width=placeholder_width, height=height, title=title)
if not base_change_data.empty:
bars = alt.Chart(base_change_data).mark_bar().encode(
x=alt.X('alt', title='to alt'),
y=alt.Y('count', title='Count', axis=alt.Axis(format='s')),
color=alt.Color(
'alt',
legend=None,
sort=BASES,
scale=alt.Scale(scheme='category20', domain=BASES)),
tooltip=alt.Tooltip('count', format='.4s'))
labels = bars.mark_text(dy=-5, fontWeight='bold').encode(text='alt')
base_change_chart = (bars + labels) \
.properties(width=100, height=200) \
.facet(column=alt.Column('ref',
title=title,
sort=BASES))
return base_change_chart
width=800,
heights=[50,400],
line_size=5,
legend_mark_size=100):
top_height, bottom_height = heights
combined_df = combine_channel_df(experiments, channel_name)
combined_df.columns = [col.replace('_{}'.format(channel_name),'') for col in combined_df.columns]
nearest = alt.selection(type='single', nearest=True, on='mouseover',
fields=['x'], empty='none')
interval = alt.selection(type='interval', encodings=['x'])
legend_selection = alt.selection_multi(fields=['id'])
legend = alt.Chart().mark_point(filled=True, size=legend_mark_size).encode(
y=alt.Y('id:N'),
color=alt.condition(legend_selection, alt.Color('id:N', legend=None), alt.value('lightgray'))
).add_selection(
legend_selection
)
selectors = alt.Chart().mark_point().encode(
x='x:Q',
opacity=alt.value(0),
).add_selection(
nearest
)
top_view = alt.Chart(width=width, height=top_height).mark_line(size=line_size).encode(
x=alt.X('x:Q', title=None),
y=alt.Y('y:Q', scale=alt.Scale(zero=False), title=None),
color=alt.Color('id:N', legend=None),
source = pd.DataFrame({"x":x, "y":y, "yerr":yerr})
# the base chart
base = alt.Chart(source).transform_calculate(
ymin="datum.y-datum.yerr",
ymax="datum.y+datum.yerr"
)
# generate the points
points = base.mark_point(
filled=True,
size=50,
color='black'
).encode(
x=alt.X('x', scale=alt.Scale(domain=(0, 6))),
y=alt.Y('y', scale=alt.Scale(domain=(10, 11)))
)
# generate the error bars
errorbars = base.mark_errorbar().encode(
x="x",
y="ymin:Q",
y2="ymax:Q"
)
points + errorbars
# Generating Data
source = pd.DataFrame({
'Trial A': np.random.normal(0, 0.8, 1000),
'Trial B': np.random.normal(-2, 1, 1000),
'Trial C': np.random.normal(3, 2, 1000)
})
alt.Chart(source).transform_fold(
['Trial A', 'Trial B', 'Trial C'],
as_=['Experiment', 'Measurement']
).mark_area(
opacity=0.3,
interpolate='step'
).encode(
alt.X('Measurement:Q', bin=alt.Bin(maxbins=100)),
alt.Y('count()', stack=None),
alt.Color('Experiment:N')
)