Пример #1
0
def data_multiline():
    chart = Chart(data=data.df_stocks, height=HEIGHT,
                  width=WIDTH).mark_line().encode(
                      color='symbol:N',
                      x='date:T',
                      y='price:Q',
                  )
    return chart.to_json()
Пример #2
0
    def getAltair(self, params):
        cars = load_dataset('cars')

        c = Chart(cars).mark_point().encode(x='Horsepower',
                                            y='Miles_per_Gallon',
                                            color='Origin')

        return c.to_html()
Пример #3
0
def save_chart(chart: alt.Chart, file_path: str) -> None:
    base_name, ext = splitext(file_path)
    json_file, svg_file = base_name + ".json", base_name + ".svg"
    json_exists = True if exists(json_file) else False
    chart.savechart(json_file)
    with open(svg_file, 'wb') as svg_fp:
        sp.call(["vl2svg", json_file], stdout=svg_fp)
    if not json_exists:
        remove(json_file)
Пример #4
0
    def getAltair(self, params):
        cars = load_dataset('cars')

        c = Chart(cars).mark_point().encode(
            x='Horsepower',
            y='Miles_per_Gallon',
            color='Origin'
        )

        return c.to_html()
Пример #5
0
def make_images(image_dir, default_image, make_thumbnails=True):
    """Use nodejs to make images and (optionally) thumbnails"""

    can_save = Chart._png_output_available()
    if not can_save:
        warnings.warn('Node is not correctly configured: cannot save images.')

    if not os.path.exists(image_dir):
        os.makedirs(image_dir)

    # store hashes so that we know whether images need to be generated
    hash_file = os.path.join(image_dir, '_image_hashes.json')

    if os.path.exists(hash_file):
        with open(hash_file) as f:
            hashes = json.load(f)
    else:
        hashes = {}

    for example in iter_examples_with_metadata():
        filename = example['name'] + '.png'
        image_file = os.path.join(image_dir, filename)

        # check whether image already exists
        spec = example['spec']
        spec_hash = dict_hash(spec)
        hashes_match = not hashes.get(filename, '') == spec_hash

        if hashes_match or not os.path.exists(image_file) or hashes_match:
            if can_save:
                chart = Chart.from_dict(spec)
                try:
                    print('-> saving {0}'.format(image_file))
                    chart.savechart(image_file)
                except CalledProcessError:
                    warnings.warn('Node is not correctly configured: '
                                  'cannot save images.')
                    can_save = False
                    if not os.path.exists(image_file):
                        shutil.copyfile(default_image, image_file)
                else:
                    hashes[filename] = spec_hash
            elif not os.path.exists(image_file):
                shutil.copyfile(default_image, image_file)

        if make_thumbnails:
            params = example.get('galleryParameters', {})
            thumb_file = os.path.join(image_dir,
                                      example['name'] + '-thumb.png')
            create_thumbnail(image_file, thumb_file, **params)

    # Save hashes so we know whether we need to re-generate plots
    if hashes:
        with open(hash_file, 'w') as f:
            json.dump(hashes, f)
