Modify example to calculate leaf position
@@ -9,22 +9,25 @@ 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
-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)
\ No newline at end of file
+# 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)
