Skip to content

Instantly share code, notes, and snippets.

@Erlemar
Created October 26, 2022 14:30
Show Gist options
  • Save Erlemar/9477c676c6355a54f47adc6026986372 to your computer and use it in GitHub Desktop.
Save Erlemar/9477c676c6355a54f47adc6026986372 to your computer and use it in GitHub Desktop.
import pandas as pd
import altair as alt
import numpy as np
alt.data_transformers.disable_max_rows()
from IPython.display import display, HTML
display(HTML("<style>.container { width:100% !important; }</style>"))
import matplotlib_inline.backend_inline
matplotlib_inline.backend_inline.set_matplotlib_formats('retina')
df = pd.read_csv('df.csv')
# creating a new set of values for colors. Altair needs data in this format:
# a single column with values and another column with a categorical value.
df['value_to_select'] = 'porosity'
df = df.rename(columns={'porosity': 'value'})
df1 = df.copy()
df1['value_to_select'] = 'porosity1'
df1['value'] *= np.random.random(df.shape[0])
df = pd.concat([df, df1])
slider = alt.binding_range(min=int(df['k'].min()), max=int(df['k'].max()), step=1)
select_layer = alt.selection_single(name="Layer", fields=['k'],
bind=slider, init={'k': 11})
input_dropdown = alt.binding_select(options=['porosity', 'porosity1'], name='value_to_select')
selection = alt.selection_single(fields=['value_to_select'], bind=input_dropdown)
i_slider = alt.binding_range(min=1, max=20, step=1)
select_i_range = alt.selection_single(name="i", fields=['i'],
bind=i_slider, init={'i': 6})
j_slider = alt.binding_range(min=1, max=20, step=1)
select_j_range = alt.selection_single(name="j", fields=['j'],
bind=j_slider, init={'j': 6})
centers = [(62, 62), (26, 26), (26, 62), (26, 99), (44, 99), (62, 99)]
charts = []
for (i_center, j_center) in centers:
chart_ = alt.Chart(df).mark_rect().encode(
x='i:O',
y='j:O',
color=alt.Color('value:Q', scale=alt.Scale(scheme='plasma', domain=(df['value'].min(), df['value'].max())))
).properties(
width=200,
height=200
).add_selection(select_i_range).transform_filter(
f"datum.i >= {i_center} - i.i[0] && datum.i <= {i_center} + i.i[0]"
).add_selection(select_j_range).transform_filter(
f"datum.j >= {j_center} - j.j[0] && datum.j <= {j_center} + j.j[0]"
).add_selection(select_layer).add_selection(
selection
).transform_filter(
selection
)
charts.append(chart_)
big_chart = alt.Chart(df).mark_rect().encode(
x='i:O',
y='j:O',
color=alt.Color('value:Q', scale=alt.Scale(scheme='plasma', domain=(df['value'].min(), df['value'].max())))
).properties(
width=400,
height=400
).transform_filter(
select_layer
).add_selection(select_layer).add_selection(
selection
).transform_filter(
selection
)
big_chart | ((charts[0] | charts[1] | charts[2]) & ((charts[3] | charts[4] | charts[5])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment