How to graph data from a CSV file using Dash, Plotly

Question:

I am new to python and am looking for ways in which I can visualise data. I have come across ‘Dash’ – but wondered how I would show a graph based on data that is saved in a CSV?

Currently I have this.. But it only shows a blank graph.

Any help would be welcomed – thanks in advance.

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.express as px
import pandas as pd

df = pd.read_csv('d6.csv')

app = dash.Dash(__name__)

app.layout = html.Div([
    dcc.Graph(id="graph"),
    html.Button("Switch Axis", id='btn', n_clicks=0)
])

@app.callback(
    Output("graph", "figure"), 
    [Input("btn", "n_clicks")])

def display_graph(n_clicks):
    if n_clicks % 2 == 0:
        x, y = 'Time', 'R1Temp'
    else:
        x, y = 'R1Temp', 'Time'

    fig = px.line(df, x=x, y=y)    
    return fig

app.run_server(debug=False)
Asked By: HWJ5

||

Answers:

Pandas is your friend, it gives you access to a data structure called a dataframe. I highly recommend you look through some tutorials on pandas as they come up everywhere in the data science field. In addition plotly often looks for dataframes as inputs into their API.
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html

But to cut to the chase, a dataframe can load a CSV with 100% structure retention
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html#pandas.read_csv

https://datatofish.com/export-dataframe-to-csv/

Once you load the csv to a dataframe you can access rows and columns with queries.
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.columns.html

Answered By: Devin Burke

where are you storing the csv file? locally? can plotly dash enterprise call a local file? @Devin Burke

Answered By: Jess Torres
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.