Пример #6
0
def make_images(image_dir, default_image, make_thumbnails=True):
    """Use nodejs to make images and (optionally) thumbnails"""

    can_save = Chart._png_output_available()
    if not can_save:
        warnings.warn('Node is not correctly configured: cannot save images.')

    if not os.path.exists(image_dir):
        os.makedirs(image_dir)

    # store hashes so that we know whether images need to be generated
    hash_file = os.path.join(image_dir, '_image_hashes.json')

    if os.path.exists(hash_file):
        with open(hash_file) as f:
            hashes = json.load(f)
    else:
        hashes = {}

    for example in iter_examples_with_metadata():
        filename = example['name'] + '.png'
        image_file = os.path.join(image_dir, filename)

        # check whether image already exists
        spec = example['spec']
        spec_hash = dict_hash(spec)
        hashes_match = not hashes.get(filename, '') == spec_hash

        if hashes_match or not os.path.exists(image_file) or hashes_match:
            if can_save:
                chart = Chart.from_dict(spec)
                try:
                    print('-> saving {0}'.format(image_file))
                    chart.savechart(image_file)
                except CalledProcessError:
                    warnings.warn('Node is not correctly configured: '
                                  'cannot save images.')
                    can_save = False
                    if not os.path.exists(image_file):
                        shutil.copyfile(default_image, image_file)
                else:
                    hashes[filename] = spec_hash
            elif not os.path.exists(image_file):
                shutil.copyfile(default_image, image_file)

        if make_thumbnails:
            params = example.get('galleryParameters', {})
            thumb_file = os.path.join(image_dir,
                                      example['name'] + '-thumb.png')
            create_thumbnail(image_file, thumb_file, **params)

    # Save hashes so we know whether we need to re-generate plots
    if hashes:
        with open(hash_file, 'w') as f:
            json.dump(hashes, f)
Пример #7
0
def extract_transform(chart: alt.Chart) -> alt.Chart:
    """Extract transforms within a chart specification."""
    chart = chart.copy()
    encoding_dict = chart.encoding.copy().to_dict(context={"data": chart.data})
    encoding, transform = _encoding_to_transform(encoding_dict)
    if transform:
        chart.encoding = alt.FacetedEncoding.from_dict(encoding)
        if chart.transform is alt.Undefined:
            chart.transform = []
        chart.transform.extend(alt.Transform.from_dict(t) for t in transform)
    return chart
Пример #8
0
def extract_transform(chart: alt.Chart) -> alt.Chart:
    """Extract transforms from encodings

    This takes a chart with transforms specified within encodings, and returns
    an equivalent chart with transforms specified separately in the ``transform``
    field.

    Parameters
    ----------
    chart : alt.Chart
        Input chart, which will not be modified

    Returns
    -------
    chart : alt.Chart
        A copy of the input chart with any encoding-specified transforms moved
        to the transforms-attribute

    Example
    -------
    >>> chart = alt.Chart('data.csv').mark_bar().encode(x='mean(x):Q', y='y:N')
    >>> new_chart = extract_transform(chart)
    >>> new_chart.transform
    [AggregateTransform({
      aggregate: [AggregatedFieldDef({
        as: FieldName('mean_x'),
        field: FieldName('x'),
        op: AggregateOp('mean')
      })],
      groupby: [FieldName('y')]
    })]
    >>> new_chart.encoding
    FacetedEncoding({
      x: PositionFieldDef({
        field: FieldName('mean_x'),
        title: 'Mean of x',
        type: StandardType('quantitative')
      }),
      y: PositionFieldDef({
        field: FieldName('y'),
        type: StandardType('nominal')
      })
    })
    """

    chart = chart.copy()
    encoding_dict = chart.encoding.copy().to_dict(context={"data": chart.data})
    encoding, transform = _encoding_to_transform(encoding_dict)
    if transform:
        chart.encoding = alt.FacetedEncoding.from_dict(encoding)
        if chart.transform is alt.Undefined:
            chart.transform = []
        chart.transform.extend(alt.Transform.from_dict(t) for t in transform)
    return chart
