-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaltair.py
More file actions
141 lines (128 loc) · 4.18 KB
/
altair.py
File metadata and controls
141 lines (128 loc) · 4.18 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
""" pyplots.ai
heatmap-basic: Basic Heatmap
Library: altair 6.0.0 | Python 3.14.3
Quality: 88/100 | Updated: 2026-02-16
"""
import altair as alt
import numpy as np
import pandas as pd
# Data - correlation matrix with realistic weather variables
np.random.seed(42)
variables = [
"Temperature",
"Humidity",
"Wind Speed",
"Pressure",
"Visibility",
"Cloud Cover",
"Precipitation",
"UV Index",
]
n_samples = 200
raw = np.random.randn(n_samples, len(variables))
# Inject realistic correlations with stronger relationships
raw[:, 1] += raw[:, 0] * 0.6 # Humidity ~ Temperature
raw[:, 5] += raw[:, 1] * 0.7 # Cloud Cover ~ Humidity
raw[:, 6] += raw[:, 5] * 0.65 # Precipitation ~ Cloud Cover
raw[:, 4] -= raw[:, 5] * 0.9 # Visibility inversely ~ Cloud Cover
raw[:, 7] -= raw[:, 5] * 0.7 # UV Index inversely ~ Cloud Cover
raw[:, 7] += raw[:, 0] * 0.5 # UV Index ~ Temperature
raw[:, 3] -= raw[:, 0] * 0.4 # Pressure inversely ~ Temperature
raw[:, 2] += raw[:, 3] * 0.3 # Wind Speed ~ Pressure
corr = np.corrcoef(raw.T)
# Build long-form dataframe using list comprehension
df = pd.DataFrame(
[
{"Row": row_var, "Column": col_var, "value": round(corr[i, j], 2)}
for i, row_var in enumerate(variables)
for j, col_var in enumerate(variables)
]
)
# Axis ordering
axis_order = list(variables)
# Plot - heatmap with colorblind-safe diverging color centered at 0
heatmap = (
alt.Chart(df)
.mark_rect(stroke="#ffffff", strokeWidth=1.5, cornerRadius=2)
.encode(
x=alt.X(
"Column:N",
title="Weather Variable",
sort=axis_order,
axis=alt.Axis(
labelFontSize=15, titleFontSize=18, labelAngle=-45, orient="top", labelPadding=8, titlePadding=10
),
),
y=alt.Y(
"Row:N",
title="Weather Variable",
sort=axis_order,
axis=alt.Axis(labelFontSize=15, titleFontSize=18, labelPadding=8, titlePadding=10),
),
color=alt.Color(
"value:Q",
scale=alt.Scale(scheme="blueorange", domain=[-1, 1], domainMid=0),
legend=alt.Legend(
title="Correlation",
titleFontSize=16,
labelFontSize=14,
gradientLength=300,
gradientThickness=16,
titlePadding=6,
offset=8,
direction="vertical",
),
),
tooltip=[
alt.Tooltip("Column:N", title="Column"),
alt.Tooltip("Row:N", title="Row"),
alt.Tooltip("value:Q", title="Correlation", format=".2f"),
],
)
)
# Highlight cells with strong correlations using thicker borders
highlight = (
alt.Chart(df)
.transform_filter((alt.datum.value >= 0.7) | (alt.datum.value <= -0.7))
.mark_rect(stroke="#2a2a2a", strokeWidth=2.5, filled=False, cornerRadius=2)
.encode(x=alt.X("Column:N", sort=axis_order), y=alt.Y("Row:N", sort=axis_order))
)
# Text annotations with adaptive color
text = (
alt.Chart(df)
.mark_text(fontSize=15, fontWeight="bold")
.encode(
x=alt.X("Column:N", sort=axis_order),
y=alt.Y("Row:N", sort=axis_order),
text=alt.Text("value:Q", format=".2f"),
color=alt.when((alt.datum.value > 0.55) | (alt.datum.value < -0.55))
.then(alt.value("#ffffff"))
.otherwise(alt.value("#333333")),
)
)
# Combine layers and configure
chart = (
(heatmap + highlight + text)
.properties(
width=740,
height=766,
title=alt.Title(
"heatmap-basic · altair · pyplots.ai",
subtitle=[
"Pairwise Pearson correlation coefficients for 8 weather metrics.",
"Bold borders highlight strong relationships (|r| ≥ 0.7).",
],
fontSize=26,
subtitleFontSize=16,
subtitleColor="#666666",
anchor="start",
offset=16,
),
padding={"left": 20, "right": 20, "top": 20, "bottom": 20},
)
.configure_axis(grid=False)
.configure_view(strokeWidth=0)
)
# Save — target: 3600×3600 square format
chart.save("plot.png", scale_factor=3.6)
chart.save("plot.html")