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

Can someone add datashader to this example

Hi i am a beginner and want to use datashader. if someone could show me how to implement it in this example it would really help me out. Im my plotly application i query an sql database. would this need to be converted to a pandas dataframe?

in my code this is how i query the database, im really trying to use datashader and plotly with my sql database but im struggling to find anything to help me

def _process(data, type):

    return data.timestamp, data.reading0

def _get_log_temp_data(ID):
    with db2.ro_engine.begin() as conn:
        result = conn.execute(
            db2.sensor_readings.select().where(
                (db2.sensor_readings.c.device == ID)
            )
        ).fetchall()

        return Plot(*zip(*(_process(data, "Temp") for data in result)))

the part of my program that configure the plot and axis,

def auxinput(n):
    aux = _get_log_temp_data("I1")
    return {
        "data": [
            plotly.graph_objs.Scattergl(
                x=list(aux.xs),
                y=list(aux.ys),
                line={"color": "#008f13"},
                name="A/C Run-time",
                mode="lines+markers",
            ),
        ],
        "layout": go.Layout(
            uirevision="null",
            height=600,
            plot_bgcolor="rgb(219, 219, 219)",
            xaxis={"type": "date", "range": [min(aux.xs), max(aux.xs)]},
            yaxis_tickwidth=2,
            xaxis_tickangle=45,
            yaxis_linewidth=1,
            xaxis_linewidth=1,
            xaxis_tickformatstops=xaxis_ticklist,
            yaxis={
                "title": "A/C Run-time",
                "fixedrange": True,
                "autorange": True,
                "range": [min(aux.ys), max(aux.ys)],
            },
        ),
    }
import plotly.graph_objects as go
import datashader as ds  # 0.6.9
# Create random data with numpy
import numpy as np
np.random.seed(1)

N = 10000
random_x = np.linspace(0, 1, N)
random_y0 = np.random.randn(N) + 5
random_y1 = np.random.randn(N)
random_y2 = np.random.randn(N) - 5



# Create traces
fig = go.Figure()
fig.add_trace(go.Scatter(x=random_x, y=random_y0,
                    mode='lines',
                    name='lines'))
fig.add_trace(go.Scatter(x=random_x, y=random_y1,
                    mode='lines+markers',
                    name='lines+markers'))
fig.add_trace(go.Scatter(x=random_x, y=random_y2,
                    mode='markers', name='markers'))

fig.show()

`

Hi @notsolowki did you already take a look at the plotly documentation on plotly and datashader? You need indeed to have data in the form of a pandas or Dask dataframe (or an xarray) to be passed to a datashader canvas object. Therefore you need indeed to create a pandas DataFrame when querying your database (see for example https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_sql.html). Then calling an aggregation function on this canvas returns a 2D xarray which you can plot with px.imshow.