Пример #9
0
 def _get_altair_chart(self,
                       xfield,
                       yfield,
                       chart_type,
                       label,
                       opts={},
                       style={},
                       **kwargs):
     """
     Get an Altair chart object
     """
     encode = self.altair_encode
     chart = None
     if chart_type == "bar":
         chart = Chart(self.df).mark_bar(**style).encode(
             x=xfield, y=yfield, **encode).properties(**opts)
     elif chart_type == "circle":
         chart = Chart(self.df).mark_circle(**style).encode(
             x=xfield, y=yfield, **encode).properties(**opts)
     elif chart_type == "line":
         chart = Chart(self.df).mark_line(**style).encode(
             x=xfield, y=yfield, **encode).properties(**opts)
     elif chart_type == "hline":
         chart = self._altair_hline_(xfield, yfield, opts, style, encode)
     elif chart_type == "line_num":
         chart = self._altair_line_num_(xfield, yfield, opts, style, encode)
     elif chart_type == "bar_num":
         chart = self._altair_bar_num_(xfield, yfield, opts, style, encode)
     elif chart_type == "point_num":
         chart = self._altair_point_num_(xfield, yfield, opts, style,
                                         encode)
     elif chart_type == "point":
         chart = Chart(self.df).mark_point(**style).encode(
             x=xfield, y=yfield, **encode).properties(**opts)
     elif chart_type == "area":
         chart = Chart(self.df).mark_area(**style).encode(
             x=xfield, y=yfield, **encode).properties(**opts)
     elif chart_type == "heatmap":
         chart = Chart(self.df).mark_rect(**style).encode(
             x=xfield, y=yfield, **encode).properties(**opts)
     elif chart_type == "text":
         chart = Chart(self.df).mark_text(**style).encode(
             x=xfield, y=yfield, **encode).properties(**opts)
     elif chart_type == "square":
         chart = Chart(self.df).mark_square(**style).encode(
             x=xfield, y=yfield, **encode).properties(**opts)
     elif chart_type == "tick":
         chart = Chart(self.df).mark_tick(**style).encode(
             x=xfield, y=yfield, **encode).properties(**opts)
     elif chart_type == "rule":
         chart = Chart(self.df).mark_rule(**style).encode(
             x=xfield, y=yfield, **encode).properties(**opts)
     return chart
Пример #10
0
def salary_v_rating_scatter(df_chart, legend, separate=False):
    """
    Description:    Create an altair chart for a salary vs rating scatterplot. 
    :param df_chart:   Dataframe of job listings with both salaries and ratings that will be used to make chart object 
    :type df_chart:    pandas.DataFrame 
    :param legend:     How we want plot to be organized i.e. by industry
    :type legend:      str in cols
    :author: Jake Kim 
    """
    cols = [
        'Job_title', 'Company', 'State', 'City', 'Min_Salary', 'Max_Salary',
        'Job_Desc', 'Industry', 'Rating', 'Date_Posted', 'Valid_until',
        'Job_Type'
    ]
    assert isinstance(df_chart, pd.DataFrame)
    assert legend in cols

    avg_sal = list()
    for i in range(df_chart.shape[0]):
        assert df_chart.loc[i, 'Max_Salary'] > 0
        assert df_chart.loc[i, 'Min_Salary'] > 0
        avg_sal.append(
            (df_chart.loc[i, 'Max_Salary'] + df_chart.loc[i, 'Min_Salary']) /
            2)

    df_chart['Avg_Salary'] = avg_sal

    df_chart = Chart(df_chart)

    if separate:
        chart = df_chart.mark_point().encode(
            x=alt.X('Avg_Salary', title='Avg Salary (USD)'),
            y='Rating',
            column=legend,
            color=alt.Color(legend, scale=alt.Scale(scheme='dark2')))
    else:
        chart = df_chart.mark_point().encode(x=alt.X('Avg_Salary',
                                                     title='Avg Salary (USD)'),
                                             y='Rating',
                                             color=legend)

    chart = chart.properties(title='Salary vs. Company Ratings',
                             height=700,
                             width=900)
    chart = chart.configure_header(titleFontSize=18, labelFontSize=18)
    chart = chart.configure_axis(titleFontSize=18,
                                 labelFontSize=18,
                                 tickCount=10)
    chart = chart.configure_title(fontSize=18)
    chart = chart.configure_point(size=75)
    chart = chart.configure_legend(titleFontSize=18, labelFontSize=16)

    return chart
Пример #11
0
def data_bar_prison():
    county_data = read_county_from_db(session.get('current_state'),
                                      session.get('current_county'))

    # Create the chart
    chart = Chart(
        data=county_data, height=HEIGHT,
        width=WIDTH).mark_bar(color='#2f3142').encode(
            X('year:O', axis=Axis(title='Year')),
            Y('total_prison_pop',
              axis=Axis(title='Total Prison Population'))).properties(
                  title='Prison population in {}'.format(
                      session.get('current_county')))
    return chart.to_json()
