コード例 #1
0
ファイル: _del_vega.py プロジェクト: mindatasleep/oura
import altair as alt
from vega_datasets import data

source = data.cars()

input_dropdown = alt.binding_select(options=['Europe', 'Japan', 'USA'])
selection = alt.selection_single(fields=['Origin'],
                                 bind=input_dropdown,
                                 name='Country of ')
color = alt.condition(selection, alt.Color('Origin:N', legend=None),
                      alt.value('lightgray'))

# alt.Chart(source).mark_point().encode(
#     x='Horsepower:Q',
#     y='Miles_per_Gallon:Q',
#     color=color,
#     tooltip='Name:N'
# ).add_selection(
#     selection
# )

vega = alt.Chart(source).mark_circle(size=60).encode(
    x='Horsepower',
    y='Miles_per_Gallon',
    color=color,
).add_selection(selection)

vega.save('a.html')
コード例 #2
0
import altair as alt
import pandas as pd
from altair import Color, Scale

df = pd.read_csv('/Users/saranking/weight-height.csv')

alt.data_transformers.enable('default', max_rows=None)

chart = alt.Chart(df).mark_point().encode(
    Color('Gender:N',
          scale=Scale(domain=['Male', 'Female'], range=['#1f77b4',
                                                        '#e377c2'])),
    x=alt.X('Height_in:Q', scale=alt.Scale(zero=False)),
    y=alt.Y('Weight_lbs:Q', scale=alt.Scale(zero=False)),
    tooltip=['Height_in', 'Weight_lbs', 'Gender'],
    opacity=alt.value(0.45)).properties(background='white')

chart
chart.save('scatterPlot.png', scale_factor=2.0)
コード例 #3
0
ファイル: utils.py プロジェクト: isabella232/matched_markets
def plot_iroas_over_time(iroas_df: pd.DataFrame,
                         experiment_dates: pd.DataFrame,
                         cooldown_date: pd.DataFrame):
    """Returns a chart of the iROAS estimate over time with confidence bands.

  This function provides a visualization of the evolution of the iROAS estimate
  over the duration of the experiment and cooldown, together with confidence
  bands.

  Args:
    iroas_df: a dataframe with columns: date, lower, mean, upper
    experiment_dates: dataframe with columns (date, color) which contains two
      dates for each period (start, end), and the column color is the label
      used in the chart to refer to the corresponding period, e.g. "Experiment
      period" or "Pretes period".
    cooldown_date: dataframe with column (date, color) with only one entry,
      where date indicates the last day in the cooldown period, and color is the
      label used in the plot legend, e.g. "End of cooldown period".

  Returns:
    iroas_chart: Chart containing the plot.
  """
    iroas_base = alt.Chart(iroas_df).mark_line().encode(
        x=alt.X('date:T', axis=alt.Axis(title='', format=('%b %e'))))

    iroas_selection = alt.selection_single(fields=['date'],
                                           nearest=True,
                                           on='mouseover',
                                           empty='none',
                                           clear='mouseout')

    iroas_lines = iroas_base.mark_line().encode(
        y=alt.Y('mean:Q', axis=alt.Axis(title=' ', format='.1')))

    iroas_points = iroas_lines.mark_point().transform_filter(iroas_selection)

    iroas_rule1 = iroas_base.mark_rule().encode(
        tooltip=['date:T', 'mean:Q', 'lower:Q', 'upper:Q'])

    iroas_rule = iroas_rule1.encode(
        opacity=alt.condition(iroas_selection, alt.value(0.3), alt.value(
            0))).add_selection(iroas_selection)

    iroas_ci_bands_rule = alt.Chart(iroas_df).mark_area(color='gray').encode(
        alt.X('date:T'), y='lower:Q', y2='upper:Q', opacity=alt.value(0.5))

    date_rule = alt.Chart(experiment_dates[
        experiment_dates['color'] == 'Experiment period']).mark_rule(
            strokeWidth=2).encode(x='date:T',
                                  color=alt.Color('color',
                                                  scale=alt.Scale(domain=[
                                                      'Experiment period',
                                                      'End of cooldown period',
                                                      'iROAS estimate'
                                                  ],
                                                                  range=[
                                                                      'black',
                                                                      'black',
                                                                      '#1f77b4'
                                                                  ])))
    cooldown_date_rule = alt.Chart(cooldown_date).mark_rule(
        strokeWidth=2, strokeDash=[5,
                                   2], color='black').encode(x='date:T',
                                                             color='color:N')
    # Compile chart
    iroas_chart = alt.layer(iroas_lines, iroas_rule, iroas_points, date_rule,
                            cooldown_date_rule, iroas_ci_bands_rule)

    return iroas_chart
コード例 #4
0
import altair as alt
from vega_datasets import data

source = data.cars()

# Configure common options
base = alt.Chart(source)
scale = alt.Scale(paddingInner=0)

# Configure heatmap
heatmap = base.mark_rect().encode(
    alt.X('Cylinders:O', scale=scale),
    alt.Y('Origin:O', scale=scale),
    color='count()'
)

# Configure text
text = base.mark_text(baseline='middle').encode(
    x='Cylinders:O',
    y='Origin:O',
    text='count()',
    color=alt.condition(
        alt.datum['count_*'] > 100,
        alt.value('black'),
        alt.value('white')
    )
)

# Draw the chart
heatmap + text
コード例 #5
0
ファイル: multiline_tooltip.py プロジェクト: mcleonard/tufty
# 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(
    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=-5).encode(
    text=alt.condition(nearest, 'y:Q', alt.value(' '))
)

# Draw a rule at the location of the selection
コード例 #6
0
# the base chart
base = alt.Chart(source)

# generate the points
points = base.mark_point(filled=True, size=50).encode(
    x=alt.X(
        "x",
        scale=alt.Scale(domain=(0,6)),
        axis=alt.Axis(title='x')
    ),
    y=alt.Y(
        'y',
        scale=alt.Scale(zero=False, domain=(10, 11)),
        axis=alt.Axis(title="y")
    ),
    color=alt.value('black')
)

# generate the error bars
errorbars = base.mark_rule().encode(
    x="x",
    y="ymin:Q",
    y2="ymax:Q"
).transform_calculate(
    ymin="datum.y-datum.yerr",
    ymax="datum.y+datum.yerr"
)

points + errorbars
コード例 #7
0
"""
Bar Chart with Negative Values
==============================
This example shows a bar chart with both positive and negative values.
"""
# category: bar charts
import altair as alt
from vega_datasets import data

source = data.us_employment()

alt.Chart(source).mark_bar().encode(
    x="month:T",
    y="nonfarm_change:Q",
    color=alt.condition(
        alt.datum.nonfarm_change > 0,
        alt.value("steelblue"),  # The positive color
        alt.value("orange")  # The negative color
    )
).properties(width=600)
コード例 #8
0
ファイル: postsim_nuc.py プロジェクト: elissasoroj/shadie
    def summary(self):
        "View selection coefficient distributions and mutation type counts"
        #calculate number of neutral mutations
        neutral = 0
        for mut in self.tscoal.mutations():
            if mut.derived_state == '1':
                neutral += 1

        #record fitness coefficients
        coeffs = []
        for mut in self.tscoal.mutations():
            mut_list = mut.metadata["mutation_list"]
            for i in mut_list:
                coeffs.append(i["selection_coeff"])

        #altair plot of mutation distrbutions
        mean = sum(coeffs) / len(coeffs)
        dist = np.histogram(coeffs, 50)
        source = pd.DataFrame(
            {
                'counts': dist[0],
                'values': dist[1][:-1],
                'mean': mean
            },
            columns=['values', 'counts', 'mean'])
        base = alt.Chart(source)

        histo = base.mark_bar(
            opacity=0.5, color=alt.ColorName("cornflowerblue")).encode(
                alt.X('values:Q', axis=alt.Axis(title='Fitness Effect')),
                alt.Y('counts:Q'),
                color=alt.condition(
                    alt.datum.values > 0,
                    alt.value("lightseagreen"),  # The positive color
                    alt.value("indianred")  # The negative color
                ),
                tooltip=[
                    alt.Tooltip('values', title='Fitness Effect'),
                ])
        mean = base.mark_rule(color=alt.ColorName("goldenrod"),
                              opacity=0.4).encode(
                                  x='mean:Q',
                                  size=alt.value(3),
                                  tooltip=[
                                      alt.Tooltip('mean(values)',
                                                  title='Mean Fitness Effect'),
                                  ])

        print("Note: this plot does not contain neutral mutations overlaid "
              "with `msprime`.")
        IPython.display.display_html(histo + mean)
        print(
            f"Total mutations: {self.tscoal.num_mutations}\n"
            f"Neutral mutations: {neutral}\n"
            f"Non-neutral mutations: {self.tscoal.num_mutations - neutral}\n")

        #static toyplot
        print("Mutation positions along chromosome:")
        chrom = Chromosome(genome=self.genome)
        chrom.toyplot()
        self.rectangles = chrom.rectangles

        canvas = toyplot.Canvas(width=2500, height=200)
        axes = canvas.cartesian()
        axes.show = False

        #draw the rectangles
        for index, row in self.rectangles.iterrows():
            axes.rectangle(
                row['x1'],
                row['x2'],
                row['y1'],
                row['y2'],
                color=row['color'],
                style={"opacity": 0.6},
            )
        #draw the positions
        lines = axes.vlines(self.positions,
                            style={
                                "stroke": "blue",
                                "stroke-width": 2
                            })
