|
| 1 | +""" |
| 2 | +scatter-color-groups: Scatter Plot with Color Groups |
| 3 | +Library: bokeh |
| 4 | +""" |
| 5 | + |
| 6 | +import seaborn as sns |
| 7 | +from bokeh.io import export_png |
| 8 | +from bokeh.models import ColumnDataSource |
| 9 | +from bokeh.plotting import figure |
| 10 | + |
| 11 | + |
| 12 | +# Data |
| 13 | +data = sns.load_dataset("iris") |
| 14 | + |
| 15 | +# Color palette (from style guide) |
| 16 | +colors = ["#306998", "#FFD43B", "#DC2626", "#059669", "#8B5CF6", "#F97316"] |
| 17 | +species_list = data["species"].unique().tolist() |
| 18 | +color_map = {species: colors[i] for i, species in enumerate(species_list)} |
| 19 | +data["color"] = data["species"].map(color_map) |
| 20 | + |
| 21 | +# Create figure |
| 22 | +p = figure( |
| 23 | + width=4800, |
| 24 | + height=2700, |
| 25 | + title="Scatter Plot with Color Groups", |
| 26 | + x_axis_label="Sepal Length (cm)", |
| 27 | + y_axis_label="Sepal Width (cm)", |
| 28 | +) |
| 29 | + |
| 30 | +# Plot each group separately for legend |
| 31 | +for species in species_list: |
| 32 | + species_data = data[data["species"] == species] |
| 33 | + source = ColumnDataSource(data={"x": species_data["sepal_length"], "y": species_data["sepal_width"]}) |
| 34 | + p.scatter( |
| 35 | + x="x", y="y", source=source, size=12, alpha=0.7, color=color_map[species], legend_label=species.capitalize() |
| 36 | + ) |
| 37 | + |
| 38 | +# Styling |
| 39 | +p.title.text_font_size = "20pt" |
| 40 | +p.xaxis.axis_label_text_font_size = "20pt" |
| 41 | +p.yaxis.axis_label_text_font_size = "20pt" |
| 42 | +p.xaxis.major_label_text_font_size = "16pt" |
| 43 | +p.yaxis.major_label_text_font_size = "16pt" |
| 44 | +p.legend.label_text_font_size = "16pt" |
| 45 | +p.legend.location = "top_right" |
| 46 | +p.grid.grid_line_alpha = 0.3 |
| 47 | + |
| 48 | +# Save |
| 49 | +export_png(p, filename="plot.png") |
0 commit comments