Пример #12
0
def visualize(result, fileName):
    from altair import Chart, Color, Y, Scale

    #chart = LayeredChart(result)
    #chart += Chart().mark_line().encode(x='Search:O', y='Elapsed Time:Q')

    chart = Chart(result).mark_point().encode(x='Search:O',
                                              color='Problem:O',
                                              y=Y('Elapsed Time:Q',
                                                  scale=Scale(type='log')))
    #x='Search:O', color='Problem:O', y='Elapsed Time:Q')
    #    with open('out.html', 'w') as f:
    #       f.write(html)
    chart.savechart(fileName + ".l.svg")
Пример #13
0
def _plot_with_data_bar(source: alt.Chart,
                        line: alt.Chart,
                        type_: ValueType,
                        width: Optional[int] = None,
                        height: Optional[int] = None) -> alt.Chart:

    width = 800 if width is None else width
    height = 400 if height is None else height

    # TODO: Add validation that the x and y series names are correct

    # Create a selection that chooses the nearest point & selects based on x-value
    nearest = alt.selection(type='single', nearest=True, on='mouseover', fields=['days'],
                            empty='none')

    # Transparent selectors across the chart. This is what tells us
    # the x-value of the cursor
    selectors = alt.Chart(source).mark_point().encode(
        x='days:Q',
        opacity=alt.value(0),
    ).add_selection(
        nearest
    )

    # Draw points on the line, and highlight based on selection
    points = line.mark_point().encode(
        opacity=alt.condition(nearest, alt.value(1), alt.value(0))
    )

    # Draw text labels near the points, and highlight based on selection
    text = line.mark_text(align='left', dx=5, dy=10).encode(
        text=alt.condition(nearest, f'{type_.value}:Q', alt.value(' '), format=".0f")
    )

    # Draw a rule at the location of the selection
    rules = alt.Chart(source).mark_rule(color='gray').encode(
        x='days:Q',
    ).transform_filter(
        nearest
    )

    # Put the five layers into a chart and bind the data
    chart = alt.layer(
        line, selectors, points, rules, text
    ).properties(
        width=width, height=height
    )

    return chart
Пример #14
0
def pretrial_jail_chart():
    county_data = read_county_from_db(session.get('current_state'),
                                      session.get('current_county'))

    chart = Chart(data=county_data, height=HEIGHT, width=WIDTH).mark_line(
        color="#08080B",
        interpolate='step-after',
        line=True,
    ).encode(X('year:O', axis=Axis(title='Year')),
             Y('total_jail_pretrial', axis=Axis(title='Number of inmates')),
             tooltip=[
                 'year', 'total_jail_pretrial'
             ]).properties(title='Pre-trial jail population in {}'.format(
                 session.get('current_county'))).interactive()
    return chart.to_json()
Пример #15
0
 def data_line():
     chart = (Chart(data=sample_data.df_list, height=HEIGHT,
                    width=WIDTH).mark_line(color="green").encode(
                        X("name", axis=Axis(title="Sample")),
                        Y("data", axis=Axis(title="Value")),
                    ).interactive())
     return chart.to_json()
Пример #16
0
def airport_chart(source: alt.Chart, subset: List[str], name: str) -> alt.Chart:

    chart = source.transform_filter(
        alt.FieldOneOfPredicate(field="airport", oneOf=subset)
    )

    highlight = alt.selection(
        type="single", nearest=True, on="mouseover", fields=["airport"]
    )

    points = (
        chart.mark_point()
        .encode(
            x="day",
            y=alt.Y("count", title="# of departing flights"),
            color=alt.Color("airport", legend=alt.Legend(title=name)),
            tooltip=["day", "airport", "city", "count"],
            opacity=alt.value(0.5),
        )
        .add_selection(highlight)
    )

    lines = (
        chart.mark_line()
        .encode(
            x="day",
            y="count",
            color="airport",
            size=alt.condition(~highlight, alt.value(1), alt.value(3)),
        )
        .transform_loess("day", "count", groupby=["airport"], bandwidth=0.2)
    )

    return lines + points