コード例 #9
0
borough boundaries. It is based on the vega-lite example at
https://vega.github.io/vega-lite/examples/geo_layer_line_london.html.
"""
# category: case studies
import altair as alt
from vega_datasets import data

boroughs = alt.topo_feature(data.londonBoroughs.url, 'boroughs')
tubelines = alt.topo_feature(data.londonTubeLines.url, 'line')
centroids = data.londonCentroids.url

background = alt.Chart(boroughs).mark_geoshape(
    stroke='white',
    strokeWidth=2
).encode(
    color=alt.value('#eee'),
).properties(
    width=700,
    height=500
)

labels = alt.Chart(centroids).mark_text().encode(
    longitude='cx:Q',
    latitude='cy:Q',
    text='bLabel:N',
    size=alt.value(8),
    opacity=alt.value(0.6)
).transform_calculate(
    "bLabel", "indexof (datum.name,' ') > 0  ? substring(datum.name,0,indexof(datum.name, ' ')) : datum.name"
)
コード例 #10
0
ファイル: graphs.py プロジェクト: rubens36/covid19
def generate_countries_map(data: pd.DataFrame,
                           date,
                           interactive: bool = False,
                           width: int = 600,
                           height: int = 600,
                           log_scale: bool = True) -> alt.Chart:

    fechas = data['fecha'].apply(lambda x: x.date())
    data = data[(fechas == date)]

    scale = alt.Scale(type='log', scheme='teals') if log_scale else alt.Scale(
        type='linear', scheme='teals')
    url_country_name = 'https://raw.githubusercontent.com/alisle/world-110m-country-codes/master/world-110m-country-codes.json'

    country_names = pd.read_json(url_country_name).drop('name', axis=1)
    country_data = get_country_data()
    country_data = country_names.join((country_data.set_index('code')),
                                      on='code')

    data = pd.merge(left=country_data,
                    right=data,
                    left_on='name',
                    right_on='pais').dropna()
    data = data.astype({'id': int, 'casos': int})

    sphere = alt.sphere()
    graticule = alt.graticule()
    source = alt.topo_feature(vd.data.world_110m.url, 'countries')

    sphere_chart = alt.Chart(
        sphere,
        title='Ubicación de los casos confirmados por país').mark_geoshape(
            fill='lightblue')
    graticule_chart = alt.Chart(graticule).mark_geoshape(stroke='white',
                                                         strokeWidth=0.5)
    countries_chart = (alt.Chart(source).mark_geoshape().encode(
        color=alt.Color('casos:Q', title='Casos', scale=scale, legend=None),
        tooltip=[
            alt.Tooltip('name:N', title='País'),
            alt.Tooltip('code:N', title='Código'),
            alt.Tooltip('casos:Q', title='Casos')
        ]).transform_lookup('id',
                            from_=alt.LookupData(
                                data=data,
                                key='id',
                                fields=['code', 'name', 'casos'])))

    single = alt.selection_single(
        on='mouseover', nearest=True, fields=['pais'],
        empty='all') if interactive else alt.selection_single()

    circle_chart = (alt.Chart(source).mark_circle(
        opacity=0.4, color='red').encode(
            longitude='lon:Q',
            latitude='lat:Q',
            size=(alt.condition(
                single,
                alt.Size('casos:Q',
                         scale=alt.Scale(range=[50, 4000]),
                         legend=None), alt.value(0))),
            tooltip=[
                alt.Tooltip('pais:N', title='País'),
                alt.Tooltip('code:N', title='Código'),
                alt.Tooltip('casos:Q', title='Casos')
            ]).transform_lookup('id',
                                from_=alt.LookupData(data=data,
                                                     key='id',
                                                     fields=[
                                                         'code', 'pais',
                                                         'casos', 'lat', 'lon'
                                                     ])).add_selection(single))

    final_chart = ((sphere_chart + graticule_chart + countries_chart +
                    circle_chart).project('naturalEarth1').properties(
                        width=800, height=500).configure_view(stroke=None))

    return final_chart
コード例 #11
0
    height=500
).interactive()

st.write(fig)


# Altair の表示
st.markdown('## Altair を使ったインタラクティブなグラフ表示')
cars = data.cars()

brush = alt.selection_interval()

points = alt.Chart(cars).mark_point().encode(
    x='Horsepower:Q',
    y='Miles_per_Gallon:Q',
    color=alt.condition(brush, 'Origin:N', alt.value('lightgray'))
).add_selection(
    brush
)

bars = alt.Chart(cars).mark_bar().encode(
    y='Origin:N',
    color='Origin:N',
    x='count(Origin):Q'
).transform_filter(
    brush
)

points & bars

コード例 #12
0
ファイル: graphs.py プロジェクト: rubens36/covid19
def generate_regions_map(data: pd.DataFrame,
                         date,
                         feature: str,
                         title: str,
                         width: int = 600,
                         height: int = 600,
                         log_scale: bool = True) -> alt.Chart:

    fechas = data['fecha'].apply(lambda x: x.date())
    data = data[(fechas == date)]
    url_geojson = 'https://raw.githubusercontent.com/rubens36/covid19/master/maps/regiones.geojson'
    regions_shape = alt.Data(url=url_geojson,
                             format=alt.DataFormat(property='features',
                                                   type='json'))
    chart_data = data[(data[feature] > 0)][[feature, 'id']]

    base_chart = (alt.Chart(
        regions_shape,
        title='Ubicación de los casos confirmados por región').mark_geoshape(
            stroke='black', strokeWidth=0.5, color='white').encode(
                tooltip=[alt.Tooltip('properties.Region:N', title='Región')]))

    scale = alt.Scale(type='log', scheme='teals') if log_scale else alt.Scale(
        type='linear', scheme='teals')

    color_chart = (alt.Chart(regions_shape).mark_geoshape(
        stroke='black', strokeWidth=0.5).encode(
            color=alt.Color(f"{feature}:Q",
                            title=(get_title(feature)),
                            scale=scale),
            tooltip=[
                alt.Tooltip('properties.Region:N', title='Región'),
                alt.Tooltip(f"{feature}:Q", title=(get_title(feature)))
            ]).transform_lookup('properties.id',
                                from_=alt.LookupData(data=chart_data,
                                                     key='id',
                                                     fields=[feature])))

    single = alt.selection_single(on='mouseover')

    circle_chart = (alt.Chart(regions_shape).mark_circle(
        opacity=0.4, color='red').encode(
            longitude='properties.centroid_lon:Q',
            latitude='properties.centroid_lat:Q',
            size=(alt.condition(
                single,
                alt.Size('infectados:Q',
                         scale=alt.Scale(range=[50, 4000]),
                         legend=None), alt.value(0))),
            tooltip=[
                alt.Tooltip('properties.Region:N', title='Región'),
                alt.Tooltip(f"{feature}:Q", title=(get_title(feature)))
            ]).transform_lookup('properties.id',
                                from_=alt.LookupData(
                                    data=chart_data,
                                    key='id',
                                    fields=['infectados'
                                            ])).properties(selection=single))

    final_chart = ((base_chart + color_chart + circle_chart).configure_view(
        strokeWidth=0).properties(width=width, height=height))

    return final_chart
コード例 #13
0
https://bl.ocks.org/amitkaps/fe4238e716db53930b2f1a70d3401701
"""
# category: interactive charts
import altair as alt
from vega_datasets import data

