import panel as pn pn.extension("plotly") import numpy as np import plotly.graph_objs as go import panel as pn pn.extension("plotly") xx = np.linspace(-3.5, 3.5, 100) yy = np.linspace(-3.5, 3.5, 100) x, y = np.meshgrid(xx, yy) z = np.exp(-((x - 1) ** 2) - y**2) - (x**3 + y**4 - x / 5) * np.exp(-(x**2 + y**2)) surface=go.Surface(z=z) fig = go.Figure(data=[surface]) fig.update_layout( title="Plotly 3D Plot", width=500, height=500, margin=dict(t=50, b=50, r=50, l=50), ) plotly_pane = pn.pane.Plotly(fig) plotly_pane new_fig = go.Figure(data=[go.Surface(z=np.sin(z+1))]) new_fig.update_layout( title="Plotly 3D Plot", width=500, height=500, margin=dict(t=50, b=50, r=50, l=50), ) plotly_pane.object = new_fig plotly_pane.object = fig import panel as pn from plotly import subplots pn.extension("plotly") heatmap = go.Heatmap( z=[[1, 20, 30], [20, 1, 60], [30, 60, 1]], showscale=False) y0 = np.random.randn(50) y1 = np.random.randn(50)+1 box_1 = go.Box(y=y0) box_2 = go.Box(y=y1) data = [heatmap, box_1, box_2] fig_layout = subplots.make_subplots( rows=2, cols=2, specs=[[{}, {}], [{'colspan': 2}, None]], subplot_titles=('First Subplot','Second Subplot', 'Third Subplot') ) fig_layout.append_trace(box_1, 1, 1) fig_layout.append_trace(box_2, 1, 2) fig_layout.append_trace(heatmap, 2, 1) fig_layout['layout'].update(height=600, width=600, title='i <3 subplots') pn.pane.Plotly(fig_layout) import pandas as pd import panel as pn import plotly.express as px pn.extension("plotly") data = pd.DataFrame([ ('Monday', 7), ('Tuesday', 4), ('Wednesday', 9), ('Thursday', 4), ('Friday', 4), ('Saturday', 4), ('Sunday', 4)], columns=['Day', 'Orders'] ) fig_responsive = px.line(data, x="Day", y="Orders") fig_responsive.update_traces(mode="lines+markers", marker=dict(size=10), line=dict(width=4)) fig_responsive.layout.autosize = True responsive = pn.pane.Plotly(fig_responsive, height=300) pn.Column('## A responsive plot', responsive, sizing_mode='stretch_width') responsive_with_zoom = pn.pane.Plotly(fig_responsive, config={"scrollZoom": True}, height=300) pn.Column('## A responsive and scroll zoomable plot', responsive_with_zoom, sizing_mode='stretch_width') import numpy as np import plotly.graph_objs as go import panel as pn pn.extension("plotly") xx = np.linspace(-3.5, 3.5, 100) yy = np.linspace(-3.5, 3.5, 100) x, y = np.meshgrid(xx, yy) z = np.exp(-((x - 1) ** 2) - y**2) - (x**3 + y**4 - x / 5) * np.exp(-(x**2 + y**2)) surface = go.Surface(z=z) layout = go.Layout( title='Plotly 3D Plot', autosize=False, width=500, height=500, margin=dict(t=50, b=50, r=50, l=50) ) fig_patch = dict(data=[surface], layout=layout) plotly_pane_patch = pn.pane.Plotly(fig_patch) plotly_pane_patch surface.z = np.sin(z+1) plotly_pane_patch.object = fig_patch fig_patch['layout']['width'] = 800 plotly_pane_patch.object = fig_patch surface.z = z fig_patch['layout']['width'] = 500 plotly_pane_patch.object = fig_patch import pandas as pd import plotly.graph_objects as go import panel as pn pn.extension("plotly") df = pn.cache(pd.read_csv)( "https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv" ) start_index = 50 data = go.Ohlc( x=df.loc[:start_index, "Date"], open=df.loc[:start_index, "AAPL.Open"], high=df.loc[:start_index, "AAPL.High"], low=df.loc[:start_index, "AAPL.Low"], close=df.loc[:start_index, "AAPL.Close"], ) fig_stream = {"data": data, "layout": go.Layout(xaxis_rangeslider_visible=False)} plotly_pane_stream = pn.pane.Plotly(fig_stream) def stream(): index = len(data.x) if index == len(df): index = 0 data["x"] = df.loc[:index, "Date"] data["open"] = df.loc[:index, "AAPL.Open"] data["high"] = df.loc[:index, "AAPL.High"] data["low"] = df.loc[:index, "AAPL.Low"] data["close"] = df.loc[:index, "AAPL.Close"] plotly_pane_stream.object = fig_stream pn.state.add_periodic_callback(stream, period=100, count=50) plotly_pane_stream from asyncio import sleep import pandas as pd import plotly.graph_objects as go import panel as pn pn.extension("plotly") df = pn.cache(pd.read_csv)( "https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv" ) start_index = 50 data = go.Ohlc( x=df.loc[:start_index, "Date"], open=df.loc[:start_index, "AAPL.Open"], high=df.loc[:start_index, "AAPL.High"], low=df.loc[:start_index, "AAPL.Low"], close=df.loc[:start_index, "AAPL.Close"], ) layout = go.Layout(xaxis_rangeslider_visible=False) async def stream_generator(): for _ in range(start_index, start_index+50): index = len(data.x) if index == len(df): index = 0 data["x"] = df.loc[:index, "Date"] data["open"] = df.loc[:index, "AAPL.Open"] data["high"] = df.loc[:index, "AAPL.High"] data["low"] = df.loc[:index, "AAPL.Low"] data["close"] = df.loc[:index, "AAPL.Close"] yield {"data": data, "layout": layout} await sleep(0.05) pn.pane.Plotly(stream_generator) import plotly.graph_objects as go import panel as pn pn.extension("plotly") fig_in_place = go.Figure() button = pn.widgets.Button(name="Create", button_type="primary") def handle_click(clicks): mod = clicks % 3 if mod == 1: button.name = "Update" fig_in_place.add_scatter(y=[2, 1, 4, 3]) fig_in_place.add_bar(y=[2, 1, 4, 3]) fig_in_place.layout.title = "New Figure" elif mod == 2: button.name = "Reset" scatter = fig_in_place.data[0] scatter.y = [3, 1, 4, 3] bar = fig_in_place.data[1] bar.y = [5, 3, 2, 8] fig_in_place.layout.title.text = "Updated Figure" else: fig_in_place.data = [] fig_in_place.layout = {"title": ""} button.name = "Create" pn.bind(handle_click, button.param.clicks, watch=True) button.clicks=1 plotly_pane_in_place = pn.pane.Plotly( fig_in_place, height=400, width=700, # link_figure=False ) pn.Column( button, plotly_pane_in_place, ) import plotly.express as px import panel as pn import pandas as pd pn.extension("plotly") # Create dataframe x = [1, 2, 3, 4, 5] y = [10, 20, 30, 20, 10] df = pd.DataFrame({'x': x, 'y': y}) # Create scatter plot fig_events = px.scatter(df, x='x', y='y', title='Click on a Point!', hover_name='x',) fig_events.update_traces(marker=dict(size=20)) fig_events.update_layout(autosize=True, hovermode='closest') plotly_pane_event=pn.pane.Plotly(fig_events, height=400, max_width=1200, sizing_mode="stretch_width") # Define Child View def child_view(event): if not event: return "No point clicked" try: point = event["points"][0] index = point['pointIndex'] x = point['x'] y = point['y'] except Exception as ex: return f"You clicked the Plotly Chart! I could not determine the point: {ex}" return f"**You clicked point {index} at ({x}, {y}) on the Plotly Chart!**" ichild_view = pn.bind(child_view, plotly_pane_event.param.click_data) # Put things together pn.Column(plotly_pane_event, ichild_view) event_parameters = ["click_data", "click_annotation_data", "hover_data", "relayout_data", "restyle_data", "selected_data", "viewport"] pn.Param(plotly_pane_event, parameters=event_parameters, max_width=1100, name="Plotly Event Parameters") import pandas as pd import plotly.express as px import panel as pn pn.extension("plotly") df = ( pd.read_csv("https://datasets.holoviz.org/penguins/v1/penguins.csv") .dropna() .reset_index(drop=True) ) df["index"] = df.index # Used to filter rows for child view color_map = {"Adelie": "blue", "Chinstrap": "red", "Gentoo": "green"} fig_parent = px.scatter( df, x="flipper_length_mm", y="body_mass_g", color_discrete_map=color_map, custom_data="index", # Used to filter rows for child view color="species", title="Parent Plot: Box or Lasso Select Points", ) def fig_child(selectedData): if selectedData: indices = [point["customdata"][0] for point in selectedData["points"]] filtered_df = df.iloc[indices] title = f"Child Plot: Selected Points({len(indices)})" else: filtered_df = df title = f"Child Plot: All Points ({len(filtered_df)})" fig = px.scatter( filtered_df, x="bill_length_mm", y="bill_depth_mm", color_discrete_map=color_map, color="species", hover_data={"flipper_length_mm": True, "body_mass_g": True}, title=title, ) return fig parent_config = { "modeBarButtonsToAdd": ["select2d", "lasso2d"], "modeBarButtonsToRemove": [ "zoomIn2d", "zoomOut2d", "pan2d", "zoom2d", "autoScale2d", ], "displayModeBar": True, "displaylogo": False, } parent_pane = pn.pane.Plotly(fig_parent, config=parent_config) ifig_child = pn.bind(fig_child, parent_pane.param.selected_data) child_config = { "modeBarButtonsToRemove": [ "select2d", "lasso2d", "zoomIn2d", "zoomOut2d", "pan2d", "zoom2d", "autoScale2d", ], "displayModeBar": True, "displaylogo": False, } child_pane = pn.pane.Plotly(ifig_child, config=child_config) pn.FlexBox(parent_pane, child_pane) pn.Row(responsive.controls(jslink=True), responsive)