File size: 3,718 Bytes
936e0a7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
import simplestart as ss
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource
from bokeh.resources import CDN
from bokeh.embed import file_html
from sklearn.datasets import load_iris
import pandas as pd
# Load the Iris dataset
iris = load_iris()
df = pd.DataFrame(data=iris.data, columns=iris.feature_names)
df['species'] = iris.target
df['species'] = df['species'].map({0: 'setosa', 1: 'versicolor', 2: 'virginica'})
# Create the data source and figure object
source = ColumnDataSource(data=dict(
sepal_length=df['sepal length (cm)'],
sepal_width=df['sepal width (cm)'],
petal_length=df['petal length (cm)'],
petal_width=df['petal width (cm)'],
species=df['species']
))
# Create a Bokeh scatter plot
p = figure(title="Iris Dataset Scatter Plot", x_axis_label='Sepal Length (cm)', y_axis_label='Sepal Width (cm)',
tools="pan,wheel_zoom,box_zoom,reset,hover,save", width=800)
# Set different colors based on species
colors = {'setosa': 'blue', 'versicolor': 'green', 'virginica': 'red'}
for species, color in colors.items():
df_species = df[df['species'] == species]
source_species = ColumnDataSource(data=dict(
sepal_length=df_species['sepal length (cm)'],
sepal_width=df_species['sepal width (cm)']
))
p.scatter('sepal_length', 'sepal_width', source=source_species, legend_label=species, color=color,
size=10, alpha=0.5)
p.legend.title = 'Species'
p.legend.location = 'top_left'
# Embed the Bokeh figure into SimpleStart
html = file_html(p, CDN, "Iris Dataset Scatter Plot")
#ui
ss.md('''
## simplestart Chart Demo - Bokeh
''')
ss.space()
ss.md('''
#### π
Example
An interactive scatter plot of the Iris data using Bokeh
''')
ss.space()
ss.htmlview(html, border = False)
ss.space()
ss.write("#### π Code")
def viewcode():
ss.session["viewcode"] = 1
ss.session["viewcode"] = 0
ss.button("View Code", size="small", onclick = viewcode)
def conditioner(event):
return (ss.session["viewcode"] == 1)
code = '''
```python
import simplestart as ss
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource
from bokeh.resources import CDN
from bokeh.embed import file_html
from sklearn.datasets import load_iris
import pandas as pd
# Load the Iris dataset
iris = load_iris()
df = pd.DataFrame(data=iris.data, columns=iris.feature_names)
df['species'] = iris.target
df['species'] = df['species'].map({0: 'setosa', 1: 'versicolor', 2: 'virginica'})
# Create the data source and figure object
source = ColumnDataSource(data=dict(
sepal_length=df['sepal length (cm)'],
sepal_width=df['sepal width (cm)'],
petal_length=df['petal length (cm)'],
petal_width=df['petal width (cm)'],
species=df['species']
))
# Create a Bokeh scatter plot
p = figure(title="Iris Dataset Scatter Plot", x_axis_label='Sepal Length (cm)', y_axis_label='Sepal Width (cm)',
tools="pan,wheel_zoom,box_zoom,reset,hover,save", width=800)
# Set different colors based on species
colors = {'setosa': 'blue', 'versicolor': 'green', 'virginica': 'red'}
for species, color in colors.items():
df_species = df[df['species'] == species]
source_species = ColumnDataSource(data=dict(
sepal_length=df_species['sepal length (cm)'],
sepal_width=df_species['sepal width (cm)']
))
p.scatter('sepal_length', 'sepal_width', source=source_species, legend_label=species, color=color,
size=10, alpha=0.5)
p.legend.title = 'Species'
p.legend.location = 'top_left'
# Embed the Bokeh figure into SimpleStart
html = file_html(p, CDN, "Iris Dataset Scatter Plot")
#ui
ss.htmlview(html, border = False)
'''
with ss.when(conditioner):
ss.md(code) |