source = data.stocks()

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

base = alt.Chart(source).encode(
    x='date:T',
    y='price:Q',
    color='symbol:N'
)

points = base.mark_circle().encode(
    opacity=alt.value(0)
).add_selection(
    highlight
).properties(
    width=600
)

lines = base.mark_line().encode(
    size=alt.condition(~highlight, alt.value(1), alt.value(3))
)

points + lines
コード例 #14
0
"""
Histogram with a Global Mean Overlay
------------------------------------
This example shows a histogram with a global mean overlay.
"""
# category: histograms
import altair as alt
from vega_datasets import data

source = data.movies.url

base = alt.Chart(source)

bar = base.mark_bar().encode(
    x=alt.X('IMDB_Rating:Q', bin=True, axis=None),
    y='count()'
)

rule = base.mark_rule(color='red').encode(
    x='mean(IMDB_Rating):Q',
    size=alt.value(5)
)

bar + rule
        "level_1": "second"
    })

correlation_data["correlation_label"] = correlation_data["correlation"].map(
    "{:.2f}".format)

base_correlation_plot = altair.Chart(correlation_data).encode(x="second:O",
                                                              y="first:O")

# Text layer with correlation labels
# Colors are for easier readability
correlation_plot_text = base_correlation_plot.mark_text(
    tooltip=altair.TooltipContent("encoding")).encode(
        text="correlation_label",
        color=altair.condition(altair.datum.correlation > 0.5,
                               altair.value("white"), altair.value("black")))

correlation_plot = base_correlation_plot.mark_rect(
    tooltip=altair.TooltipContent("encoding")).encode(color=altair.Color(
        "correlation:Q",
        scale=altair.Scale(domain=[-1, 0, 1],
                           range=["DarkBlue", "White", "DarkRed"],
                           type="linear")))

CORRELATION_MATRIX = correlation_plot + correlation_plot_text

(CORRELATION_MATRIX_COLUMN,
 CORRELATION_OBSERVATION_COLUMN) = streamlit.columns([2, 1])

