
import altair as alt
import pandas as pd
import numpy as np
np.random.seed(42)

# Generating Random Data
original_data = pd.DataFrame({'samples':np.array(np.random.normal(50, 15, 100), dtype=np.int)})

# Splitting Steam and Leaf
original_data['stem'] = original_data['samples'].apply(lambda x: str(x)[:-1])
original_data['leaf'] = original_data['samples'].apply(lambda x: str(x)[-1])

# Grouping Leafs for each Stem
grouped_data = pd.DataFrame(columns=['stem', 'leaf'])
for key, group in original_data.groupby('stem'):
    grouped_data = grouped_data.append({'stem':key,
                                        'leaf': ''.join(group['leaf'].sort_values())},
                                        ignore_index=True)

# Plotting Stems and Leafs 
chart = alt.Chart(grouped_data).mark_text(align='left', baseline='middle',dx=-40).encode(
    y = alt.Y('stem', axis=alt.Axis(title='', tickSize=0)), 
    text = 'leaf'
).properties(width=400).configure_axis(labelFontSize=20).configure_text(fontSize=20)

import altair as alt
import pandas as pd
import numpy as np
np.random.seed(42)

# Generating random data
original_data = pd.DataFrame({'samples':np.array(np.random.normal(50, 15, 100), dtype=np.int)})

# Splitting steam and leaf
original_data['stem'] = original_data['samples'].apply(lambda x: str(x)[:-1])
original_data['leaf'] = original_data['samples'].apply(lambda x: str(x)[-1])

original_data.sort_values(by=['stem', 'leaf'], inplace=True)

# Determining position
position = np.array([], dtype=np.int64)
for key, group in original_data.groupby('stem'):
    position = np.hstack([position, [*group.reset_index().index.values]])
    
original_data['position'] = position + 1

# Creating stem and leaf plot
chart = alt.Chart(original_data).mark_text(align='left', baseline='middle', dx=-5).encode(
    y = alt.Y('stem:N', axis=alt.Axis(title='', tickSize=0)), 
    x = alt.X('position:Q', axis=alt.Axis(title='', ticks=False,labels=False,grid=False)), 
    text = 'leaf:N'
).configure_axis(labelFontSize=20).configure_text(fontSize=20)

Modify example to calculate leaf position
Modify example to calculate leaf position

  
  import altair as alt
  import pandas as pd
  import numpy as np
  np.random.seed(42)
  
- # Generating Random Data
+ # Generating random data
  original_data = pd.DataFrame({'samples':np.array(np.random.normal(50, 15, 100), dtype=np.int)})
  
- # Splitting Steam and Leaf
+ # Splitting steam and leaf
  original_data['stem'] = original_data['samples'].apply(lambda x: str(x)[:-1])
  original_data['leaf'] = original_data['samples'].apply(lambda x: str(x)[-1])
  
- # Grouping Leafs for each Stem
- grouped_data = pd.DataFrame(columns=['stem', 'leaf'])
+ original_data.sort_values(by=['stem', 'leaf'], inplace=True)
+ 
+ # Determining position
+ position = np.array([], dtype=np.int64)
  for key, group in original_data.groupby('stem'):
-     grouped_data = grouped_data.append({'stem':key,
-                                         'leaf': ''.join(group['leaf'].sort_values())},
-                                         ignore_index=True)
+     position = np.hstack([position, [*group.reset_index().index.values]])
+     
+ original_data['position'] = position + 1
  
- # Plotting Stems and Leafs 
+ # Creating stem and leaf plot
- chart = alt.Chart(grouped_data).mark_text(align='left', baseline='middle',dx=-40).encode(
+ chart = alt.Chart(original_data).mark_text(align='left', baseline='middle', dx=-5).encode(
-     y = alt.Y('stem', axis=alt.Axis(title='', tickSize=0)), 
+     y = alt.Y('stem:N', axis=alt.Axis(title='', tickSize=0)), 
+     x = alt.X('position:Q', axis=alt.Axis(title='', ticks=False,labels=False,grid=False)), 
-     text = 'leaf'
+     text = 'leaf:N'
- ).properties(width=400).configure_axis(labelFontSize=20).configure_text(fontSize=20)
+ ).configure_axis(labelFontSize=20).configure_text(fontSize=20)
+ 
Modify example to calculate leaf position
## Code Before:

import altair as alt
import pandas as pd
import numpy as np
np.random.seed(42)

# Generating Random Data
original_data = pd.DataFrame({'samples':np.array(np.random.normal(50, 15, 100), dtype=np.int)})

# Splitting Steam and Leaf
original_data['stem'] = original_data['samples'].apply(lambda x: str(x)[:-1])
original_data['leaf'] = original_data['samples'].apply(lambda x: str(x)[-1])

# Grouping Leafs for each Stem
grouped_data = pd.DataFrame(columns=['stem', 'leaf'])
for key, group in original_data.groupby('stem'):
    grouped_data = grouped_data.append({'stem':key,
                                        'leaf': ''.join(group['leaf'].sort_values())},
                                        ignore_index=True)

# Plotting Stems and Leafs 
chart = alt.Chart(grouped_data).mark_text(align='left', baseline='middle',dx=-40).encode(
    y = alt.Y('stem', axis=alt.Axis(title='', tickSize=0)), 
    text = 'leaf'
).properties(width=400).configure_axis(labelFontSize=20).configure_text(fontSize=20)
## Instruction:
Modify example to calculate leaf position
## Code After:

import altair as alt
import pandas as pd
import numpy as np
np.random.seed(42)

# Generating random data
original_data = pd.DataFrame({'samples':np.array(np.random.normal(50, 15, 100), dtype=np.int)})

# Splitting steam and leaf
original_data['stem'] = original_data['samples'].apply(lambda x: str(x)[:-1])
original_data['leaf'] = original_data['samples'].apply(lambda x: str(x)[-1])

original_data.sort_values(by=['stem', 'leaf'], inplace=True)

# Determining position
position = np.array([], dtype=np.int64)
for key, group in original_data.groupby('stem'):
    position = np.hstack([position, [*group.reset_index().index.values]])
    
original_data['position'] = position + 1

# Creating stem and leaf plot
chart = alt.Chart(original_data).mark_text(align='left', baseline='middle', dx=-5).encode(
    y = alt.Y('stem:N', axis=alt.Axis(title='', tickSize=0)), 
    x = alt.X('position:Q', axis=alt.Axis(title='', ticks=False,labels=False,grid=False)), 
    text = 'leaf:N'
).configure_axis(labelFontSize=20).configure_text(fontSize=20)