Пример #17
0
def plot_sig_recon(sigs, recons, time=None):
    """
    sigs: channels x samples
    """
    long_dfs = []
    for sig_name, sig_array in zip(('sig', 'recon'), (sigs, recons)):
        df = pd.DataFrame(data=sig_array.T)
        if time is not None:
            sdf['time'] = time
        else:
            df['time'] = df.index
        df_long = pd.melt(df,
                          id_vars=('time', ),
                          var_name='signal',
                          value_name='magnitude')
        df_long['src'] = sig_name
        long_dfs.append(df_long)
    combined_dfs = pd.concat(long_dfs, ignore_index=True)
    combined_dfs.src = pd.Categorical(combined_dfs.src,
                                      categories=('sig', 'recon'),
                                      ordered=True)
    sig_chart = (Chart(combined_dfs).encode(
        X('time'), Y('magnitude'), Row('signal'),
        Column('src:O', scale=Scale(domain=('sig', 'recon'),
                                    type='ordinal'))).mark_point())
    return sig_chart
Пример #18
0
def populate_examples(num_examples=None, category=None, shuffle=False,
                      shuffle_seed=42, **kwargs):
    """Iterate through Altair examples and extract code"""

    examples = sorted(iter_examples_with_metadata(), key=itemgetter('name'))
    if category is not None:
        examples = [ex for ex in examples if ex['category'] == category]
    if shuffle:
        random.Random(shuffle_seed).shuffle(examples)
    if num_examples is not None:
        examples = examples[:num_examples]

    for prev_ex, example, next_ex in prev_this_next(examples):
        try:
            code = Chart.from_dict(example['spec']).to_altair()
        except Exception as e:
            warnings.warn('altair-gallery: example {0} produced an error:\n'
                          '{1}\n{2}'.format(example['name'], type(e), str(e)))
            code = '# (Altair JSON conversion failed).\nChart()'

        example['code'] = code

        if prev_ex:
            example['prev_ref'] = "gallery_{name}".format(**prev_ex)

        if next_ex:
            example['next_ref'] = "gallery_{name}".format(**next_ex)

        example['filename'] = '{0}.rst'.format(example['name'])
        example.update(kwargs)

    return examples
Пример #19
0
def populate_examples(**kwargs):
    """Iterate through Altair examples and extract code"""

    examples = list(iter_examples_with_metadata())

    for prev_ex, example, next_ex in prev_this_next(examples):
        try:
            code = Chart.from_dict(example['spec']).to_altair()
        except Exception as e:
            warnings.warn('altair-gallery: example {0} produced an error:\n'
                          '{1}\n{2}'.format(example['name'], type(e), str(e)))
            code = '# (Altair JSON conversion failed).\nChart()'

        example['code'] = code

        if prev_ex:
            example['prev_ref'] = "gallery_{name}".format(**prev_ex)

        if next_ex:
            example['next_ref'] = "gallery_{name}".format(**next_ex)

        example['filename'] = '{0}.rst'.format(example['name'])
        example.update(kwargs)

    return examples
Пример #20
0
def populate_examples(num_examples=None,
                      category=None,
                      shuffle=False,
                      shuffle_seed=42,
                      **kwargs):
    """Iterate through Altair examples and extract code"""

    examples = sorted(iter_examples_with_metadata(), key=itemgetter('name'))
    if category is not None:
        examples = [ex for ex in examples if ex['category'] == category]
    if shuffle:
        random.Random(shuffle_seed).shuffle(examples)
    if num_examples is not None:
        examples = examples[:num_examples]

    for prev_ex, example, next_ex in prev_this_next(examples):
        try:
            code = Chart.from_dict(example['spec']).to_altair()
        except Exception as e:
            warnings.warn('altair-gallery: example {0} produced an error:\n'
                          '{1}\n{2}'.format(example['name'], type(e), str(e)))
            code = '# (Altair JSON conversion failed).\nChart()'

        example['code'] = code

        if prev_ex:
            example['prev_ref'] = "gallery_{name}".format(**prev_ex)

        if next_ex:
            example['next_ref'] = "gallery_{name}".format(**next_ex)

        example['filename'] = '{0}.rst'.format(example['name'])
        example.update(kwargs)

    return examples
Пример #21
0
def transform_chart(
    chart: alt.Chart, extract_encoding_transforms: bool = True
) -> alt.Chart:
    """Return a chart with the transformed data

    Parameters
    ----------
    chart : alt.Chart
        The chart instance from which the data and transform
        will be extracted.
    extract_encoding_transforms : bool
        If True (default), then also extract transforms from encodings.

    Returns
    -------
    chart_out : alt.Chart
        A copy of the input chart with the transformed data.

    Example
    -------
    >>> import pandas as pd
    >>> data = pd.DataFrame({'x': range(5), 'y': list('ABCAB')})
    >>> chart = alt.Chart(data).mark_bar().encode(x='sum(x)', y='y')
    >>> new_chart = transform_chart(chart)
    >>> new_chart.data
       y  sum_x
    0  A      3
    1  B      5
    2  C      2
    >>> new_chart.encoding
    FacetedEncoding({
      x: PositionFieldDef({
        field: FieldName('sum_x'),
        title: 'Sum of x',
        type: StandardType('quantitative')
      }),
      y: PositionFieldDef({
        field: FieldName('y'),
        type: StandardType('nominal')
      })
    })
    """
    if extract_encoding_transforms:
        chart = extract_transform(chart)
    chart = chart.properties(data=extract_data(chart, apply_encoding_transforms=False))
    chart.transform = alt.Undefined
    return chart
Пример #22
0
    def electricity_demo():

        chart = (Chart(
            data=electricity, height=700,
            width=700).mark_area().encode(x="year:T",
                                          y="net_generation:Q",
                                          color="source:N").interactive())
        return chart.to_json()
Пример #23
0
 def stocks():
     chart = (Chart(data=sample_data.df_stocks, height=HEIGHT,
                    width=WIDTH).mark_bar().encode(
                        color="symbol:N",
                        x="date:T",
                        y="price:Q",
                    ).interactive())
     return chart.to_json()
Пример #24
0
    def cars_demo():

        chart = (Chart(data=cars, height=700, width=700).mark_point().encode(
            x="Horsepower",
            y="Miles_per_Gallon",
            color="Origin",
        ).interactive())
        return chart.to_json()
Пример #25
0
def transform_chart(chart: alt.Chart) -> alt.Chart:
    """Return a chart with the transformed data

    Parameters
    ----------
    chart : alt.Chart
        The chart instance from which the data and transform
        will be extracted.

    Returns
    -------
    chart_out : alt.Chart
        A copy of the input chart with the transformed data.
    """
    chart = chart.properties(data=extract_data(chart))
    chart.transform = alt.Undefined
    return chart
Пример #26
0
 def data_waterfall():
     chart = (Chart(
         data=sample_data.df_water,
         width=WIDTH,
     ).mark_bar(color="gray").encode(
         X("Name", axis=Axis(title="Sample")),
         Y("Value", axis=Axis(title="Value")),
     ).interactive())
     return chart.to_json()
Пример #27
0
    def barley_yield_demo():

        chart = (Chart(data=barley_yield, height=400,
                       width=150).mark_bar().encode(
                           x="year:O",
                           y="sum(yield):Q",
                           color="year:N",
                           column="site:N").interactive())
        return chart.to_json()
