How to update figure in same window dynamically without opening and redrawing in new tab?

2024/11/13 23:48:04

I am creating a 3D scatter plot based off a pandas dataframe, and then I want to re-draw it with slightly updated data whenever the user presses a button in my program. I almost have this functionality working, except the updated figure is drawn via a new opened tab, when really I just want my origin existing figure to be updated.

Here is my code. First I initialize the plot with 'version 1' of the data, then I set up a simple while loop to wait for the user to request an update. Then ideally once they enter input to ask for the update, I just re-draw everything in the same tab that is open. But instead a new tab is opened (which redraws the data correctly at least).

    fig = go.Figure(data=[go.Scatter3d(x=df['x'],y=df['y'],z=df['z'],mode='markers', marker=dict(size=4,color=df['y'],                # set color to an array/list of desired valuescolorscale='Viridis',   # choose a colorscaleopacity=0.3))])# Column max and mins for plotting:xmax = df_1.max(axis=0)['x']; xmin = df_1.min(axis=0)['x']ymax = df_1.max(axis=0)['y']; ymin = df_1.min(axis=0)['y']zmax = df_1.max(axis=0)['z']; zmin = df_1.min(axis=0)['z']fig.update_layout(scene = dict(xaxis = dict(nticks=4, range=[xmin,xmax],),yaxis = dict(nticks=4, range=[ymin,ymax],),zaxis = dict(nticks=4, range=[zmin,zmax],),))f2 = go.FigureWidget(fig)f2.show()#fig.show()while True:choice = input("> ")choice = choice.lower() #Convert input to "lowercase"if choice == 'exit':print("Good bye.")breakif choice == 'w':print("W, moving forward")cube_origin = cube_origin + np.array([0.1,0,0])df_cube = createCubeMesh(cube_size, cube_density, cube_origin)new_df = df_scene_orig.copy()new_df = new_df.append(df_cube)fig = go.Figure(data=[go.Scatter3d(x=new_df['x'],y=new_df['y'],z=new_df['z'],mode='markers', marker=dict(size=4,color=new_df['y'],                # set color to an array/list of desired valuescolorscale='Viridis',   # choose a colorscaleopacity=0.3))])f2 = go.FigureWidget(fig)f2.show()

I based my code on another answer that said to use go.FigureWidget(fig), but it doesn't seem to work as intended.

Edit

Instead of me using f2.show() at the end, I just want a simple thing analogous to f2.update() that redraws.

Answer

This is the case you want.

Everywhere in this page that you see fig.show(), you can display the same figure in a Dash application by passing it to the figure argument of the Graph component from the built-in dash_core_components package like this:

import plotly.graph_objects as gofig = go.Figure(data=[go.Scatter(mode="markers+text",x=[10, 20],y=[20, 25],text=["Point A", "Point B"])],layout=dict(height=400, width=400, template="none")
)import dash
import dash_core_components as dcc
import dash_html_components as htmlapp = dash.Dash()
app.layout = html.Div([dcc.Graph(figure=fig)
])app.run_server(debug=True, use_reloader=False)

reference: https://plotly.com/python/figure-introspection/

Help you write a code that is closest to your needs:

import plotly as py
from dash import dcc
from dash import html
from dash.dependencies import Input, Output
import plotly.graph_objects as go
from jupyter_dash import JupyterDash
import pandas as pd
import numpy as nppy.offline.init_notebook_mode(connected=True)app = JupyterDash('SimpleExample')
app.layout = html.Div([dcc.Dropdown(id='dropdown', options=[{'label': 'W', 'value': 'W'},{'label': 'exit', 'value': 'exit'}],value='exit'),dcc.Graph(id='graph-court')])def random_data():# sample dataframe of a wide formatnp.random.seed(4)cols = list('xyz')X = np.random.randint(50, size=(3, len(cols)))df = pd.DataFrame(X, columns=cols)df.iloc[0] = 0return dfdf = random_data()def create_figure(df):fig = go.Figure(data=[go.Scatter3d(x=df['x'], y=df['y'], z=df['z'], mode='markers', marker=dict(size=10,color=df['y'],colorscale='Viridis',opacity=0.3))])# Column max and mins for plotting:xmax = df.max(axis=0)['x']xmin = df.min(axis=0)['x']ymax = df.max(axis=0)['y']ymin = df.min(axis=0)['y']zmax = df.max(axis=0)['z']zmin = df.min(axis=0)['z']fig.update_layout(scene=dict(xaxis=dict(nticks=4, range=[xmin, xmax], ),yaxis=dict(nticks=4, range=[ymin, ymax], ),zaxis=dict(nticks=4, range=[zmin, zmax], ), ))fig = go.FigureWidget(fig)return fig@app.callback(Output('graph-court', 'figure'),[Input('dropdown', 'value')])
def update_figure(selected_value):selected_value = selected_value.lower()  # Convert input to "lowercase"if selected_value == 'exit':print("Good bye.")new_x, new_y, new_z = [], [], []else:print("W, moving forward")# new datanew_x, new_y, new_z = np.random.randint(10, size=(3, 1))# ployfig = create_figure(df)  # Set as global variable or local variable as requiredfig.add_trace(go.Scatter3d(x=new_x, y=new_y, z=new_z, marker=dict(size=10, color='green'), mode='markers'))return figapp.run_server(debug=False, use_reloader=False)
https://en.xdnf.cn/q/72181.html

Related Q&A

Serializing a C struct in Python and sending over a socket

Im trying to serializing the following C structstruct packet {int id;unsigned char *ce;unsigned char *syms; };in Python and send it over a socket. The number of elements pointed by ce and syms are know…

creating multiple audio streams of an icecast2 server using python-shout

I am trying to create a web radio server to stream 3 sources at once. I am using python to create a source client for icecast2 using the python-shout library. I am not too familiar with the language (p…

Custom Deployment to Azure Websites

I recently started using Gulp.js to package all my CSS and JavaScript into single files, which I then include in my web app. My web app is written in Python (using Flask).I obviously dont want to track…

Python - Gspread Request Error 401

Im currently making a Discord-bot that connects to a Google-spreadsheet (gspread). But after Ive been running it for a while it starts to hand out errors and it cant connect to my gspread anymore (unle…

paramiko server mode port forwarding

I need to implement a ssh server using paramiko that only handles -R port forwarding requests like this:ssh -N -T -R 40005:destination_host:22 [email protected]So far from what i understand ill have to…

Pandas dataframe.hist() change title size on subplot?

I am manipulating DataFrame using pandas, Python. My data is 10000(rows) X 20(columns) and I am visualizing it, like this.df.hist(figsize=(150,150))However, if I make figsize bigger, each of subplots t…

Regex match back to a period or start of string

Id like to match a word, then get everything before it up to the first occurance of a period or the start of the string. For example, given this string and searching for the word "regex":s = …

Finding differences between strings

I have the following function that gets a source and a modified strings, and bolds the changed words in it.def appendBoldChanges(s1, s2):"Adds <b></b> tags to words that are changed&qu…

Python pandas: select 2nd smallest value in groupby

I have an example DataFrame like the following:import pandas as pd import numpy as np df = pd.DataFrame({ID:[1,2,2,2,3,3,], date:array([2000-01-01,2002-01-01,2010-01-01,2003-01-01,2004-01-01,2008-01-01…

How to disable SSL3 and weak ciphers with cherrypy builtin ssl module (python 3)

I have configured Cherrypy 3.8.0 with Python 3 to use SSL/TLS. However, I want to disable SSL3 to avoid POODLE. I searched through the documentation but I am unsure on how to implement it.I am using th…