Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def plot(self, show=True):
""" Assumes nothing in self.settings is None (i.e., there are no keys
in settings such that settings[key] == None"""
kwargs = {e['encoding']: _get_plot_command(e)
for e in self.settings['encodings']}
mark_opts = {k: v for k, v in self.settings['mark'].items()}
mark = mark_opts.pop('mark')
Chart_mark = getattr(altair.Chart(self.df), mark)
self.chart = Chart_mark(**mark_opts).encode(**kwargs)
if show and self.show:
clear_output()
display(self.chart)
# category: scatter plots
import numpy as np
import pandas as pd
import altair as alt
# Generate some random data
rng = np.random.RandomState(1)
x = rng.rand(40) ** 2
y = 10 - 1.0 / (x + 0.1) + rng.randn(40)
source = pd.DataFrame({"x": x, "y": y})
# Define the degree of the polynomial fits
degree_list = [1, 3, 5]
base = alt.Chart(source).mark_circle(color="black").encode(
alt.X("x"), alt.Y("y")
)
polynomial_fit = [
base.transform_regression(
"x", "y", method="poly", order=order, as_=["x", str(order)]
)
.mark_line()
.transform_fold([str(order)], as_=["degree", "y"])
.encode(alt.Color("degree:N"))
for order in degree_list
]
alt.layer(base, *polynomial_fit)
chart_pixel_width = (len(values) / 60.0) * 1000
if chart_pixel_width < 200:
chart_pixel_width = 200
chart_container_col_width = round((len(values) / 60.0) * 12)
if chart_container_col_width < 4:
chart_container_col_width = 4
elif chart_container_col_width > 8:
chart_container_col_width = 12
elif chart_container_col_width > 4:
chart_container_col_width = 8
mark_bar_args = {}
if len(values) == 1:
mark_bar_args["size"] = 20
bars = alt.Chart(df).mark_bar(**mark_bar_args).encode(
y='count:Q',
x="value:O",
tooltip=["value", "count"]
).properties(height=400, width=chart_pixel_width, autosize="fit")
chart = bars.to_json()
new_block = RenderedComponentContent(**{
"content_block_type": "graph",
"header":
{
"template": "Value Counts",
"tooltip": {
"content": "expect_column_distinct_values_to_be_in_set"
}
},
def stripplot(
data=None, columns=None, group_by=None, color=None, opacity=1, height=600, width=800
):
"""Generate a stripplot."""
data, key, value = multivariate_preprocess(data, columns, group_by)
enc_args = dict()
if color is not None:
enc_args["color"] = color
return (
alt.Chart(data, height=height, width=width)
.mark_tick(opacity=opacity, thickness=2)
.encode(x=key + ":N", y=value, **enc_args)
)
"""
Atmospheric CO2 Concentration
-----------------------------
This example is a fully developed line chart that uses a window transformation.
It was inspired by `Gregor Aisch's work at datawrapper
`_.
"""
# category: case studies
import altair as alt
from vega_datasets import data
source = data.co2_concentration.url
base = alt.Chart(
source,
title="Carbon Dioxide in the Atmosphere"
).transform_calculate(
year="year(datum.Date)"
).transform_calculate(
decade="floor(datum.year / 10)"
).transform_calculate(
scaled_date="(datum.year % 10) + (month(datum.Date)/12)"
).transform_window(
first_date='first_value(scaled_date)',
last_date='last_value(scaled_date)',
sort=[{"field": "scaled_date", "order": "ascending"}],
groupby=['decade'],
frame=[None, None]
).transform_calculate(
end="datum.first_date === datum.scaled_date ? 'first' : datum.last_date === datum.scaled_date ? 'last' : null"
def _placeholder_for_empty_chart(text_to_display,
width=100,
height=100,
title=''):
chart = alt.Chart({'values': [{'placeholder': text_to_display}]}) \
.mark_text(size=14).encode(text='placeholder:N') \
.properties(width=width, height=height, title=title)
return chart
Such a chart can be created in Altair by first transforming the data into a
suitable representation.
This example shows a modified parallel coordinates chart with the Iris dataset,
where the y-axis shows the value after min-max rather than the raw value. It's a
simplified Altair version of `the VegaLite version `_
"""
# category: other charts
import altair as alt
from vega_datasets import data
from altair import datum
source = data.iris()
alt.Chart(source).transform_window(
index='count()'
).transform_fold(
['petalLength', 'petalWidth', 'sepalLength', 'sepalWidth']
).transform_joinaggregate(
min='min(value)',
max='max(value)',
groupby=['key']
).transform_calculate(
minmax_value=(datum.value-datum.min)/(datum.max-datum.min),
mid=(datum.min+datum.max)/2
).mark_line().encode(
x='key:N',
y='minmax_value:Q',
color='species:N',
detail='index:N',
opacity=alt.value(0.5)
# US states background
background = alt.Chart(states).mark_geoshape(
fill='lightgray',
stroke='white'
).properties(
title='US State Capitols',
width=700,
height=400
).project('albersUsa')
# Points and text
hover = alt.selection(type='single', on='mouseover', nearest=True,
fields=['lat', 'lon'])
base = alt.Chart(capitals).encode(
longitude='lon:Q',
latitude='lat:Q'
)
text = base.mark_text(dy=-5, align='right').encode(
alt.Text('city', type='nominal'),
opacity=alt.condition(~hover, alt.value(0), alt.value(1))
)
points = base.mark_point().encode(
color=alt.value('black'),
size=alt.condition(~hover, alt.value(30), alt.value(100))
).add_selection(hover)
background + points + text
# category: interactive charts
import altair as alt
import pandas as pd
import numpy as np
np.random.seed(42)
source = pd.DataFrame(np.cumsum(np.random.randn(100, 3), 0).round(2),
columns=['A', 'B', 'C'], index=pd.RangeIndex(100, name='x'))
source = source.reset_index().melt('x', var_name='category', value_name='y')
# Create a selection that chooses the nearest point & selects based on x-value
nearest = alt.selection(type='single', nearest=True, on='mouseover',
fields=['x'], empty='none')
# The basic line
line = alt.Chart().mark_line(interpolate='basis').encode(
x='x:Q',
y='y:Q',
color='category:N'
)
# Transparent selectors across the chart. This is what tells us
# the x-value of the cursor
selectors = alt.Chart().mark_point().encode(
x='x:Q',
opacity=alt.value(0),
).add_selection(
nearest
)
# Draw points on the line, and highlight based on selection
points = line.mark_point().encode(
if plot_type == "scatter":
df = df.reset_index()
sort_order = sort_helper(sort_x, df[magic_fields[haxis]].tolist())
alt_kwargs = dict(
x=alt.X(magic_fields[haxis], axis=alt.Axis(title=xlabel), sort=sort_order),
y=alt.Y(magic_fields[vaxis], axis=alt.Axis(title=ylabel)),
tooltip=["Label", "{}:Q".format(magic_fields[vaxis])],
href="url:N",
url="https://app.onecodex.com/classification/" + alt.datum.classification_id,
)
chart = (
alt.Chart(df)
.transform_calculate(url=alt_kwargs.pop("url"))
.mark_circle()
.encode(**alt_kwargs)
)
elif plot_type == PlotType.BoxPlot:
if sort_x:
raise OneCodexException("Must not specify sort_x when plot_type is boxplot")
# See the following issue in case this gets fixed in altair:
# https://github.com/altair-viz/altair/issues/2144
if (df.groupby(magic_fields[haxis]).size() < 2).any():
warnings.warn(
"There is at least one sample group consisting of only a single sample. Groups "
"of size 1 may not have their boxes displayed in the plot.",
PlottingWarning,