|
176 | 176 | "box_2 = go.Box(y=y1)\n", |
177 | 177 | "data = [heatmap, box_1, box_2]\n", |
178 | 178 | "\n", |
179 | | - "fig = subplots.make_subplots(\n", |
| 179 | + "fig_layout = subplots.make_subplots(\n", |
180 | 180 | " rows=2, cols=2, specs=[[{}, {}], [{'colspan': 2}, None]],\n", |
181 | 181 | " subplot_titles=('First Subplot','Second Subplot', 'Third Subplot')\n", |
182 | 182 | ")\n", |
183 | 183 | "\n", |
184 | | - "fig.append_trace(box_1, 1, 1)\n", |
185 | | - "fig.append_trace(box_2, 1, 2)\n", |
186 | | - "fig.append_trace(heatmap, 2, 1)\n", |
| 184 | + "fig_layout.append_trace(box_1, 1, 1)\n", |
| 185 | + "fig_layout.append_trace(box_2, 1, 2)\n", |
| 186 | + "fig_layout.append_trace(heatmap, 2, 1)\n", |
187 | 187 | "\n", |
188 | | - "fig['layout'].update(height=600, width=600, title='i <3 subplots')\n", |
| 188 | + "fig_layout['layout'].update(height=600, width=600, title='i <3 subplots')\n", |
189 | 189 | "\n", |
190 | | - "# Conversion to dictionary only necessary if you want efficient updates\n", |
191 | | - "fig = fig.to_dict()\n", |
192 | | - "\n", |
193 | | - "subplot_panel = pn.pane.Plotly(fig)\n", |
194 | | - "subplot_panel" |
| 190 | + "pn.pane.Plotly(fig_layout)" |
195 | 191 | ] |
196 | 192 | }, |
197 | 193 | { |
|
220 | 216 | " ('Friday', 4), ('Saturday', 4), ('Sunday', 4)], columns=['Day', 'Orders']\n", |
221 | 217 | ")\n", |
222 | 218 | "\n", |
223 | | - "fig = px.line(data, x=\"Day\", y=\"Orders\")\n", |
224 | | - "fig.update_traces(mode=\"lines+markers\", marker=dict(size=10), line=dict(width=4))\n", |
225 | | - "fig.layout.autosize = True\n", |
| 219 | + "fig_responsive = px.line(data, x=\"Day\", y=\"Orders\")\n", |
| 220 | + "fig_responsive.update_traces(mode=\"lines+markers\", marker=dict(size=10), line=dict(width=4))\n", |
| 221 | + "fig_responsive.layout.autosize = True\n", |
226 | 222 | "\n", |
227 | | - "responsive = pn.pane.Plotly(fig, height=300)\n", |
| 223 | + "responsive = pn.pane.Plotly(fig_responsive, height=300)\n", |
228 | 224 | "\n", |
229 | 225 | "pn.Column('## A responsive plot', responsive, sizing_mode='stretch_width')" |
230 | 226 | ] |
|
244 | 240 | "metadata": {}, |
245 | 241 | "outputs": [], |
246 | 242 | "source": [ |
247 | | - "responsive_with_zoom = pn.pane.Plotly(fig, config={\"scrollZoom\": True}, height=300)\n", |
| 243 | + "responsive_with_zoom = pn.pane.Plotly(fig_responsive, config={\"scrollZoom\": True}, height=300)\n", |
248 | 244 | "\n", |
249 | 245 | "pn.Column('## A responsive and scroll zoomable plot', responsive_with_zoom, sizing_mode='stretch_width')" |
250 | 246 | ] |
|
253 | 249 | "cell_type": "markdown", |
254 | 250 | "metadata": {}, |
255 | 251 | "source": [ |
256 | | - "Try scrolling with the mouse!" |
| 252 | + "Try scrolling with the mouse over the plot!" |
257 | 253 | ] |
258 | 254 | }, |
259 | 255 | { |
|
294 | 290 | " margin=dict(t=50, b=50, r=50, l=50)\n", |
295 | 291 | ")\n", |
296 | 292 | "\n", |
297 | | - "fig = dict(data=[surface], layout=layout)\n", |
| 293 | + "fig_patch = dict(data=[surface], layout=layout)\n", |
298 | 294 | "\n", |
299 | | - "plotly_pane = pn.pane.Plotly(fig)\n", |
300 | | - "plotly_pane" |
| 295 | + "plotly_pane_patch = pn.pane.Plotly(fig_patch)\n", |
| 296 | + "plotly_pane_patch" |
301 | 297 | ] |
302 | 298 | }, |
303 | 299 | { |
|
307 | 303 | "outputs": [], |
308 | 304 | "source": [ |
309 | 305 | "surface.z = np.sin(z+1)\n", |
310 | | - "plotly_pane.object = fig" |
| 306 | + "plotly_pane_patch.object = fig_patch" |
311 | 307 | ] |
312 | 308 | }, |
313 | 309 | { |
|
323 | 319 | "metadata": {}, |
324 | 320 | "outputs": [], |
325 | 321 | "source": [ |
326 | | - "fig['layout']['width'] = 800\n", |
| 322 | + "fig_patch['layout']['width'] = 800\n", |
327 | 323 | "\n", |
328 | | - "plotly_pane.object = fig" |
| 324 | + "plotly_pane_patch.object = fig_patch" |
329 | 325 | ] |
330 | 326 | }, |
331 | 327 | { |
|
342 | 338 | "outputs": [], |
343 | 339 | "source": [ |
344 | 340 | "surface.z = z\n", |
345 | | - "fig['layout']['width'] = 500\n", |
| 341 | + "fig_patch['layout']['width'] = 500\n", |
346 | 342 | "\n", |
347 | | - "plotly_pane.object = fig" |
| 343 | + "plotly_pane_patch.object = fig_patch" |
348 | 344 | ] |
349 | 345 | }, |
350 | 346 | { |
|
386 | 382 | " close=df.loc[:start_index, \"AAPL.Close\"],\n", |
387 | 383 | ")\n", |
388 | 384 | "\n", |
389 | | - "fig = {\"data\": data, \"layout\": go.Layout(xaxis_rangeslider_visible=False)}\n", |
| 385 | + "fig_stream = {\"data\": data, \"layout\": go.Layout(xaxis_rangeslider_visible=False)}\n", |
390 | 386 | "\n", |
391 | | - "plotly_pane = pn.pane.Plotly(fig)\n", |
| 387 | + "plotly_pane_stream = pn.pane.Plotly(fig_stream)\n", |
392 | 388 | "\n", |
393 | 389 | "\n", |
394 | 390 | "def stream():\n", |
|
401 | 397 | " data[\"high\"] = df.loc[:index, \"AAPL.High\"]\n", |
402 | 398 | " data[\"low\"] = df.loc[:index, \"AAPL.Low\"]\n", |
403 | 399 | " data[\"close\"] = df.loc[:index, \"AAPL.Close\"]\n", |
404 | | - " plotly_pane.object = fig\n", |
| 400 | + " plotly_pane_stream.object = fig_stream\n", |
405 | 401 | "\n", |
406 | 402 | "\n", |
407 | 403 | "pn.state.add_periodic_callback(stream, period=100, count=50)\n", |
408 | 404 | "\n", |
409 | | - "plotly_pane" |
| 405 | + "plotly_pane_stream" |
410 | 406 | ] |
411 | 407 | }, |
412 | 408 | { |
|
448 | 444 | "layout = go.Layout(xaxis_rangeslider_visible=False)\n", |
449 | 445 | "\n", |
450 | 446 | "\n", |
451 | | - "async def stream():\n", |
452 | | - " for _ in range(0, 50):\n", |
| 447 | + "async def stream_generator():\n", |
| 448 | + " for _ in range(start_index, start_index+50):\n", |
453 | 449 | " index = len(data.x)\n", |
454 | 450 | " if index == len(df):\n", |
455 | 451 | " index = 0\n", |
|
464 | 460 | " await sleep(0.05)\n", |
465 | 461 | "\n", |
466 | 462 | "\n", |
467 | | - "pn.pane.Plotly(stream)" |
| 463 | + "pn.pane.Plotly(stream_generator)" |
468 | 464 | ] |
469 | 465 | }, |
470 | 466 | { |
|
489 | 485 | "pn.extension(\"plotly\")\n", |
490 | 486 | "\n", |
491 | 487 | "\n", |
492 | | - "fig = go.Figure()\n", |
| 488 | + "fig_in_place = go.Figure()\n", |
493 | 489 | "\n", |
494 | 490 | "button = pn.widgets.Button(name=\"Create\", button_type=\"primary\")\n", |
495 | 491 | "\n", |
|
498 | 494 | " mod = clicks % 3\n", |
499 | 495 | " if mod == 1:\n", |
500 | 496 | " button.name = \"Update\"\n", |
501 | | - " fig.add_scatter(y=[2, 1, 4, 3])\n", |
502 | | - " fig.add_bar(y=[2, 1, 4, 3])\n", |
503 | | - " fig.layout.title = \"New Figure\"\n", |
| 497 | + " fig_in_place.add_scatter(y=[2, 1, 4, 3])\n", |
| 498 | + " fig_in_place.add_bar(y=[2, 1, 4, 3])\n", |
| 499 | + " fig_in_place.layout.title = \"New Figure\"\n", |
504 | 500 | " elif mod == 2:\n", |
505 | 501 | " button.name = \"Reset\"\n", |
506 | | - " scatter = fig.data[0]\n", |
| 502 | + " scatter = fig_in_place.data[0]\n", |
507 | 503 | " scatter.y = [3, 1, 4, 3]\n", |
508 | | - " bar = fig.data[1]\n", |
| 504 | + " bar = fig_in_place.data[1]\n", |
509 | 505 | " bar.y = [5, 3, 2, 8]\n", |
510 | | - " fig.layout.title.text = \"Updated Figure\"\n", |
| 506 | + " fig_in_place.layout.title.text = \"Updated Figure\"\n", |
511 | 507 | " else:\n", |
512 | | - " fig.data = []\n", |
513 | | - " fig.layout = {\"title\": \"\"}\n", |
| 508 | + " fig_in_place.data = []\n", |
| 509 | + " fig_in_place.layout = {\"title\": \"\"}\n", |
514 | 510 | " button.name = \"Create\"\n", |
515 | 511 | "\n", |
516 | 512 | "pn.bind(handle_click, button.param.clicks, watch=True)\n", |
517 | 513 | "button.clicks=1\n", |
518 | 514 | "\n", |
519 | | - "plotly_pane = pn.pane.Plotly(\n", |
520 | | - " fig,\n", |
| 515 | + "plotly_pane_in_place = pn.pane.Plotly(\n", |
| 516 | + " fig_in_place,\n", |
521 | 517 | " height=400,\n", |
522 | 518 | " width=700,\n", |
523 | 519 | " # link_figure=False\n", |
524 | 520 | ")\n", |
525 | 521 | "\n", |
526 | 522 | "pn.Column(\n", |
527 | 523 | " button,\n", |
528 | | - " plotly_pane,\n", |
| 524 | + " plotly_pane_in_place,\n", |
529 | 525 | ")" |
530 | 526 | ] |
531 | 527 | }, |
|
567 | 563 | "df = pd.DataFrame({'x': x, 'y': y})\n", |
568 | 564 | "\n", |
569 | 565 | "# Create scatter plot\n", |
570 | | - "fig = px.scatter(df, x='x', y='y', title='Click on a Point!', hover_name='x',)\n", |
571 | | - "fig.update_traces(marker=dict(size=20))\n", |
572 | | - "fig.update_layout(autosize=True, hovermode='closest')\n", |
| 566 | + "fig_events = px.scatter(df, x='x', y='y', title='Click on a Point!', hover_name='x',)\n", |
| 567 | + "fig_events.update_traces(marker=dict(size=20))\n", |
| 568 | + "fig_events.update_layout(autosize=True, hovermode='closest')\n", |
573 | 569 | "\n", |
574 | | - "plotly_pane=pn.pane.Plotly(fig, height=400, max_width=1200, sizing_mode=\"stretch_width\")\n", |
| 570 | + "plotly_pane_event=pn.pane.Plotly(fig_events, height=400, max_width=1200, sizing_mode=\"stretch_width\")\n", |
575 | 571 | "\n", |
576 | 572 | "# Define Child View\n", |
577 | 573 | "def child_view(event):\n", |
|
587 | 583 | " \n", |
588 | 584 | " return f\"**You clicked point {index} at ({x}, {y}) on the Plotly Chart!**\"\n", |
589 | 585 | "\n", |
590 | | - "ichild_view = pn.bind(child_view, plotly_pane.param.click_data)\n", |
| 586 | + "ichild_view = pn.bind(child_view, plotly_pane_event.param.click_data)\n", |
591 | 587 | "\n", |
592 | 588 | "# Put things together\n", |
593 | | - "pn.Column(plotly_pane, ichild_view)" |
| 589 | + "pn.Column(plotly_pane_event, ichild_view)" |
594 | 590 | ] |
595 | 591 | }, |
596 | 592 | { |
|
612 | 608 | "source": [ |
613 | 609 | "event_parameters = [\"click_data\", \"click_annotation_data\", \"hover_data\", \"relayout_data\", \"restyle_data\", \"selected_data\", \"viewport\"]\n", |
614 | 610 | "\n", |
615 | | - "pn.Param(plotly_pane, parameters=event_parameters, max_width=1100, name=\"Plotly Event Parameters\")" |
| 611 | + "pn.Param(plotly_pane_event, parameters=event_parameters, max_width=1100, name=\"Plotly Event Parameters\")" |
616 | 612 | ] |
617 | 613 | }, |
618 | 614 | { |
|
0 commit comments