Plotly#

New in version 1.4.0

The Subgrounds Plotly Wrapper is an extension of the Plotly components to understand and work seamlessly with the Subgrounds library. It provides a convenient way to visualize data fetched from subgraphs using Plotly, by wrapping Plotly's trace components with additional functionality.

Resources

Getting Started#

To start using the Subgrounds Plotly Wrapper, you'll need to import the required components:

from subgrounds import Subgrounds
from subgrounds.contrib.plotly import Figure, Indicator

Next, load your subgraph using the Subgrounds library:

sg = Subgrounds()
aave_v2 = sg.load_subgraph("https://api.thegraph.com/subgraphs/name/messari/aave-v2-ethereum")

Now, you can create a Figure instance with the appropriate traces:

market = aave_v2.Query.market(id="0x030ba81f1c18d280636f32af80b9aad02cf0854e")

Figure(
    subgrounds=sg,
    traces=[
        Indicator(value=market.inputToken.lastPriceUSD),
    ],
)

Examples#

Here are some code examples demonstrating how to use the Subgrounds Plotly Wrapper:

Simple Indcator#

An indicator for the price of a token#
from subgrounds import Subgrounds
from subgrounds.contrib.plotly import Figure, Indicator

sg = Subgrounds()
aave_v2 = sg.load_subgraph(
  "https://api.thegraph.com/subgraphs/name/messari/aave-v2-ethereum")

market = aave_v2.Query.market(id="0x030ba81f1c18d280636f32af80b9aad02cf0854e")

Figure(
    subgrounds=sg,
    traces=[
        Indicator(value=market.inputToken.lastPriceUSD),
    ],
)

Scatter and Bar Plots#

Scatter and bar plots across a months worth of data#
from datetime import datetime

import pandas as pd

from subgrounds import FieldPath, Subgrounds, SyntheticField
from subgrounds.contrib.plotly import Figure, Scatter, Bar

sg = Subgrounds()

lido_activity = sg.load_subgraph(
    "https://api.thegraph.com/subgraphs/name/messari/lido-ethereum"
)

usage_daily_snapshot_30days = lido_activity.Query.usageMetricsDailySnapshots(
    orderBy=lido_activity.UsageMetricsDailySnapshot.timestamp,
    orderDirection="desc",
    first=30,
)

daily_snapshot = lido_activity.UsageMetricsDailySnapshot

daily_snapshot.datetime = SyntheticField.datetime_of_timestamp(
    daily_snapshot.timestamp
)

# Create the Scatter trace with appropriate field paths
trace = Scatter(
    x=usage_daily_snapshot_30days.datetime,
    y=usage_daily_snapshot_30days.dailyActiveUsers,
)

# Create the Figure instance with the trace and display it
fig = Figure(
    subgrounds=sg,
    traces=trace,
    layout=dict(
        title="Daily Active Users vs Datetime",
        xaxis=dict(title="Datetime"),
        yaxis=dict(title="Daily Active Users")
    ),
)

# Create the Bar trace with appropriate field paths:
trace2 = Bar(
    x=usage_daily_snapshot_30days.datetime,
    y=usage_daily_snapshot_30days.dailyTransactionCount,
)

# Create the Figure instance with the trace and display it:
fig2 = Figure(
    subgrounds=sg,
    traces=trace2,
    layout=dict(
        title="Daily Transaction Count",
        xaxis=dict(title="Datetime"),
        yaxis=dict(title="Daily Transaction Count")
    ),
)
Show figure 1#
fig.figure.show()
Show figure 2#
fig2.figure.show()

In this example, we first load a subgraph and fetch data for the past 30 days. We then create a synthetic field to convert the timestamp into a datetime object. Next, we create a Scatter trace and a Bar trace with the appropriate field paths. Finally, we create two Figure instances and display them.

With the Subgrounds Plotly Wrapper, you can easily extend the provided classes to support more Plotly trace types and create custom visualizations for your subgraph data.