Пример #28
0
def data_bar_prison():
    county_data = read_county_from_db(session.get(
        'current_state'), session.get('current_county'))

    # Create a label for the prison population to be included in the chart.
    # Result of lambda is a float, thus the slice notation is used
    county_data['total_prison_pop_label'] = county_data['total_prison_pop'].apply(lambda x: "{:,}".format(x)[:-2])

    # Create the chart
    chart = Chart(data=county_data, height=HEIGHT, width=WIDTH).mark_bar(color='#2f3142').encode(
        X('year:O', axis=Axis(title='Year')),
        Y('total_prison_pop', axis=Axis(title='Total Prison Population')),
        tooltip=[alt.Tooltip('year', title='Year'), alt.Tooltip(
            'total_prison_pop_label', title='Total prison population')]
    ).properties(
    title='Prison population in {}'.format(session.get('current_county'))
    ).interactive()

    return chart.to_json()
Пример #29
0
def df_to_vega_lite(df, path=None):
    """
    Export a pandas.DataFrame to a vega-lite data JSON.
    
    Params
    ------
    df : pandas.DataFrame
        dataframe to convert to JSON
    path : None or str
        if None, return the JSON str. Else write JSON to the file specified by
        path.
    Source: https://github.com/dhimmel/biorxiv-licenses/
    """
    chart = Chart(data=df)
    data = chart.to_dict()['data']['values']
    if path is None:
        return json.dumps(data, **json_dump_kwargs)
    with open(path, 'w') as write_file:
        json.dump(data, write_file, **json_dump_kwargs)
Пример #30
0
def source_vs_hour_chart(
    base: alt.Chart, sensor_unit: str, max_absolute_error: float, faceted: bool = False
) -> Union[alt.Chart, alt.FacetChart]:
    hd_chart = (
        base.mark_rect()
        .transform_joinaggregate(
            on_the_fly_mae="mean(mae)",
            on_the_fly_reference="mean(reference_value)",
            groupby=["event_start", "source"],
        )
        .transform_calculate(accuracy=alt.datum.on_the_fly_mae)
        .encode(
            x=alt.X(
                "event_start:O",
                timeUnit="hours",
                axis=alt.Axis(domain=False, ticks=False, labelAngle=0),
                scale=alt.Scale(domain=list(range(24))),
                title="Hour of day",  # "UTC hour of day"
            ),
            color=alt.condition(
                selectors.time_selection_brush,
                alt.Color(
                    "accuracy:Q",
                    scale=alt.Scale(
                        domain=(max_absolute_error, 0), scheme="redyellowgreen"
                    ),
                    title="Error",
                ),
                alt.value(selectors.idle_color),
            ),
            tooltip=[
                alt.Tooltip("event_start:T", timeUnit="hours", title="Hour of day"),
                alt.Tooltip(
                    "accuracy:Q",
                    title="Mean absolute error (%s)" % sensor_unit,
                    format=".2f",
                ),
            ],
        )
    )
    if faceted:
        hd_chart = hd_chart.facet(
            row=alt.Row("source:O", title=None, header=alt.Header(labelAngle=0))
        )
    else:
        hd_chart = hd_chart.encode(
            y=alt.Y(
                "source:O",
                axis=alt.Axis(domain=False, ticks=False, labelAngle=0, labelPadding=5),
                title=None,
            )
        )
    return hd_chart.properties(
        title=alt.TitleParams("Model performance given a time of day", anchor="middle")
    )
Пример #31
0
def covid_demo():

    interval = selection_interval()

    circle = Chart(selected_data).mark_circle().encode(
        x='monthdate(Date):O',
        y='Country/Region',
        color=condition(interval, 'Country/Region', value('lightgray')),
        size=Size('New cases:Q',
                  scale=Scale(range=[0, 3000]),
                  legend=Legend(title='Daily new cases'))).properties(
                      width=1000, height=300, selection=interval)

    bars = Chart(selected_data).mark_bar().encode(
        y='Country/Region', color='Country/Region',
        x='sum(New cases):Q').properties(width=1000).transform_filter(interval)

    chart = circle & bars

    return chart.to_json()
