|
1 | 1 | """ pyplots.ai |
2 | 2 | qrcode-basic: Basic QR Code Generator |
3 | | -Library: plotly 6.5.0 | Python 3.13.11 |
4 | | -Quality: 91/100 | Created: 2026-01-07 |
| 3 | +Library: plotly 6.6.0 | Python 3.14.3 |
| 4 | +Quality: 90/100 | Updated: 2026-04-07 |
5 | 5 | """ |
6 | 6 |
|
7 | 7 | import numpy as np |
|
11 | 11 |
|
12 | 12 | # Data - Generate QR code for pyplots.ai |
13 | 13 | content = "https://pyplots.ai" |
14 | | -error_correction = qrcode.constants.ERROR_CORRECT_M # 15% error correction |
15 | 14 |
|
16 | | -qr = qrcode.QRCode( |
17 | | - version=1, |
18 | | - error_correction=error_correction, |
19 | | - box_size=1, |
20 | | - border=4, # Quiet zone |
21 | | -) |
| 15 | +qr = qrcode.QRCode(version=None, error_correction=qrcode.constants.ERROR_CORRECT_M, box_size=1, border=4) |
22 | 16 | qr.add_data(content) |
23 | 17 | qr.make(fit=True) |
24 | 18 |
|
25 | | -# Convert QR code to numpy array (0=white, 1=black) |
26 | 19 | qr_matrix = np.array(qr.get_matrix(), dtype=int) |
| 20 | +n_modules = qr_matrix.shape[0] |
27 | 21 |
|
28 | | -# Invert so black=1, white=0 for proper display |
29 | | -qr_display = 1 - qr_matrix |
30 | | - |
31 | | -# Create figure with heatmap |
| 22 | +# Plot - Render as heatmap with custom colorscale |
32 | 23 | fig = go.Figure( |
33 | | - data=go.Heatmap(z=qr_display, colorscale=[[0, "#000000"], [1, "#FFFFFF"]], showscale=False, xgap=0, ygap=0) |
| 24 | + data=go.Heatmap( |
| 25 | + z=qr_matrix, |
| 26 | + colorscale=[[0, "#FFFFFF"], [1, "#1a1a2e"]], |
| 27 | + showscale=False, |
| 28 | + xgap=0.3, |
| 29 | + ygap=0.3, |
| 30 | + hovertemplate="Module: (%{x}, %{y})<br>Value: %{z}<extra></extra>", |
| 31 | + ) |
34 | 32 | ) |
35 | 33 |
|
36 | | -# Layout for square display |
| 34 | +# Style - tighter layout for better canvas utilization |
37 | 35 | fig.update_layout( |
38 | 36 | title={ |
39 | 37 | "text": "qrcode-basic · plotly · pyplots.ai", |
40 | | - "font": {"size": 28, "color": "#306998"}, |
| 38 | + "font": {"size": 64, "color": "#306998", "family": "Arial Black, sans-serif"}, |
41 | 39 | "x": 0.5, |
42 | 40 | "xanchor": "center", |
| 41 | + "y": 0.97, |
| 42 | + }, |
| 43 | + xaxis={ |
| 44 | + "showticklabels": False, |
| 45 | + "showgrid": False, |
| 46 | + "zeroline": False, |
| 47 | + "scaleanchor": "y", |
| 48 | + "scaleratio": 1, |
| 49 | + "constrain": "domain", |
| 50 | + "showline": False, |
| 51 | + "range": [-0.5, n_modules - 0.5], |
43 | 52 | }, |
44 | | - xaxis={"showticklabels": False, "showgrid": False, "zeroline": False, "scaleanchor": "y", "scaleratio": 1}, |
45 | 53 | yaxis={ |
46 | 54 | "showticklabels": False, |
47 | 55 | "showgrid": False, |
48 | 56 | "zeroline": False, |
49 | | - "autorange": "reversed", # Flip to show QR code correctly |
| 57 | + "autorange": "reversed", |
| 58 | + "constrain": "domain", |
| 59 | + "showline": False, |
| 60 | + "range": [-0.5, n_modules - 0.5], |
50 | 61 | }, |
51 | 62 | template="plotly_white", |
52 | | - margin={"l": 150, "r": 150, "t": 200, "b": 350}, |
53 | | - paper_bgcolor="white", |
| 63 | + margin={"l": 60, "r": 60, "t": 220, "b": 260}, |
| 64 | + paper_bgcolor="#f8f9fa", |
54 | 65 | plot_bgcolor="white", |
55 | 66 | ) |
56 | 67 |
|
57 | | -# Add annotations below the plot |
| 68 | +# Decorative border around the QR code area |
| 69 | +fig.add_shape( |
| 70 | + type="rect", |
| 71 | + xref="paper", |
| 72 | + yref="paper", |
| 73 | + x0=-0.01, |
| 74 | + y0=-0.01, |
| 75 | + x1=1.01, |
| 76 | + y1=1.01, |
| 77 | + line={"color": "#306998", "width": 1, "dash": "solid"}, |
| 78 | + fillcolor="rgba(0,0,0,0)", |
| 79 | + opacity=0.3, |
| 80 | +) |
| 81 | + |
| 82 | +# Subtle divider line between QR code and annotations |
| 83 | +fig.add_shape( |
| 84 | + type="line", |
| 85 | + xref="paper", |
| 86 | + yref="paper", |
| 87 | + x0=0.3, |
| 88 | + y0=-0.01, |
| 89 | + x1=0.7, |
| 90 | + y1=-0.01, |
| 91 | + line={"color": "#306998", "width": 1.5, "dash": "dot"}, |
| 92 | +) |
| 93 | + |
| 94 | +# Content URL annotation - prominent, below plot area |
58 | 95 | fig.add_annotation( |
59 | | - text=f"Content: {content}", |
| 96 | + text=f"<b>{content}</b>", |
60 | 97 | xref="paper", |
61 | 98 | yref="paper", |
62 | 99 | x=0.5, |
63 | | - y=-0.05, |
| 100 | + y=-0.025, |
64 | 101 | showarrow=False, |
65 | | - font={"size": 28, "color": "#666666"}, |
| 102 | + font={"size": 48, "color": "#306998", "family": "Arial, sans-serif"}, |
66 | 103 | xanchor="center", |
67 | 104 | yanchor="top", |
68 | 105 | ) |
| 106 | + |
| 107 | +# Error correction and version info |
69 | 108 | fig.add_annotation( |
70 | | - text="Error Correction: M (15%)", |
| 109 | + text=f"Error Correction: M (15%) · Version {qr.version} · {n_modules}×{n_modules} modules", |
71 | 110 | xref="paper", |
72 | 111 | yref="paper", |
73 | 112 | x=0.5, |
74 | | - y=-0.10, |
| 113 | + y=-0.055, |
75 | 114 | showarrow=False, |
76 | | - font={"size": 24, "color": "#888888"}, |
| 115 | + font={"size": 38, "color": "#888888", "family": "Arial, sans-serif"}, |
77 | 116 | xanchor="center", |
78 | 117 | yanchor="top", |
79 | 118 | ) |
80 | 119 |
|
81 | | -# Save outputs |
| 120 | +# Save |
82 | 121 | fig.write_image("plot.png", width=3600, height=3600, scale=1) |
83 | | -fig.write_html("plot.html") |
| 122 | +fig.write_html("plot.html", include_plotlyjs="cdn") |
0 commit comments