|
| 1 | +""" pyplots.ai |
| 2 | +line-styled: Styled Line Plot |
| 3 | +Library: bokeh 3.8.1 | Python 3.13.11 |
| 4 | +Quality: 92/100 | Created: 2025-12-30 |
| 5 | +""" |
| 6 | + |
| 7 | +import numpy as np |
| 8 | +from bokeh.io import export_png, output_file, save |
| 9 | +from bokeh.models import ColumnDataSource, Legend |
| 10 | +from bokeh.plotting import figure |
| 11 | + |
| 12 | + |
| 13 | +# Data - Monthly performance metrics over a year |
| 14 | +np.random.seed(42) |
| 15 | +months = np.arange(1, 13) |
| 16 | + |
| 17 | +# Generate realistic trending data for different metrics |
| 18 | +base = np.array([65, 68, 72, 75, 78, 82, 85, 84, 80, 77, 74, 70]) |
| 19 | +cpu_usage = base + np.random.randn(12) * 3 |
| 20 | +memory_usage = base * 0.85 + np.random.randn(12) * 4 + 10 |
| 21 | +disk_io = base * 0.7 + np.random.randn(12) * 5 + 20 |
| 22 | +network = base * 1.1 + np.random.randn(12) * 2 - 5 |
| 23 | + |
| 24 | +# Create ColumnDataSource |
| 25 | +source = ColumnDataSource( |
| 26 | + data={"month": months, "cpu": cpu_usage, "memory": memory_usage, "disk": disk_io, "network": network} |
| 27 | +) |
| 28 | + |
| 29 | +# Create figure (4800 × 2700 px) |
| 30 | +p = figure( |
| 31 | + width=4800, |
| 32 | + height=2700, |
| 33 | + title="line-styled · bokeh · pyplots.ai", |
| 34 | + x_axis_label="Month", |
| 35 | + y_axis_label="Utilization (%)", |
| 36 | +) |
| 37 | + |
| 38 | +# Define line styles and colors |
| 39 | +# Using solid, dashed, dotted, and dash-dot patterns with pronounced differences |
| 40 | +line_styles = ["solid", [20, 10], [4, 8], [20, 8, 4, 8]] |
| 41 | +colors = ["#306998", "#FFD43B", "#4CAF50", "#FF5722"] |
| 42 | +series_names = ["CPU Usage", "Memory Usage", "Disk I/O", "Network Traffic"] |
| 43 | +y_columns = ["cpu", "memory", "disk", "network"] |
| 44 | + |
| 45 | +# Create legend items |
| 46 | +legend_items = [] |
| 47 | + |
| 48 | +for col, style, color, name in zip(y_columns, line_styles, colors, series_names, strict=True): |
| 49 | + # Add line with appropriate style |
| 50 | + line = p.line(x="month", y=col, source=source, line_width=6, color=color, line_dash=style) |
| 51 | + |
| 52 | + # Add scatter points for better visibility |
| 53 | + scatter = p.scatter(x="month", y=col, source=source, size=25, color=color, alpha=0.9) |
| 54 | + |
| 55 | + legend_items.append((name, [line, scatter])) |
| 56 | + |
| 57 | +# Create and configure legend - place inside plot area |
| 58 | +legend = Legend(items=legend_items, location="top_left") |
| 59 | +legend.label_text_font_size = "28pt" |
| 60 | +legend.glyph_height = 40 |
| 61 | +legend.glyph_width = 80 |
| 62 | +legend.spacing = 15 |
| 63 | +legend.padding = 20 |
| 64 | +legend.background_fill_alpha = 0.85 |
| 65 | +legend.background_fill_color = "white" |
| 66 | +legend.border_line_color = "#cccccc" |
| 67 | +legend.border_line_width = 2 |
| 68 | +p.add_layout(legend, "center") |
| 69 | +p.legend.location = "top_left" |
| 70 | + |
| 71 | +# Style configuration - larger fonts for 4800x2700 canvas |
| 72 | +p.title.text_font_size = "48pt" |
| 73 | +p.title.align = "center" |
| 74 | +p.xaxis.axis_label_text_font_size = "36pt" |
| 75 | +p.yaxis.axis_label_text_font_size = "36pt" |
| 76 | +p.xaxis.major_label_text_font_size = "28pt" |
| 77 | +p.yaxis.major_label_text_font_size = "28pt" |
| 78 | + |
| 79 | +# Grid styling - subtle |
| 80 | +p.grid.grid_line_alpha = 0.3 |
| 81 | +p.grid.grid_line_dash = "dashed" |
| 82 | + |
| 83 | +# Axis styling |
| 84 | +p.xaxis.ticker = list(range(1, 13)) |
| 85 | +p.xaxis.major_label_overrides = { |
| 86 | + 1: "Jan", |
| 87 | + 2: "Feb", |
| 88 | + 3: "Mar", |
| 89 | + 4: "Apr", |
| 90 | + 5: "May", |
| 91 | + 6: "Jun", |
| 92 | + 7: "Jul", |
| 93 | + 8: "Aug", |
| 94 | + 9: "Sep", |
| 95 | + 10: "Oct", |
| 96 | + 11: "Nov", |
| 97 | + 12: "Dec", |
| 98 | +} |
| 99 | + |
| 100 | +# Background and outline |
| 101 | +p.background_fill_color = "#fafafa" |
| 102 | +p.border_fill_color = "#ffffff" |
| 103 | +p.outline_line_color = "#333333" |
| 104 | + |
| 105 | +# Axis line styling |
| 106 | +p.xaxis.axis_line_width = 2 |
| 107 | +p.yaxis.axis_line_width = 2 |
| 108 | +p.xaxis.major_tick_line_width = 2 |
| 109 | +p.yaxis.major_tick_line_width = 2 |
| 110 | + |
| 111 | +# Save as PNG |
| 112 | +export_png(p, filename="plot.png") |
| 113 | + |
| 114 | +# Save as HTML for interactivity |
| 115 | +output_file("plot.html") |
| 116 | +save(p) |
0 commit comments