✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
🐇 Announcing Dash VTK for 3d simulation graphics. Check out the March webinar.

Issue formatting datetime data on plot

Hello all, I’m new to Dash and have what is probably a simple question. I’m plotting data pulled from an SQL server as a function of time. Having trouble with the axis formatting as can be seen here:

I want the time data to show up as hour:minute:seconds instead of the current format. How would I go about fixing this?

def update_graph_scatter(n):

    dataSQL = []
    conn = pymysql.connect("localhost","root","abc123","db")
    sql_select_Query = "select Time,Value4 from Weather"
    cursor = conn.cursor()
    cursor.execute(sql_select_Query)
    records = cursor.fetchall()
    X = deque(maxlen=10)
    Y = deque(maxlen=10)
    for row in records:
        dataSQL.append(list(row))
        labels = ['Time','Value-4']
        df = pd.DataFrame.from_records(dataSQL, columns=labels)
        X = df['Time']
        Y = df['Value-4']

    data = plotly.graph_objs.Scatter(
            x=list(X),
            y=list(Y),
            name='Scatter',
            mode= 'lines+markers'
            )

    return {'data': [data],'layout' : go.Layout(
                                  xaxis=dict(range=[min(X),max(X)]),
                                  yaxis=dict(range=[min(Y),max(Y)]))}

if __name__ == "__main__":

    app.run_server(debug=True)

Try converting your x data into a string like 2020-04-01, from https://plotly.com/python/time-series :

Plotly auto-sets the axis type to a date format when the corresponding data are either ISO-formatted date strings or if they’re a date pandas column or datetime NumPy array.

Thanks for the clarification. I wanted to make sure I wasn’t making a simple mistake. I converted the x data and everything looks great!

def update_graph_scatter(n):

    dataSQL = []
    conn = pymysql.connect("localhost","root","abc123","db")
    sql_select_Query = "select Time,Value4 from Weather"
    cursor = conn.cursor()
    cursor.execute(sql_select_Query)
    records = cursor.fetchall()
    X_1 = deque(maxlen=10)
    for row in records:
        dataSQL.append(list(row))
        labels = ['Time','Value-4']
        df = pd.DataFrame.from_records(dataSQL, columns=labels)
        X_1.append(str(row[0]))
        X = df['Time']
        Y = df['Value-4']
    data = plotly.graph_objs.Scatter(
            x=list(X_1),
            y=list(Y),
            name='Scatter',
            mode= 'lines+markers'
            )

    return {'data': [data],'layout' : go.Layout(
                                  xaxis=dict(range=[min(X_1),max(X_1)]),
                                  yaxis=dict(range=[min(Y)-5,max(Y)+5]),)}

if __name__ == "__main__":

    app.run_server(debug=True)```