Пример #32
0
def time_window_selector(base: alt.Chart, interpolate: bool) -> alt.LayerChart:
    if interpolate is True:
        tws = base.mark_area(interpolate="monotone")
    else:
        tws = base.mark_bar().encode(x2=alt.X2("event_end:T"))
    tws = tws.encode(
        x=alt.X("event_start", title=""),
        y=alt.Y(
            "reference_value",
            title="",
            axis=alt.Axis(values=[], domain=False, ticks=False),
        ),
        color=alt.ColorValue(idle_color),
        tooltip=alt.TooltipValue("Click and drag to select time window"),
    ).properties(height=30, title="Select time window")
    tws = tws.add_selection(time_selection_brush) + tws.transform_filter(
        time_selection_brush).encode(
            color=alt.condition(time_selection_brush, alt.ColorValue(
                "#c21431"), alt.ColorValue(idle_color)))
    return tws
Пример #33
0
def draw_map():
    fips=session.get('fips')
    state_id=int(str(fips)[0:-3])

    states_topo=alt.topo_feature(
        'https://raw.githubusercontent.com/vega/vega-datasets/master/data/us-10m.json', feature='states')
    counties_topo=alt.topo_feature(
        'https://raw.githubusercontent.com/vega/vega-datasets/master/data/us-10m.json', feature='counties')

    state_map=Chart(data=states_topo, height=HEIGHT, width=WIDTH).mark_geoshape(
                fill='#827f7f',
                stroke='white'
            ).transform_filter((alt.datum.id == state_id))

    county_map=Chart(data=counties_topo, height=HEIGHT, width=WIDTH).mark_geoshape(
                fill='red',
                stroke='white'
            ).transform_filter((alt.datum.id == int(fips)))

    chart=alt.layer(state_map, county_map).configure_view(strokeWidth=0)

    return chart.to_json()
Пример #34
0
def make_images(image_dir, default_image, make_thumbnails=True):
    """Use nodejs to make images and (optionally) thumbnails"""

    can_save = savechart_available()
    if not can_save:
        warnings.warn('Node is not correctly configured: cannot save images.')

    if not os.path.exists(image_dir):
        os.makedirs(image_dir)

    # store hashes so that we know whether images need to be generated
    hash_file = os.path.join(image_dir, '_image_hashes.json')

    if os.path.exists(hash_file):
        with open(hash_file) as f:
            hashes = json.load(f)
    else:
        hashes = {}

    for example in iter_examples_with_metadata():
        filename = example['name'] + '.png'
        image_file = os.path.join(image_dir, filename)

        # check whether image already exists
        spec = example['spec']
        spec_hash = dict_hash(spec)
        if hashes.get(filename, '') == spec_hash:
            continue

        if can_save:
            chart = Chart.from_dict(spec)
            try:
                print('-> saving {0}'.format(image_file))
                savechart(chart, image_file)
            except NodeExecError:
                warnings.warn('Node is not correctly configured: cannot save images.')
                can_save = False
                if not os.path.exists(image_file):
                    shutil.copyfile(default_image, image_file)
            else:
                hashes[filename] = spec_hash
        elif not os.path.exists(image_file):
            shutil.copyfile(default_image, image_file)

        if make_thumbnails:
            convert_pct = lambda x: 0.01 * int(x.strip('%'))
            params = example.get('galleryParameters', {})

            zoom = params.get('backgroundSize', '100%')
            if zoom == 'contains': zoom = '100%'
            zoom = convert_pct(zoom)

            #position = params.get('backgroundPosition', '0% 0%')
            #if position == 'left': position = '0% 0%'
            #xoffset, yoffset = map(convert_pct, position.split())

            thumb_file = os.path.join(image_dir, example['name'] + '-thumb.png')
            create_thumbnail(image_file, thumb_file, zoom=zoom)

    # Save hashes so we know whether we need to re-generate plots
    if hashes:
        with open(hash_file, 'w') as f:
            json.dump(hashes, f)