Skip to content

Commit 67a2c40

Browse files
committed
doc2
1 parent 94d73c0 commit 67a2c40

File tree

13 files changed

+4907
-370
lines changed

13 files changed

+4907
-370
lines changed

docs/gallery/3d.ipynb

Lines changed: 98 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,195 +4,264 @@
44
"cell_type": "markdown",
55
"id": "cell-0",
66
"metadata": {},
7-
"source": ["# 3D Visualizations\n", "\n", "ggplotly provides geoms for creating interactive 3D plots powered by Plotly's WebGL renderer."]
7+
"source": [
8+
"# 3D Visualizations\n",
9+
"\n",
10+
"ggplotly provides geoms for creating interactive 3D plots powered by Plotly's WebGL renderer."
11+
]
812
},
913
{
1014
"cell_type": "markdown",
1115
"id": "cell-1",
1216
"metadata": {},
13-
"source": ["## 3D Scatter Plots\n", "\n", "### Basic 3D Scatter"]
17+
"source": [
18+
"## 3D Scatter Plots\n",
19+
"\n",
20+
"### Basic 3D Scatter"
21+
]
1422
},
1523
{
1624
"cell_type": "code",
1725
"execution_count": null,
1826
"id": "cell-2",
1927
"metadata": {},
2028
"outputs": [],
21-
"source": ["import numpy as np\n", "import pandas as pd\n", "from ggplotly import *\n", "\n", "df = pd.DataFrame({\n", " 'x': np.random.randn(200),\n", " 'y': np.random.randn(200),\n", " 'z': np.random.randn(200)\n", "})\n", "\n", "(ggplot(df, aes(x='x', y='y', z='z')) + geom_point_3d()).draw()"]
29+
"source": "import numpy as np\nimport pandas as pd\nfrom ggplotly import *\n\ndf = pd.DataFrame({\n 'x': np.random.randn(200),\n 'y': np.random.randn(200),\n 'z': np.random.randn(200)\n})\n\n(ggplot(df, aes(x='x', y='y', z='z')) + geom_point_3d())"
2230
},
2331
{
2432
"cell_type": "markdown",
2533
"id": "cell-3",
2634
"metadata": {},
27-
"source": ["### Colored by Group"]
35+
"source": [
36+
"### Colored by Group"
37+
]
2838
},
2939
{
3040
"cell_type": "code",
3141
"execution_count": null,
3242
"id": "cell-4",
3343
"metadata": {},
3444
"outputs": [],
35-
"source": ["df = pd.DataFrame({\n", " 'x': np.random.randn(200),\n", " 'y': np.random.randn(200),\n", " 'z': np.random.randn(200),\n", " 'group': np.random.choice(['A', 'B', 'C'], 200)\n", "})\n", "\n", "(ggplot(df, aes(x='x', y='y', z='z', color='group')) + geom_point_3d(size=6)).draw()"]
45+
"source": "df = pd.DataFrame({\n 'x': np.random.randn(200),\n 'y': np.random.randn(200),\n 'z': np.random.randn(200),\n 'group': np.random.choice(['A', 'B', 'C'], 200)\n})\n\n(ggplot(df, aes(x='x', y='y', z='z', color='group')) + geom_point_3d(size=6))"
3646
},
3747
{
3848
"cell_type": "markdown",
3949
"id": "cell-5",
4050
"metadata": {},
41-
"source": ["### Colored by Continuous Variable"]
51+
"source": [
52+
"### Colored by Continuous Variable"
53+
]
4254
},
4355
{
4456
"cell_type": "code",
4557
"execution_count": null,
4658
"id": "cell-6",
4759
"metadata": {},
4860
"outputs": [],
49-
"source": ["df = pd.DataFrame({\n", " 'x': np.random.randn(200),\n", " 'y': np.random.randn(200),\n", " 'z': np.random.randn(200),\n", " 'value': np.random.rand(200) * 100\n", "})\n", "\n", "(ggplot(df, aes(x='x', y='y', z='z', color='value'))\n", " + geom_point_3d(size=5)\n", " + scale_color_gradient(low='blue', high='red')).draw()"]
61+
"source": "df = pd.DataFrame({\n 'x': np.random.randn(200),\n 'y': np.random.randn(200),\n 'z': np.random.randn(200),\n 'value': np.random.rand(200) * 100\n})\n\n(ggplot(df, aes(x='x', y='y', z='z', color='value'))\n + geom_point_3d(size=5)\n + scale_color_gradient(low='blue', high='red'))"
5062
},
5163
{
5264
"cell_type": "markdown",
5365
"id": "cell-7",
5466
"metadata": {},
55-
"source": ["## 3D Surfaces\n", "\n", "### Creating Surface Data\n", "\n", "Surfaces require gridded data. Here's a helper function:"]
67+
"source": [
68+
"## 3D Surfaces\n",
69+
"\n",
70+
"### Creating Surface Data\n",
71+
"\n",
72+
"Surfaces require gridded data. Here's a helper function:"
73+
]
5674
},
5775
{
5876
"cell_type": "code",
5977
"execution_count": null,
6078
"id": "cell-8",
6179
"metadata": {},
6280
"outputs": [],
63-
"source": ["def make_surface(func, x_range=(-5, 5), y_range=(-5, 5), resolution=50):\n", " \"\"\"Generate surface data from a function z = f(x, y).\"\"\"\n", " x = np.linspace(x_range[0], x_range[1], resolution)\n", " y = np.linspace(y_range[0], y_range[1], resolution)\n", " X, Y = np.meshgrid(x, y)\n", " Z = func(X, Y)\n", " return pd.DataFrame({\n", " 'x': X.flatten(),\n", " 'y': Y.flatten(),\n", " 'z': Z.flatten()\n", " })"]
81+
"source": "def make_surface(func, x_range=(-5, 5), y_range=(-5, 5), resolution=50):\n \"\"\"Generate surface data from a function z = f(x, y).\"\"\"\n x = np.linspace(x_range[0], x_range[1], resolution)\n y = np.linspace(y_range[0], y_range[1], resolution)\n X, Y = np.meshgrid(x, y)\n Z = func(X, Y)\n return pd.DataFrame({\n 'x': X.flatten(),\n 'y': Y.flatten(),\n 'z': Z.flatten()\n })"
6482
},
6583
{
6684
"cell_type": "markdown",
6785
"id": "cell-9",
6886
"metadata": {},
69-
"source": ["### Paraboloid"]
87+
"source": [
88+
"### Paraboloid"
89+
]
7090
},
7191
{
7292
"cell_type": "code",
7393
"execution_count": null,
7494
"id": "cell-10",
7595
"metadata": {},
7696
"outputs": [],
77-
"source": ["df = make_surface(lambda x, y: x**2 + y**2)\n", "(ggplot(df, aes(x='x', y='y', z='z')) + geom_surface(colorscale='Viridis')).draw()"]
97+
"source": "df = make_surface(lambda x, y: x**2 + y**2)\n(ggplot(df, aes(x='x', y='y', z='z')) + geom_surface(colorscale='Viridis'))"
7898
},
7999
{
80100
"cell_type": "markdown",
81101
"id": "cell-11",
82102
"metadata": {},
83-
"source": ["### Saddle Surface (Hyperbolic Paraboloid)"]
103+
"source": [
104+
"### Saddle Surface (Hyperbolic Paraboloid)"
105+
]
84106
},
85107
{
86108
"cell_type": "code",
87109
"execution_count": null,
88110
"id": "cell-12",
89111
"metadata": {},
90112
"outputs": [],
91-
"source": ["df = make_surface(lambda x, y: x**2 - y**2)\n", "\n", "(ggplot(df, aes(x='x', y='y', z='z'))\n", " + geom_surface(colorscale='RdBu')\n", " + labs(title='Saddle Surface')).draw()"]
113+
"source": "df = make_surface(lambda x, y: x**2 - y**2)\n\n(ggplot(df, aes(x='x', y='y', z='z'))\n + geom_surface(colorscale='RdBu')\n + labs(title='Saddle Surface'))"
92114
},
93115
{
94116
"cell_type": "markdown",
95117
"id": "cell-13",
96118
"metadata": {},
97-
"source": ["### Sinc Function (2D)"]
119+
"source": [
120+
"### Sinc Function (2D)"
121+
]
98122
},
99123
{
100124
"cell_type": "code",
101125
"execution_count": null,
102126
"id": "cell-14",
103127
"metadata": {},
104128
"outputs": [],
105-
"source": ["def sinc_2d(x, y):\n", " r = np.sqrt(x**2 + y**2)\n", " return np.where(r == 0, 1, np.sin(r) / r)\n", "\n", "df = make_surface(sinc_2d, x_range=(-10, 10), y_range=(-10, 10), resolution=80)\n", "\n", "(ggplot(df, aes(x='x', y='y', z='z'))\n", " + geom_surface(colorscale='Plasma')\n", " + labs(title='2D Sinc Function')).draw()"]
129+
"source": "def sinc_2d(x, y):\n r = np.sqrt(x**2 + y**2)\n return np.where(r == 0, 1, np.sin(r) / r)\n\ndf = make_surface(sinc_2d, x_range=(-10, 10), y_range=(-10, 10), resolution=80)\n\n(ggplot(df, aes(x='x', y='y', z='z'))\n + geom_surface(colorscale='Plasma')\n + labs(title='2D Sinc Function'))"
106130
},
107131
{
108132
"cell_type": "markdown",
109133
"id": "cell-15",
110134
"metadata": {},
111-
"source": ["### Trigonometric Surface"]
135+
"source": [
136+
"### Trigonometric Surface"
137+
]
112138
},
113139
{
114140
"cell_type": "code",
115141
"execution_count": null,
116142
"id": "cell-16",
117143
"metadata": {},
118144
"outputs": [],
119-
"source": ["df = make_surface(lambda x, y: np.sin(x) * np.cos(y))\n", "\n", "(ggplot(df, aes(x='x', y='y', z='z'))\n", " + geom_surface(colorscale='Viridis')\n", " + labs(title='sin(x) * cos(y)')).draw()"]
145+
"source": "df = make_surface(lambda x, y: np.sin(x) * np.cos(y))\n\n(ggplot(df, aes(x='x', y='y', z='z'))\n + geom_surface(colorscale='Viridis')\n + labs(title='sin(x) * cos(y)'))"
120146
},
121147
{
122148
"cell_type": "markdown",
123149
"id": "cell-17",
124150
"metadata": {},
125-
"source": ["### Surface Colorscales\n", "\n", "Available colorscales for `geom_surface`:\n", "\n", "- **Sequential**: `Viridis`, `Plasma`, `Inferno`, `Magma`, `Cividis`, `Blues`, `Greens`, `Reds`, `YlOrRd`, `YlGnBu`\n", "- **Diverging**: `RdBu`, `RdYlBu`, `RdYlGn`, `BrBG`, `PiYG`, `PRGn`, `Spectral`\n", "- **Other**: `Jet`, `Hot`, `Electric`, `Blackbody`, `Earth`, `Picnic`, `Portland`\n", "\n", "## Wireframe Plots\n", "\n", "Wireframes show the surface structure without solid fills:"]
151+
"source": [
152+
"### Surface Colorscales\n",
153+
"\n",
154+
"Available colorscales for `geom_surface`:\n",
155+
"\n",
156+
"- **Sequential**: `Viridis`, `Plasma`, `Inferno`, `Magma`, `Cividis`, `Blues`, `Greens`, `Reds`, `YlOrRd`, `YlGnBu`\n",
157+
"- **Diverging**: `RdBu`, `RdYlBu`, `RdYlGn`, `BrBG`, `PiYG`, `PRGn`, `Spectral`\n",
158+
"- **Other**: `Jet`, `Hot`, `Electric`, `Blackbody`, `Earth`, `Picnic`, `Portland`\n",
159+
"\n",
160+
"## Wireframe Plots\n",
161+
"\n",
162+
"Wireframes show the surface structure without solid fills:"
163+
]
126164
},
127165
{
128166
"cell_type": "code",
129167
"execution_count": null,
130168
"id": "cell-18",
131169
"metadata": {},
132170
"outputs": [],
133-
"source": ["df = make_surface(lambda x, y: np.sin(x) * np.cos(y), resolution=30)\n", "\n", "(ggplot(df, aes(x='x', y='y', z='z'))\n", " + geom_wireframe(color='steelblue', linewidth=1)\n", " + labs(title='Wireframe Plot')).draw()"]
171+
"source": "df = make_surface(lambda x, y: np.sin(x) * np.cos(y), resolution=30)\n\n(ggplot(df, aes(x='x', y='y', z='z'))\n + geom_wireframe(color='steelblue', linewidth=1)\n + labs(title='Wireframe Plot'))"
134172
},
135173
{
136174
"cell_type": "markdown",
137175
"id": "cell-19",
138176
"metadata": {},
139-
"source": ["### Wireframe Parameters\n", "\n", "| Parameter | Default | Description |\n", "|-----------|---------|-------------|\n", "| `color` | 'steelblue' | Line color |\n", "| `linewidth` | 1 | Line width |\n", "| `opacity` | 1.0 | Transparency (0-1) |\n", "\n", "## Combining 3D Geoms\n", "\n", "You can layer 3D geoms:"]
177+
"source": [
178+
"### Wireframe Parameters\n",
179+
"\n",
180+
"| Parameter | Default | Description |\n",
181+
"|-----------|---------|-------------|\n",
182+
"| `color` | 'steelblue' | Line color |\n",
183+
"| `linewidth` | 1 | Line width |\n",
184+
"| `opacity` | 1.0 | Transparency (0-1) |\n",
185+
"\n",
186+
"## Combining 3D Geoms\n",
187+
"\n",
188+
"You can layer 3D geoms:"
189+
]
140190
},
141191
{
142192
"cell_type": "code",
143193
"execution_count": null,
144194
"id": "cell-20",
145195
"metadata": {},
146196
"outputs": [],
147-
"source": ["# Surface with scatter points\n", "df_surface = make_surface(lambda x, y: np.sin(x) * np.cos(y))\n", "\n", "# Sample points on the surface\n", "sample_idx = np.random.choice(len(df_surface), 50, replace=False)\n", "df_points = df_surface.iloc[sample_idx].copy()\n", "df_points['z'] = df_points['z'] + 0.1 # Offset slightly above surface\n", "\n", "(ggplot(df_surface, aes(x='x', y='y', z='z'))\n", " + geom_surface(colorscale='Viridis', opacity=0.7)\n", " + geom_point_3d(data=df_points, color='red', size=5)).draw()"]
197+
"source": "# Surface with scatter points\ndf_surface = make_surface(lambda x, y: np.sin(x) * np.cos(y))\n\n# Sample points on the surface\nsample_idx = np.random.choice(len(df_surface), 50, replace=False)\ndf_points = df_surface.iloc[sample_idx].copy()\ndf_points['z'] = df_points['z'] + 0.1 # Offset slightly above surface\n\n(ggplot(df_surface, aes(x='x', y='y', z='z'))\n + geom_surface(colorscale='Viridis', opacity=0.7)\n + geom_point_3d(data=df_points, color='red', size=5))"
148198
},
149199
{
150200
"cell_type": "markdown",
151201
"id": "cell-21",
152202
"metadata": {},
153-
"source": ["## Mathematical Visualizations\n", "\n", "### Gaussian (Bell Curve) in 3D"]
203+
"source": [
204+
"## Mathematical Visualizations\n",
205+
"\n",
206+
"### Gaussian (Bell Curve) in 3D"
207+
]
154208
},
155209
{
156210
"cell_type": "code",
157211
"execution_count": null,
158212
"id": "cell-22",
159213
"metadata": {},
160214
"outputs": [],
161-
"source": ["def gaussian_2d(x, y, sigma=1):\n", " return np.exp(-(x**2 + y**2) / (2 * sigma**2))\n", "\n", "df = make_surface(gaussian_2d, x_range=(-3, 3), y_range=(-3, 3), resolution=60)\n", "\n", "(ggplot(df, aes(x='x', y='y', z='z'))\n", " + geom_surface(colorscale='Viridis')\n", " + labs(title='2D Gaussian Distribution')).draw()"]
215+
"source": "def gaussian_2d(x, y, sigma=1):\n return np.exp(-(x**2 + y**2) / (2 * sigma**2))\n\ndf = make_surface(gaussian_2d, x_range=(-3, 3), y_range=(-3, 3), resolution=60)\n\n(ggplot(df, aes(x='x', y='y', z='z'))\n + geom_surface(colorscale='Viridis')\n + labs(title='2D Gaussian Distribution'))"
162216
},
163217
{
164218
"cell_type": "markdown",
165219
"id": "cell-23",
166220
"metadata": {},
167-
"source": ["### Ripple Effect"]
221+
"source": [
222+
"### Ripple Effect"
223+
]
168224
},
169225
{
170226
"cell_type": "code",
171227
"execution_count": null,
172228
"id": "cell-24",
173229
"metadata": {},
174230
"outputs": [],
175-
"source": ["def ripple(x, y):\n", " r = np.sqrt(x**2 + y**2)\n", " return np.sin(3 * r) * np.exp(-0.3 * r)\n", "\n", "df = make_surface(ripple, x_range=(-5, 5), y_range=(-5, 5), resolution=80)\n", "\n", "(ggplot(df, aes(x='x', y='y', z='z'))\n", " + geom_surface(colorscale='RdBu')\n", " + labs(title='Ripple Effect')).draw()"]
231+
"source": "def ripple(x, y):\n r = np.sqrt(x**2 + y**2)\n return np.sin(3 * r) * np.exp(-0.3 * r)\n\ndf = make_surface(ripple, x_range=(-5, 5), y_range=(-5, 5), resolution=80)\n\n(ggplot(df, aes(x='x', y='y', z='z'))\n + geom_surface(colorscale='RdBu')\n + labs(title='Ripple Effect'))"
176232
},
177233
{
178234
"cell_type": "markdown",
179235
"id": "cell-25",
180236
"metadata": {},
181-
"source": ["### Rosenbrock Function (Optimization Test)"]
237+
"source": [
238+
"### Rosenbrock Function (Optimization Test)"
239+
]
182240
},
183241
{
184242
"cell_type": "code",
185243
"execution_count": null,
186244
"id": "cell-26",
187245
"metadata": {},
188246
"outputs": [],
189-
"source": ["def rosenbrock(x, y, a=1, b=100):\n", " return (a - x)**2 + b * (y - x**2)**2\n", "\n", "df = make_surface(rosenbrock, x_range=(-2, 2), y_range=(-1, 3), resolution=60)\n", "\n", "(ggplot(df, aes(x='x', y='y', z='z'))\n", " + geom_surface(colorscale='Hot')\n", " + labs(title='Rosenbrock Function')).draw()"]
247+
"source": "def rosenbrock(x, y, a=1, b=100):\n return (a - x)**2 + b * (y - x**2)**2\n\ndf = make_surface(rosenbrock, x_range=(-2, 2), y_range=(-1, 3), resolution=60)\n\n(ggplot(df, aes(x='x', y='y', z='z'))\n + geom_surface(colorscale='Hot')\n + labs(title='Rosenbrock Function'))"
190248
},
191249
{
192250
"cell_type": "markdown",
193251
"id": "cell-27",
194252
"metadata": {},
195-
"source": ["## Interactivity\n", "\n", "All 3D plots support:\n", "\n", "- **Rotation**: Click and drag to rotate\n", "- **Zoom**: Scroll wheel or pinch\n", "- **Pan**: Shift + drag\n", "- **Reset**: Double-click\n", "\n", "The 3D camera position is automatically saved when you interact, so subsequent renders maintain your viewpoint."]
253+
"source": [
254+
"## Interactivity\n",
255+
"\n",
256+
"All 3D plots support:\n",
257+
"\n",
258+
"- **Rotation**: Click and drag to rotate\n",
259+
"- **Zoom**: Scroll wheel or pinch\n",
260+
"- **Pan**: Shift + drag\n",
261+
"- **Reset**: Double-click\n",
262+
"\n",
263+
"The 3D camera position is automatically saved when you interact, so subsequent renders maintain your viewpoint."
264+
]
196265
}
197266
],
198267
"metadata": {
@@ -208,4 +277,4 @@
208277
},
209278
"nbformat": 4,
210279
"nbformat_minor": 5
211-
}
280+
}

0 commit comments

Comments
 (0)