CORRELATION_MATRIX_COLUMN.altair_chart(
    CORRELATION_MATRIX.properties(width=600, height=600).configure_axis(
コード例 #16
0
import altair as alt
from vega_datasets import data

cars = data.cars()

highlight = alt.selection_single()
select = alt.selection_single(encodings=['x', 'y'])

chart = alt.Chart(cars).mark_rect().encode(
    x=alt.X('Miles_per_Gallon', bin=True),
    y=alt.X('Horsepower', bin=True),
    color=alt.condition(highlight, 'count()',
                        alt.value('lightgray'))).add_selection(
                            highlight, select)

hist = alt.Chart(cars).mark_bar().encode(y='count()',
                                         x='Origin').transform_filter(select)

chart | hist
コード例 #17
0
color = alt.Color('weather:N', scale=scale)

# We create two selections:
# - a brush that is active on the top panel
# - a multi-click that is active on the bottom panel
brush = alt.selection_interval(encodings=['x'])
click = alt.selection_multi(encodings=['color'])

# Top panel is scatter plot of temperature vs time
points = alt.Chart().mark_point().encode(
    alt.X('monthdate(date):T', axis=alt.Axis(title='Date')),
    alt.Y('temp_max:Q',
        axis=alt.Axis(title='Maximum Daily Temperature (C)'),
        scale=alt.Scale(domain=[-5, 40])
    ),
    color=alt.condition(brush, color, alt.value('lightgray')),
    size=alt.Size('precipitation:Q', scale=alt.Scale(range=[5, 200]))
).properties(
    width=600,
    height=300
).add_selection(
    brush
).transform_filter(
    click
)

# Bottom panel is a bar chart of weather type
bars = alt.Chart().mark_bar().encode(
    x='count()',
    y='weather:N',
    color=alt.condition(click, color, alt.value('lightgray')),
コード例 #18
0
def viz_paired(df, input_city):
    # Data Transform
    cities = df.groupby(['city', 'category_name'])[[
        'city', 'category_name', 'total_business_count', 'sample_rating',
        'sample_review_count'
    ]].mean()
    cities = cities.join(df.groupby(['city'])[['city',
                                               'total_business_count']].sum(),
                         on='city',
                         rsuffix='_by_city')
    cities['%_of_total'] = cities['total_business_count'] / cities[
        'total_business_count_by_city']

    ## Input city
    selected_cities = ['New York, New York', input_city]

    cities_pair = cities.reset_index()
    cities_pair = cities_pair[cities_pair['city'].isin(selected_cities)]

    # Create layered visualization
    viz_cities_slope_circles = alt.Chart(cities_pair).mark_point(
        size=40, filled=True, opacity=1).encode(
            x=alt.X('city:N',
                    sort=alt.Sort(selected_cities),
                    axis=alt.Axis(labelAngle=0)),
            y=alt.Y('%_of_total:Q',
                    axis=alt.Axis(format='.2p',
                                  title='Percent of Total Businesses')),
            color=alt.Color('category_name:N', legend=None),
            tooltip=[
                alt.Tooltip('category_name:N', title='Ethnic Category'),
                alt.Tooltip('%_of_total:Q',
                            format='.2%',
                            title='Percentage of Total'),
                alt.Tooltip('total_business_count:Q', title='Count')
            ]).interactive(bind_x=False)

    selection_opacity = alt.selection_single(encodings=['y'],
                                             on='mouseover',
                                             clear="click",
                                             empty='none')

    condition_opacity = alt.condition(selection_opacity, alt.value(1),
                                      alt.value(0.2))
    condition_size = alt.condition(selection_opacity, alt.value(3),
                                   alt.value(2))

    viz_cities_slope_line = alt.Chart(cities_pair).mark_line().add_selection(
        selection_opacity).encode(
            x=alt.X('city:N',
                    sort=alt.Sort(selected_cities),
                    axis=alt.Axis(labelAngle=0)),
            y=alt.Y('%_of_total:Q',
                    axis=alt.Axis(format='.2p',
                                  title='Percent of Total Businesses')),
            color=alt.Color('category_name:N', legend=None),
            opacity=condition_opacity,
            size=condition_size,
            tooltip=[
                alt.Tooltip('category_name:N', title='Ethnic Category'),
                alt.Tooltip('%_of_total:Q',
                            format='.2%',
                            title='Percentage of Total'),
                alt.Tooltip('total_business_count:Q', title='Count')
            ]).interactive(bind_x=False)

    viz_cities_slope = (viz_cities_slope_line +
                        viz_cities_slope_circles).properties(height=600)

    return viz_cities_slope
コード例 #19
0
ファイル: us_state_capitals.py プロジェクト: mcleonard/tufty
# 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
コード例 #20
0
# generate time series vizs
for municipality in municipalities:
    t = f"<option value=\"{municipality}\">{municipality}</option>"
    options.append(t)
    fname = join("charts", municipality + ".html")
    data = df[["date", municipality]]
    nearest = alt.selection(type="single",
                            nearest=True,
                            on="mouseover",
                            fields=[municipality],
                            empty="none")
    line = (alt.Chart(data).mark_line(interpolate="basis").encode(
        x="date:Q", y=f"{municipality}:Q"))
    selectors = (alt.Chart(data).mark_point().encode(
        x="date:Q",
        opacity=alt.value(0),
    ).add_selection(nearest))

    points = line.mark_point().encode(
        opacity=alt.condition(nearest, alt.value(1), alt.value(0)))

    text = line.mark_text(align="left", dx=5, dy=-5).encode(
        text=alt.condition(nearest, f"{municipality}:Q", alt.value(" ")))

    # Draw a rule at the location of the selection
    rules = (alt.Chart(data).mark_rule(color="gray").encode(
        x="date: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=600, height=300)
コード例 #21
0
"""
Line Chart with Layered Aggregates
----------------------------------
This example shows how to make a multi-series line chart of the daily closing
stock prices for AAPL, AMZN, GOOG, IBM, and MSFT between 2000 and 2010, along
with a layered rule showing the average values.
"""
# category: line charts
import altair as alt
from vega_datasets import data

source = data.stocks()

base = alt.Chart(source).properties(width=600)

line = base.mark_line().encode(
    x='date',
    y='price',
    color='symbol'
)

rule = base.mark_rule().encode(
    y='average(price)',
    color='symbol',
    size=alt.value(2)
)

line + rule
コード例 #22
0
"""
Error Bars showing Confidence Interval
======================================
This example shows how to show error bars using covidence intervals.
The confidence intervals are computed internally in vega by
a non-parametric [bootstrap of the mean](https://github.com/vega/vega-statistics/blob/master/src/bootstrapCI.js).
"""

import altair as alt
from vega_datasets import data

barley = data.barley()

points = alt.Chart(barley).mark_point(filled=True).encode(
    alt.X(
        'mean(yield)',
        scale=alt.Scale(zero=False),
        axis=alt.Axis(title='Barley Yield')
    ),
    y='variety',
    color=alt.value('black')
)

error_bars = alt.Chart(barley).mark_rule().encode(
    x='ci0(yield)',
    x2='ci1(yield)',
    y='variety'
)

chart = points + error_bars
コード例 #23
0
===================
This chart shows an example of using an interval selection to filter the
contents of an attached histogram, allowing the user to see the proportion
of items in each category within the selection.
"""
# category: interactive charts
import altair as alt
from vega_datasets import data

source = data.cars()

brush = alt.selection(type='interval')

points = alt.Chart().mark_point().encode(
    x='Horsepower:Q',
    y='Miles_per_Gallon:Q',
    color=alt.condition(brush, 'Origin:N', alt.value('lightgray'))
).add_selection(
    brush
)

bars = alt.Chart().mark_bar().encode(
    y='Origin:N',
    color='Origin:N',
    x='count(Origin):Q'
).transform_filter(
    brush
)

alt.vconcat(points, bars, data=source)
コード例 #24
0
def plot(county, state):
    charts = []
    for daily in (True, False):
        if daily:
            vals = ['cases-daily', 'deaths-daily']
            daily_or_total = 'Daily'
            angle = 0
        else:
            daily_or_total = 'Total'
            vals = ['cases', 'deaths']
            angle = 270
        thecounty = county_data[(county_data.state == state)
                                & (county_data.county == county)]
        thecounty['realdate'] = pd.to_datetime(thecounty['date'])
        thecounty = thecounty.set_index(['realdate'])
        thecounty['cases-daily'] = (thecounty['cases'].shift(-1) -
                                    thecounty['cases']).shift(1)
        thecounty['deaths-daily'] = (thecounty['deaths'].shift(-1) -
                                     thecounty['deaths']).shift(1)
        thecounty = thecounty.loc['2020-03-01':]  # not much data before then

        # Shorten dates
        thecounty['month-day'] = thecounty.date.str.slice(6)

        # Plot daily cases and deaths
        base = alt.Chart(thecounty).mark_bar().transform_fold(
            fold=vals, as_=['variable', 'value']).encode(
                x='month-day',
                y='max(value):Q',
                color=alt.Color('variable:N', scale=alt.Scale(scheme='set2')),
                tooltip=[
                    'cases-daily', 'deaths-daily', 'cases', 'deaths'
                ]).properties(
                    width=900,
                    title='%s County %s COVID-19 %s Cases and Deaths' %
                    (county, state, daily_or_total))
        if daily: x, y = -5, -8
        else: x, y = 8, 0
        text1 = base.mark_text(align='left',
                               baseline='middle',
                               dx=x,
                               dy=y,
                               angle=angle,
                               fontSize=8,
                               binSpacing=1).encode(y=vals[1],
                                                    text=vals[1],
                                                    color=alt.value('black'))
        text2 = base.mark_text(align='left',
                               baseline='middle',
                               dx=x,
                               dy=y,
                               angle=angle,
                               fontSize=8,
                               binSpacing=1).encode(y=vals[0],
                                                    text=vals[0],
                                                    color=alt.value('black'))

        newchart = base.interactive() + text1 + text2
        charts.append(newchart)

    combined = alt.vconcat(charts[0], charts[1])
    combined.save('%s.html' % county)
    return charts
コード例 #25
0
ファイル: plots.py プロジェクト: renato145/peru-stats
def tl_summary(df,
               values,
               time,
               bars,
               col,
               text,
               title='',
               bars_w=810,
               bars_h=200,
               bars_stack='zero',
               timeline_w=450,
               timeline_h=200,
               slope_avg='Average',
               slope_w=300,
               slope_h=200,
               slope_y_pos=10,
               palette='tableau10'):
    '''
    Plots 3 charts: bars, timeline and slopegraph

    Parameters
    ----------
    df : pandas.DataFrame
    values : str
        Name of the column used for values.
    time : str
        Name of the column used for time values.
    bars : str
        Name of the column used to plot as X-axis on the bars.
    col : str
        Name of the column used for colors.
    text : str
        Name of the column used to show text on slopegraph.
    title : str
        Title of the plot.
    bars_w : int
        Bars plot width.
    bars_h : int
        Bars plot height.
    timeline_w : int
        Timeline plot width.
    timeline_h : int
        Timeline plot height.
    slope_avg : str
        Title for the avg measures on slopegraph.
    slope_w : int
        Slopegraph plot width.
    slope_h : int
        Slopegraph plot height.
    slope_y_pos : int
        Slopegraph titles position.
    palette : str
        Check https://vega.github.io/vega/docs/schemes/#reference

    Returns
    -------
        altair.Chart
    '''
    df = df.copy()
    df['slope_x'] = 'measures'
    df_avg = df.groupby([col, time]).mean().reset_index()
    df_avg[bars] = slope_avg
    df_avg['slope_x'] = 'averages'
    df = pd.concat([df, df_avg], ignore_index=True, sort=True)
    df[values] = df[values].round(2)
    df['slope_text'] = df[values].astype(str) + ' ' + df[col]

    max_time = df[time].max()
    orders = (df[df[time] == max_time].groupby(bars)[values].sum().sort_values(
        ascending=False).index.tolist())
    orders.remove(slope_avg)

    filter_in = alt.selection_single(fields=[bars],
                                     on='mouseover',
                                     empty='none')
    base = alt.Chart(df)
    barsplot = base.mark_bar().encode(
        alt.X(f'{bars}:N', title=None, scale=alt.Scale(domain=orders)),
        alt.Y(f'{values}:Q', title=text, stack=bars_stack),
        alt.Color(col,
                  legend=alt.Legend(orient='bottom-left', title=None),
                  scale=alt.Scale(scheme=palette)),
        opacity=alt.condition(
            filter_in, alt.value('1'), alt.value('0.6'))).transform_filter({
                'and':
                [f'datum.{time} == {max_time}', 'datum.slope_x == "measures"']
            }).properties(title=title,
                          selection=filter_in,
                          width=bars_w,
                          height=bars_h)

    timeline_base = base.mark_line().encode(
        alt.X(f'{time}:O'),
        alt.Y(f'{values}:Q',
              title=text,
              scale=alt.Scale(domain=[df[values].min(), df[values].max()])),
        alt.Color(col, legend=None)).properties(width=timeline_w,
                                                height=timeline_h)

    timeline = timeline_base.transform_filter(filter_in)
    timeline += timeline.mark_circle(size=25)

    timeline_avg = timeline_base.mark_line(
        strokeDash=[4, 2],
        opacity=0.45).transform_filter(f'datum.{bars} == {slope_avg!r}')

    slope = _build_slope(df, values, time, bars, col, text, filter_in,
                         slope_y_pos, slope_w, slope_h)
    chart = barsplot & ((timeline_avg + timeline) | slope)

    return chart
コード例 #26
0
# 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(
    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=-5).encode(
    text=alt.condition(nearest, 'y:Q', alt.value(' '))
)

# Draw a rule at the location of the selection
コード例 #27
0
+csv["Solar Energy (kWh)"]
csv = csv.round(0)

background = altair.Chart(states).mark_geoshape(
    fill="black",
    stroke="white",
).properties(width=1300, height=750).project("albersUsa")
points = altair.Chart(csv).mark_circle(
    color='red',
    size=50,
    opacity=0.6,
).encode(
    longitude='Longitude:Q',
    latitude='Latitude:Q',
    size=altair.Size("Total Energy (kWh):Q", title="Total Energy (kWh)"),
    color="Total Energy (kWh):Q",
    opacity=altair.value(0.6),
    tooltip=[
        'State:N', 'Capital:N', 'Wind Energy (MWh):N', 'Solar Energy (kWh):N',
        'Total Energy (kWh):N'
    ],
)
chart = altair.layer(background, points).configure_legend(
    labelFontSize=15,
    titleFontSize=15,
    labelFont="Times New Roman",
    titleFont="Times New Roman",
).properties(background="none").configure_view(strokeOpacity=0)
chart.save('templates/energy.html', embed_options={'renderer': 'svg'})
chart
コード例 #28
0
    num_cars='count()', groupby=['Origin', 'Cylinders']).encode(
        alt.X('Cylinders:O', scale=alt.Scale(paddingInner=0)),
        alt.Y('Origin:O', scale=alt.Scale(paddingInner=0)),
    )

# Configure heatmap
heatmap = base.mark_rect().encode(
    color=alt.Color('num_cars:Q',
                    scale=alt.Scale(scheme='viridis'),
                    legend=alt.Legend(direction='horizontal')))

# Configure text
text = base.mark_text(baseline='middle').encode(text='num_cars:Q',
                                                color=alt.condition(
                                                    alt.datum.num_cars > 100,
                                                    alt.value('black'),
                                                    alt.value('white')))

# Draw the chart
heatmap + text

##
import altair as alt
from vega_datasets import data

alt.Chart(data.cars()).transform_density(
    'Miles_per_Gallon',
    as_=['Miles_per_Gallon', 'density'],
    extent=[5, 50],
    groupby=['Origin']).mark_area(orient='horizontal').encode(
        y='Miles_per_Gallon:Q',
コード例 #29
0
    def get_context_data(self, **kwargs):
        """

        :param kwargs:
        :return:
        """
        kwargs = super(DoctorWelcome, self).get_context_data(**kwargs)

        # look for oauth user, so we can use it's access_token
        oauth_provider = get_object_or_404(UserSocialAuth, provider='drchrono')

        # check if token is about to expire, and refresh it if so
        # I couldn't get oauth_provider.access_token_expired() to work properly so wrote my own method
        if self.is_access_token_expired(oauth_provider.extra_data['access_token']):
            oauth_provider.refresh_token(load_strategy())

        access_token = oauth_provider.extra_data['access_token']
        patient_client = PatientEndpoint(access_token)
        appointments_client = AppointmentEndpoint(access_token)

        # information about the doctor
        kwargs['doctor'] = next(DoctorEndpoint(access_token).list())

        # list of patients
        patients = list(patient_client.list())

        # list of today's appointments
        today_str = timezone.now().strftime('%m-%d-%y')
        todays_appointments = list(appointments_client.list({}, start=today_str, end=today_str))
        for appointment in todays_appointments:
            patient = [patient for patient in patients if patient.get('id') == appointment.get('patient')][0]
            appointment['first_name'] = patient.get('first_name')
            appointment['last_name'] = patient.get('last_name')
        kwargs['appointments'] = todays_appointments

        # fetch information about patients who have checked in
        visits = Visit.objects.filter(status='Arrived', arrival_time__isnull=False, start_time__isnull=True).all()
        for visit in visits:
            visit.wait_since_arrived = visit.get_wait_duration().seconds
            patient = [patient for patient in patients if patient.get('id') == visit.patient_id][0]
            visit.first_name = patient.get('first_name')
            visit.last_name = patient.get('last_name')
        kwargs['arrived'] = visits

        # fetch information about our current appointment
        current_appointment = Visit.objects.filter(status='In Session', arrival_time__isnull=False,
                                                   start_time__isnull=False).first()
        if current_appointment:
            kwargs['current_appointment'] = current_appointment
            current_appointment.visit_duration = current_appointment.get_visit_duration().seconds
            patient = [patient for patient in patients if patient.get('id') == current_appointment.patient_id][0]
            current_appointment.first_name = patient.get('first_name')
            current_appointment.last_name = patient.get('last_name')
            current_appointment.date_of_birth = patient.get('date_of_birth')
            current_appointment.date_of_last_appointment = patient.get('date_of_last_appointment')
            current_appointment.race = patient.get('race')
            current_appointment.gender = patient.get('gender')
            current_appointment.ethnicity = patient.get('ethnicity')

        # create list of past visit, and use it to generate average wait and visit duration
        past_visits = Visit.objects.filter(status="Finished", arrival_time__isnull=False,
                                           start_time__isnull=False).all()
        if len(past_visits) > 0:
            avg_wait_time = sum([(visit.start_time - visit.arrival_time).seconds for visit in past_visits]) / len(
                past_visits)
            kwargs['avg_wait_duration'] = math.ceil(avg_wait_time)

            avg_visit_duration = sum([(visit.end_time - visit.start_time).seconds for visit in past_visits]) / len(
                past_visits)
            kwargs['avg_visit_duration'] = math.ceil(avg_visit_duration)
        else:
            kwargs['avg_wait_duration'] = "You have no arrivals!"
            kwargs['avg_visit_duration'] = "You have no visits!"

        # creating altair visualization

        # create a df with list of times and durations
        visit_data = [{
            'start_time': visit.start_time,
            'arrival_time': visit.arrival_time,
            'wait_duration': visit.get_wait_duration().seconds,
            'visit_duration': visit.get_visit_duration().seconds,
        } for visit in past_visits]
        visit_data_df = pd.DataFrame(visit_data)

        # https://altair-viz.github.io/user_guide/interactions.html#selections-building-blocks-of-interactions
        brush = alt.selection_interval(encodings=['x'])
        chart = alt.Chart(visit_data_df).mark_bar().properties(
            width=300,
            height=150
        ).add_selection(
            brush
        )

        # combine two charts one with wait duration and one with visit duration
        kwargs['chart'] = alt.hconcat(chart.encode(
            x=alt.X('start_time:T', axis=alt.Axis(title='Time the visit started')),
            y=alt.Y('wait_duration:Q', axis=alt.Axis(title='Wait Duration')),
            color=alt.condition(brush, alt.value('black'), alt.value('lightgray'))
        ), chart.encode(
            x=alt.X('start_time:T', axis=alt.Axis(title='Time the visit started')),
            y=alt.Y('visit_duration:Q', axis=alt.Axis(title='Visit Duration')),
            color=alt.condition(brush, alt.value('black'), alt.value('lightgray'))
        )).resolve_scale(
            y='shared'
        )

        return kwargs
コード例 #30
0
ファイル: vis3.py プロジェクト: jvelleu/649_final_project
This is a temporary script file.
"""
import altair as alt
import pandas as pd
from vega_datasets import data as vega_data

morse = pd.read_csv(
    'https://raw.githubusercontent.com/jvelleu/649_final_project/7648fb227384b3dad2707efdf8fb960f502b52ad/morse_data.csv',
    encoding='utf-8')

mouseSelection = alt.selection_single(on="mouseover",
                                      nearest=True,
                                      empty='none')

opacityCondition = alt.condition(mouseSelection, alt.value(1.0),
                                 alt.value(0.6))

scatter1 = alt.Chart(
    morse, width=400, height=400).mark_point(filled=True).encode(
        alt.X("x", title="", axis=None), alt.Y("y", title="", axis=None),
        alt.Tooltip(["char", "code"], title=None),
        alt.Size("components:O")).add_selection(mouseSelection).encode(
            opacity=opacityCondition)

scatter2 = alt.Chart(
    morse, width=400, height=400).mark_point(filled=True).encode(
        alt.X("x", title="", axis=None),
        alt.Y("y", title="", axis=None),
        alt.Tooltip(["char", "code"], title=None),
        alt.Color("type:N"),
df = pd.read_excel(file,index_col=False)


interval = alt.selection_interval()

base = alt.Chart(df).properties(
    width=350,
    height=350, 
).add_selection(interval)

points = base.mark_point(filled=True, size=1000).encode(
    x='XX_Bottom:Q',
    y='YY_Bottom:Q',
    size='BOPD:Q',
    #color=alt.condition(interval, 'Well_Name', alt.value('lightgray')),
    color=alt.condition(interval, 'Well_Name', alt.value('lightgray')),
    tooltip='Well_Name', 
).properties(
    title='Volve Field Map',
    selection=interval


)

timeseries = base.mark_line().encode(
    x='Date',
    y=alt.Y('BOPD', scale=alt.Scale(domain=(0, 40000))),
    color=alt.Color('Well_Name:O')
    
).properties(
    title='Volve Production by Well in BOPD',
コード例 #32
0
        tot_y='sum(Total)',
        mean_x='mean(Income)',
        groupby=['State']
    ).interactive().transform_filter(brush
    )
    st.write("### *Pan and zoom to see data points more clearly on the scatter plot*")
    #
    #
    #BAR
    bar = alt.Chart(df_w).mark_bar(
    ).transform_filter(
        alt.datum['Year'] == year_slider
    ).encode(
        x=alt.X('State:N',sort='-y', scale=alt.Scale(zero=False),axis=alt.Axis(titleFontSize=20,labelFontSize=14)),
        y=alt.Y('mean(Unemployment):Q',title='Unemployment Rate (%)', scale=alt.Scale(zero=False),axis=alt.Axis(titleFontSize=20,labelFontSize=14)),
        color= alt.condition(brush,'State:N',alt.value('lightgray'), legend=None),
        tooltip=[alt.Tooltip('State:N'),alt.Tooltip('mean(Unemployment):Q',title="Unemployment")]
    ).properties(
        width=1000,
        height=500
    ).add_selection(
        brush
    )

    states_us = alt.topo_feature('https://vega.github.io/vega-datasets/data/us-10m.json', 'states')
    source = 'https://raw.githubusercontent.com/sammyhajomar/test/main/altair-dataset.csv'
    variables = ['State','Total','id']


    US_map = alt.Chart(states_us).mark_geoshape().encode(
        color=alt.condition(multi_state,'Total:Q',alt.value('lightgray'),title='Total Deaths'),
コード例 #33
0
capitals = data.us_state_capitals.url

# US states background
background = alt.Chart(states).mark_geoshape(
    fill='lightgray',
    stroke='white').properties(title='US State Capitols',
                               projection={'type': 'albersUsa'},
                               width=700,
                               height=400)

# 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))).properties(selection=hover)

chart = background + points + text
コード例 #34
0
def build_altair_text_error(error: str) -> str:
    return alt.Chart(pd.DataFrame({'message': [f'❌ {error}']}))\
        .mark_text(size=20, font='monospace').encode(
            text='message',
            color=alt.value('red')
        ).properties(width=500, height=50).configure_view(strokeWidth=0).to_json()
コード例 #35
0
ファイル: select_detail.py プロジェクト: mcleonard/tufty
timeseries = timeseries.reset_index().melt('time')

# Merge the (x, y) metadata into the long-form view
timeseries['id'] = timeseries['id'].astype(int)  # make merge not complain
data = pd.merge(timeseries, locations, on='id')

# Data is prepared, now make a chart

selector = alt.selection_single(empty='all', fields=['id'])

base = alt.Chart(data).properties(
    width=250,
    height=250
).add_selection(selector)

points = base.mark_point(filled=True, size=200).encode(
    x='mean(x)',
    y='mean(y)',
    color=alt.condition(selector, 'id:O', alt.value('lightgray'), legend=None),
).interactive()

timeseries = base.mark_line().encode(
    x='time',
    y=alt.Y('value', scale=alt.Scale(domain=(-15, 15))),
    color=alt.Color('id:O', legend=None)
).transform_filter(
    selector
)

points | timeseries
コード例 #36
0
settle_data = pd.read_csv(
    "D:/OneDrive/data/seattle-weather.csv")  # 导入数据集,数据见文末
settle_data.weather.unique()  # 定义比例:根据天气类型的分布来绘制数据
scale = alt.Scale(
    domain=['sun', 'fog', 'drizzle', 'rain', 'snow'],
    range=['#e7ba52', '#a7a7a7', '#aec7e8', '#1f77b4', '#9467bd'])
color = alt.Color('weather:N', scale=scale)
brush = alt.selection_interval(encodings=['x'])  #添加互动功能
click = alt.selection_multi(encodings=['color'])
#顶部散射图:温度 Vs 日期
points = alt.Chart().mark_point().encode(
    alt.X('monthdate(date):T', title='Date'),
    alt.Y('temp_max:Q',
          title='Maximum Daily Temperature (C)',
          scale=alt.Scale(domain=[-5, 40])),
    color=alt.condition(brush, color, alt.value('lightgray')),
    size=alt.Size('precipitation:Q',
                  scale=alt.Scale(range=[5, 200]))).properties(
                      width=1080,
                      height=500).add_selection(brush).transform_filter(click)
##########################
# 2. 底部栏图:天气类型
bars = alt.Chart().mark_bar().encode(
    x='count()',
    y='weather:N',
    color=alt.condition(click, color, alt.value('lightgray')),
).transform_filter(brush).properties(width=550, ).add_selection(click)
##########################
#3. 构建复合图:垂直串联两个图表
tu = alt.vconcat(points, bars, data=settle_data, title="天气数据: 2012-2015")
tu.save('c:/in/4.html')
コード例 #37
0
ファイル: dot_dash_plot.py プロジェクト: mcleonard/tufty
"""
# category: scatter plots
import altair as alt
from vega_datasets import data

source = data.cars()

# Configure the options common to all layers
brush = alt.selection(type='interval')
base = alt.Chart(source).add_selection(brush)

# Configure the points
points = base.mark_point().encode(
    x=alt.X('Miles_per_Gallon', axis=alt.Axis(title='')),
    y=alt.Y('Horsepower', axis=alt.Axis(title='')),
    color=alt.condition(brush, 'Origin', alt.value('grey'))
)

# Configure the ticks
tick_options = dict(labels=False, domain=False, ticks=False)
tick_axis = alt.Axis(**tick_options)
tick_axis_notitle = alt.Axis(title='', **tick_options)

x_ticks = base.mark_tick().encode(
    alt.X('Miles_per_Gallon', axis=tick_axis),
    alt.Y('Origin', axis=tick_axis_notitle),
    color=alt.condition(brush, 'Origin', alt.value('lightgrey'))
)

y_ticks = base.mark_tick().encode(
    alt.X('Origin', axis=tick_axis_notitle),
コード例 #38
0
# Production values only
df["power"] = df["power"].clip(lower=0)
max_production = df["power"].max()

# Create selection brush
date_selection_brush = alt.selection_multi(encodings=["x", "y"], name="date_select")

# Create calendar
calendar = (
    alt.Chart(df).mark_rect()
    .encode(
        x=alt.Y("date(datetime):O", title="Day of the month"),
        y=alt.X("month(datetime):O", title="Month"),
        color=alt.Color("sum(power):Q", title="Excess solar production (kWh)", scale=alt.Scale(scheme="dark2")),
        opacity=alt.condition(date_selection_brush, alt.value(1), alt.value(0.7)))
)

# Make calendar days selectable
calendar = calendar.add_selection(date_selection_brush)

# Create a chart that shows a power profile of a single day (the selected day)
chart = (
    alt.Chart(df).mark_line()
    .transform_filter(date_selection_brush)
    .encode(
        x=alt.X(
            "datetime",
            title="Time of day",
        ),
        y=alt.Y("power", title="Power (kW)", scale=alt.Scale(domain=(0, max_production))),
コード例 #39
0
# left panel: scatter plot
points = alt.Chart().mark_point(filled=True, color="black").encode(
    x='x',
    y='y'
).transform_filter(
    pts.ref()
).properties(
    width=300,
    height=300
)

# right panel: histogram
mag = alt.Chart().mark_bar().encode(
    x='mbin:N',
    y="count()",
    color=alt.condition(pts, alt.value("black"), alt.value("lightgray"))
).properties(
    selection=pts,
    width=300,
    height=300
)

# build the chart:
alt.hconcat(
    points,
    mag,
    data=source
).transform_bin(
    "mbin",
    field="m",
    bin=alt.Bin(maxbins=20)
コード例 #40
0
def plot_interactive_histograms_sm():
    data1 = pd.read_csv(
        'https://raw.githubusercontent.com/jamescoller/multilayer_design_network_tool/master/results/all_data2018-01-01.csv'
    )
    data2 = pd.read_csv(
        'https://raw.githubusercontent.com/jamescoller/multilayer_design_network_tool/master/results/all_data2018-02-01.csv'
    )
    data3 = pd.read_csv(
        'https://raw.githubusercontent.com/jamescoller/multilayer_design_network_tool/master/results/all_data2018-03-01.csv'
    )
    data4 = pd.read_csv(
        'https://raw.githubusercontent.com/jamescoller/multilayer_design_network_tool/master/results/all_data2018-04-01.csv'
    )
    data5 = pd.read_csv(
        'https://raw.githubusercontent.com/jamescoller/multilayer_design_network_tool/master/results/all_data2018-05-01.csv'
    )
    data6 = pd.read_csv(
        'https://raw.githubusercontent.com/jamescoller/multilayer_design_network_tool/master/results/all_data2018-06-01.csv'
    )
    data7 = pd.read_csv(
        'https://raw.githubusercontent.com/jamescoller/multilayer_design_network_tool/master/results/all_data2018-07-01.csv'
    )
    data8 = pd.read_csv(
        'https://raw.githubusercontent.com/jamescoller/multilayer_design_network_tool/master/results/all_data2018-08-01.csv'
    )
    data9 = pd.read_csv(
        'https://raw.githubusercontent.com/jamescoller/multilayer_design_network_tool/master/results/all_data2018-09-01.csv'
    )
    data10 = pd.read_csv(
        'https://raw.githubusercontent.com/jamescoller/multilayer_design_network_tool/master/results/all_data2018-10-01.csv'
    )
    data11 = pd.read_csv(
        'https://raw.githubusercontent.com/jamescoller/multilayer_design_network_tool/master/results/all_data2018-11-01.csv'
    )
    data12 = pd.read_csv(
        'https://raw.githubusercontent.com/jamescoller/multilayer_design_network_tool/master/results/all_data2018-12-01.csv'
    )

    node_info = pd.read_csv(
        'https://raw.githubusercontent.com/jamescoller/multilayer_design_network_tool/master/Data/Node_List.csv'
    )

    time_summary = pd.read_csv(
        'https://raw.githubusercontent.com/jamescoller/multilayer_design_network_tool/master/results/summary_data.csv'
    )

    data1['month'] = 'January'
    data2['month'] = 'February'
    data3['month'] = 'March'
    data4['month'] = 'April'
    data5['month'] = 'May'
    data6['month'] = 'June'
    data7['month'] = 'July'
    data8['month'] = 'August'
    data9['month'] = 'September'
    data10['month'] = 'October'
    data11['month'] = 'November'
    data12['month'] = 'December'

    data1 = pd.merge(data1,
                     node_info,
                     how='inner',
                     left_on='NodeID',
                     right_on='ID')
    data2 = pd.merge(data2,
                     node_info,
                     how='inner',
                     left_on='NodeID',
                     right_on='ID')
    data3 = pd.merge(data3,
                     node_info,
                     how='inner',
                     left_on='NodeID',
                     right_on='ID')
    data4 = pd.merge(data4,
                     node_info,
                     how='inner',
                     left_on='NodeID',
                     right_on='ID')
    data5 = pd.merge(data5,
                     node_info,
                     how='inner',
                     left_on='NodeID',
                     right_on='ID')
    data6 = pd.merge(data6,
                     node_info,
                     how='inner',
                     left_on='NodeID',
                     right_on='ID')
    data7 = pd.merge(data7,
                     node_info,
                     how='inner',
                     left_on='NodeID',
                     right_on='ID')
    data8 = pd.merge(data8,
                     node_info,
                     how='inner',
                     left_on='NodeID',
                     right_on='ID')
    data9 = pd.merge(data9,
                     node_info,
                     how='inner',
                     left_on='NodeID',
                     right_on='ID')
    data10 = pd.merge(data10,
                      node_info,
                      how='inner',
                      left_on='NodeID',
                      right_on='ID')
    data11 = pd.merge(data11,
                      node_info,
                      how='inner',
                      left_on='NodeID',
                      right_on='ID')
    data12 = pd.merge(data12,
                      node_info,
                      how='inner',
                      left_on='NodeID',
                      right_on='ID')

    all_data = pd.concat([
        data1, data2, data3, data4, data5, data6, data7, data8, data9, data10,
        data11, data12
    ])

    months = [
        'January', 'February', 'March', 'April', 'May', 'June', 'July',
        'August', 'September', 'October', 'November', 'December'
    ]

    layers = ['Algorithm', 'Physical', 'Task', 'Function', 'Information']

    input_dropdown = alt.binding_select(options=months)
    selection = alt.selection_single(fields=['month'],
                                     bind=input_dropdown,
                                     name='Month')

    layer_dropdown = alt.binding_select(options=layers)
    layer_selection = alt.selection_single(fields=['Layer'],
                                           bind=layer_dropdown,
                                           name='Layer')

    cn = alt.Chart(all_data).mark_bar().encode(
        x=alt.X('Cn:Q', bin=alt.Bin(maxbins=20), title='Connectedness Rating'),
        y=alt.Y('count()', title='Number of Nodes'),
        color=alt.value('#4e79a7')).add_selection(selection).transform_filter(
            selection).add_selection(layer_selection).transform_filter(
                layer_selection)

    rn = alt.Chart(all_data).mark_bar().encode(
        x=alt.X('Rn:Q', bin=alt.Bin(maxbins=20), title='Reliability Rating'),
        y=alt.Y('count()', title='Number of Nodes'),
        color=alt.value('#f28e2b')).add_selection(selection).transform_filter(
            selection).add_selection(layer_selection).transform_filter(
                layer_selection)

    id = alt.Chart(all_data).mark_bar().encode(
        x=alt.X('Id:Q',
                bin=alt.Bin(maxbins=20),
                title='Interdependency Rating'),
        y=alt.Y('count()', title='Number of Nodes'),
        color=alt.value('#e15759')).add_selection(selection).transform_filter(
            selection).add_selection(layer_selection).transform_filter(
                layer_selection)

    chart = alt.hconcat(cn, rn, id)
    #(cn | rn | id)
    chart.serve()
    return
コード例 #41
0
"""
Parallel Coordinates Example
----------------------------
A `Parallel Coordinates <https://en.wikipedia.org/wiki/Parallel_coordinates>`_
chart is a chart that lets you visualize the individual data points by drawing
a single line for each of them.
Such a chart can be created in Altair, but requires some data preprocessing
to transform the data into a suitable representation.
This example shows a parallel coordinates chart with the Iris dataset.
"""
# category: other charts

import altair as alt
from vega_datasets import data

source = data.iris()
source_transformed = source.reset_index().melt(['species', 'index'])

alt.Chart(source_transformed).mark_line().encode(
    x='variable:N',
    y='value:Q',
    color='species:N',
    detail='index:N',
    opacity=alt.value(0.5)
).properties(width=500)
コード例 #42
0
import altair as alt
from vega_datasets import data

source = data.cars()

# Configure the options common to all layers
brush = alt.selection(type='interval')
base = alt.Chart(source).add_selection(brush)

# Configure the points
points = base.mark_point().encode(x=alt.X('Miles_per_Gallon',
                                          axis=alt.Axis(title='')),
                                  y=alt.Y('Horsepower',
                                          axis=alt.Axis(title='')),
                                  color=alt.condition(brush, 'Origin',
                                                      alt.value('grey')))

# Configure the ticks
tick_options = dict(labels=False, domain=False, ticks=False)
tick_axis = alt.Axis(**tick_options)
tick_axis_notitle = alt.Axis(title='', **tick_options)

x_ticks = base.mark_tick().encode(alt.X('Miles_per_Gallon', axis=tick_axis),
                                  alt.Y('Origin', axis=tick_axis_notitle),
                                  color=alt.condition(brush, 'Origin',
                                                      alt.value('lightgrey')))

y_ticks = base.mark_tick().encode(alt.X('Origin', axis=tick_axis_notitle),
                                  alt.Y('Horsepower', axis=tick_axis),
                                  color=alt.condition(brush, 'Origin',
                                                      alt.value('lightgrey')))
コード例 #43
0
ファイル: interactive_brush.py プロジェクト: mcleonard/tufty
"""
Interactive Rectangular Brush
=============================
This example shows how to add a simple rectangular brush to a scatter plot.
By clicking and dragging on the plot, you can highlight points within the
range.
"""
# category: interactive charts
import altair as alt
from vega_datasets import data

source = data.cars()
brush = alt.selection(type='interval')

alt.Chart(source).mark_point().encode(
    x='Horsepower:Q',
    y='Miles_per_Gallon:Q',
    color=alt.condition(brush, 'Cylinders:O', alt.value('grey'))
).add_selection(brush)
コード例 #44
0
import altair as alt
import pandas as pd


penguins_df = pd.read_csv('data/penguins.csv')

slider = alt.____(name=.____, max=max(penguins_df.____))
select_rating = alt.____(
    fields=[____],
    bind=____)

slider_scatter = (alt.Chart(penguins_df).mark_circle().encode(
    alt.X('culmen_length_mm', title='Culmen length (mm)', scale=alt.Scale(zero=False)),
    alt.Y('culmen_depth_mm', title='Culmen depth (mm)', scale=alt.Scale(zero=False)),
    color='species',
    opacity=alt.condition(____ < ____, alt.value(0.7), alt.value(0.05)))
    .add_selection(____))

slider_scatter
コード例 #45
0
brush = alt.selection(type='interval', encodings=['x'])

# Define the base chart, with the common parts of the
# background and highlights
base = alt.Chart().mark_bar().encode(
    x=alt.X(alt.repeat('column'), type='quantitative', bin=alt.Bin(maxbins=20)),
    y='count()'
).properties(
    width=180,
    height=130
)

# blue background with selection
background = base.properties(selection=brush)

# yellow highlights on the transformed data
highlight = base.encode(
    color=alt.value('goldenrod')
).transform_filter(brush)

# layer the two charts & repeat
alt.layer(
    background,
    highlight,
    data=source
).transform_calculate(
    "time",
    "hours(datum.date)"
).repeat(column=["distance", "delay", "time"])
コード例 #46
0
ファイル: plots.py プロジェクト: renato145/peru-stats
def slope_comparison(df,
                     values,
                     bars,
                     col,
                     text,
                     bars_w=200,
                     bars_h=515,
                     slope_avg='Average',
                     slope_w=350,
                     slope_h=240,
                     slope_y_pos=10,
                     slope_y_title=None):
    '''
    Plots 3 charts: v-bars and 2 slopegraph for comparison.

    Parameters
    ----------
    df : pandas.DataFrame
    values : str
        Name of the column used for values.
    bars : str
        Name of the column used to plot as X-axis on the bars.
    col : str
        Name of the column used for colors.
    text : str
        Name of the column used to show text on slopegraph.
    bars_w : int
        Bars plot width.
    bars_h : int
        Bars plot height.
    slope_avg : str
        Title for the avg measures on slopegraph.
    slope_w : int
        Slopegraph plot width.
    slope_h : int
        Slopegraph plot height.
    slope_y_pos : int
        Slopegraph titles position.
    slope_y_title : str
        Title to use on slope y axis.

    Returns
    -------
        altair.Chart

    Parameters
    ----------
    df : pandas.DataFrame
    vs : str list
        List of variables to include in the plot.
    year : int
        Year to extract from data.
    custom_fn : function
        Function to apply to df after formatting.
        Use it to format names on the df.
    kwargs : arguments passed to get_data_series
    '''
    df = df.copy()
    df['slope_x'] = 'measures'
    df_avg = df.groupby(col).mean().reset_index()
    df_avg[bars] = slope_avg
    df_avg['slope_x'] = 'averages'
    df = pd.concat([df, df_avg], ignore_index=True, sort=True)
    df[values] = df[values].round(2)
    df['slope_text'] = df[values].astype(str) + ' ' + df[col]

    mouse = alt.selection_single(on='mouseover',
                                 fields=[bars],
                                 empty='none',
                                 nearest=True)
    click = alt.selection_single(fields=[bars], empty='none')

    base = alt.Chart(df)

    barsplot = base.mark_point(filled=True).encode(
        alt.X(f'mean({values})',
              scale=alt.Scale(zero=False),
              axis=alt.Axis(title=None)),
        alt.Y(f'{bars}:N', axis=alt.Axis(title=None)),
        size=alt.condition(mouse, alt.value(400), alt.value(
            200))).transform_filter('datum.slope_x == "measures"').properties(
                selection=mouse, width=bars_w, height=bars_h)

    barsplot += barsplot.encode(
        size=alt.condition(click, alt.value(350), alt.value(200)),
        color=alt.condition(click, alt.ColorValue('#800000'),
                            alt.value('#879cab'))).properties(selection=click)

    bars_ci = base.mark_rule().encode(
        x=f'ci0({values})', x2=f'ci1({values})',
        y=f'{bars}:N').transform_filter(
            'datum.slope_x == "measures"').properties(width=bars_w,
                                                      height=bars_h)

    slope_mouse = _build_slope(df, values, None, bars, col, text, mouse,
                               slope_y_pos, slope_w, slope_h, slope_y_title)
    slope_click = _build_slope(df, values, None, bars, col, text, click,
                               slope_y_pos, slope_w, slope_h)
    chart = (bars_ci + barsplot) | (slope_mouse & slope_click)

    return chart
コード例 #47
0
"""
Bar Chart with Highlighted Bar
------------------------------
This example shows a basic bar chart with a single bar highlighted.
"""
# category: bar charts
import altair as alt
from vega_datasets import data

source = data.wheat.url

alt.Chart(source).mark_bar().encode(
    x='year:O',
    y="wheat:Q",
    # The highlight will be set on the result of a conditional statement
    color=alt.condition(
        alt.datum.year == 1810,  # If the year is 1810 this test returns True,
        alt.value('orange'),     # which sets the bar orange.
        alt.value('steelblue')   # And if it's not true it sets the bar steelblue.
    )
).properties(width=600)
コード例 #48
0
ファイル: airports.py プロジェクト: mcleonard/tufty
Locations of US Airports
========================
This is a layered geographic visualization that shows the positions of US
airports on a background of US states.
"""
# category: case studies
import altair as alt
from vega_datasets import data

airports = data.airports()
states = alt.topo_feature(data.us_10m.url, feature='states')

# US states background
background = alt.Chart(states).mark_geoshape(
    fill='lightgray',
    stroke='white'
).properties(
    width=500,
    height=300
).project('albersUsa')

# airport positions on background
points = alt.Chart(airports).mark_circle().encode(
    longitude='longitude:Q',
    latitude='latitude:Q',
    size=alt.value(10),
    color=alt.value('steelblue')
)

background + points