Built-in plotting API

Deephaven's built-in plotting API offers methods for creating real-time figures, plots, and charts from static and ticking tables. This guide provides an overview of its capabilities.

Note

The built-in plotting API is no longer under active development, and there are no plans for new patches, features, or fixes. Deephaven recommends Deephaven Express for most plotting tasks.

This guide covers the following plot types:

It also shows how to combine plots in subplots and make plots that users filter from the UI.

Every plot starts from a Figure object. You create one with the Figure constructor, chain plotting methods onto it, and finish with show.

XY series

XY series plots generally show values over a continuum, such as time. They can take the form of a line, a bar, an area, or a collection of points. The X axis shows the domain, while the Y axis shows the related values at specific points in the range.

You can create an XY series plot using data from Deephaven tables with the following syntax:

  • plot_xy is the method used to create an XY series plot.
  • series_name is the name (as a string) you want to use to identify the series on the plot itself.
  • t is the table that holds the data you want to plot.
  • x is the name of the column that holds the X values.
  • y is the name of the column that holds the Y values.
  • show returns a displayable version of the figure. Assign the result to a variable to see the plot in the console.

The example below creates an XY series plot of distance traveled over time during a metric-century bike ride. The where filter drops the rows where the rider was stopped.

Shared axes

You can compare multiple series over the same period of time by creating an XY series plot with shared axes. In the following example, two series create two line graphs on the same plot.

To add more series to the plot, chain more plot_xy calls onto the figure.

Tip

To hide a series, click its name at the right of the plot. Click the name again to restore it.

Plot multiple series in a loop

It's common in data analysis to have multiple series that share an X axis. In that case, a for loop can add each series to the figure. The following example plots three different series on the same plot in a for loop.

Plot by a grouping column

To plot a separate series for each key, pass the grouping columns to the by parameter of plot_xy. Deephaven plots one XY series for each unique value, or combination of values, in the by columns.

Multiple X or Y axes

When series in one plot have very different value ranges, a shared Y axis has to cover all of them, so the smaller series flatten and their changes become hard to see.

To fix this, add a second Y axis with the x_twin method. x_twin enables you to use one Y axis for some of the series being plotted and a second Y axis for the others, while sharing the same X axis:

  • Series plotted before x_twin share the left Y axis.
  • Series plotted after x_twin share the right Y axis.
  • All series share the same X axis.

In the following example, distance reaches about 100,000 meters while speed stays below 70 KPH, so each series gets its own Y axis:

The left Y axis shows the value range for distance, and the right Y axis shows the value range for speed.

The y_twin method enables you to use one X axis for one set of the values being plotted and a second X axis for another, while sharing the same Y axis:

  • Series plotted before y_twin use the lower X axis.
  • Series plotted after y_twin use the upper X axis.

Plot colors

It's easy to change the line colors in an XY series plot with the line method. This example takes the Distance and Speed plot from Multiple X or Y axes and draws its lines in red and yellow:

For the full list of available colors, see Named colors.

Plot styles

The XY series plot in Deephaven defaults to a line plot. Use the plot_style parameter of the axes method to format XY series plots as area charts, stacked area charts, bar charts, stacked bar charts, scatter charts, or step charts.

In any of the examples below, you can swap in another PlotStyle value, such as PlotStyle.BAR, PlotStyle.STACKED_BAR, PlotStyle.LINE, PlotStyle.AREA, PlotStyle.STACKED_AREA, PlotStyle.SCATTER, or PlotStyle.STEP. The plot_style parameter also accepts the style name as a string, such as "AREA".

XY series as a stacked area plot

The following example stacks cadence on top of heart rate:

XY series as a scatter plot

In the example below, both series use the PlotStyle.SCATTER plot style. x_twin keeps the shared X axis and gives each series its own Y axis.

XY series with markers on selected points

In the example below, a line plot of speed gets a marker at every point where the rider went faster than 40 KPH.

  • The twin method adds a second set of axes that shares both the X and Y axes.
  • axes then gives those axes the PlotStyle.SCATTER style, so the series plotted on them appears as markers over the line.

XY series as a step plot

In the example below, axes(plot_style=PlotStyle.STEP) draws a step plot. Then line(style=LineStyle(...)) sets the line width and end style through a LineStyle object.

Category

Use the plot_cat method to create category plots, which display data values from different discrete categories.

To plot data from a Deephaven table, use the following syntax:

  • plot_cat is the method used to create a category plot.
  • series_name is the name (as a string) you want to use to identify the series on the plot itself.
  • t is the table that holds the data you want to plot.
  • category is the name of the column that holds the category values.
  • y is the name of the column that holds the Y values.
  • y_low and y_high (optional) are the names of columns that hold lower and upper Y error-bar values. Passing both creates a category error-bar plot.
  • by (optional) is a list of one or more columns that hold grouping data. Deephaven plots a separate series for each unique value, or combination of values, in the by columns.
  • show returns a displayable version of the figure. Assign the result to a variable to see the plot in the console.

Shared axes

You can compare multiple series across the same categories by creating a category plot with shared axes. The following example plots two tables with the same Categories column as bar graphs on one chart:

To add more series to the chart, chain more plot_cat calls onto the figure.

Plot styles

By default, category plots draw values as vertical bars. To use another style, pass a PlotStyle value, such as PlotStyle.STACKED_BAR, PlotStyle.LINE, or PlotStyle.AREA, to the plot_style parameter of the axes method.

Category plot with stacked bars

The following example draws the source_one and source_two series from the category shared axes example as stacked bars:

Category histogram

Use the plot_cat_hist method to create category histograms, which show how often each discrete value (category) occurs.

To plot data from a Deephaven table, use the following syntax:

  • plot_cat_hist is the method used to create a category histogram.
  • series_name is the name (as a string) you want to use to identify the series on the chart itself.
  • t is the table that holds the data you want to plot.
  • category is the name of the column (as a string) containing the discrete values.
  • show returns a displayable version of the figure. Assign the result to a variable to see the plot in the console.

To plot data from a Python list instead of a table, use the following syntax:

  • category is the list of discrete values to count.

The following example counts values from a NumPy array, converted to a list with list(values):

chart_title sets the title of the chart the series is drawn on.

Histogram

Use the plot_xy_hist method to create histograms.

A histogram shows how frequently different data values occur. It divides the data into intervals (bins), counts the values in each bin, and draws each count as a vertical bar. Unlike bar charts (category plots), bars in histograms do not have spaces between them unless there is a gap in the data.

To plot data from a Deephaven table, use the following syntax:

  • plot_xy_hist is the method used to create a histogram.
  • series_name is the name (as a string) you want to use to identify the series on the chart itself.
  • t is the table that holds the data you want to plot.
  • x is the name of the column (as a string) that holds the X values.
  • nbins is the integer number of intervals to use in the chart.
  • show returns a displayable version of the figure. Assign the result to a variable to see the plot in the console.

The plot_xy_hist method assumes you want to plot the entire range of values in the dataset. You can also set the minimum and maximum values of the range with xmin and xmax, respectively:

  • xmin is a floating-point minimum value in the x column you want to include in the plot.
  • xmax is a floating-point maximum value in the x column you want to include in the plot.

To plot data from a Python list instead of a table, use the following syntax:

  • x is the list of values to bin.

The following example plots values from a NumPy array, converted to a list with list(values):

OHLC

Use the plot_ohlc method to create Open, High, Low, and Close (OHLC) plots. These typically show four prices of a security or commodity per time slice: the open and close of the time slice, and the highest and lowest values reached during the time slice.

This plotting method requires a dataset that includes one column containing the values for the X axis (time), and one column for each of the corresponding four values (open, high, low, close).

To plot data from a Deephaven table, use the following syntax:

  • plot_ohlc is the method used to create an OHLC chart.
  • series_name is the name (as a string) you want to use to identify the series on the chart itself.
  • t is the table that holds the data you want to plot.
  • x is the name of the column to use for the X axis.
  • open is the name of the column holding the opening price.
  • high is the name of the column holding the highest price.
  • low is the name of the column holding the lowest price.
  • close is the name of the column holding the closing price.
  • by (optional) is a list of one or more columns that hold grouping data. Deephaven plots a separate OHLC series for each unique value, or combination of values, in the by columns.
  • show returns a displayable version of the figure. Assign the result to a variable to see the plot in the console.

The following example bins BTC trades into one-minute intervals and uses an aggregation to compute each interval's open, high, low, and close prices. The trades in the file are not in time order, so the example sorts them by Timestamp first. That way, agg.first and agg.last return each interval's earliest and latest trade.

figure_title sets the title of the whole figure, which can hold several charts (see Subplots).

Plot by a grouping column

To plot a separate OHLC series for each key, pass the grouping columns to the by parameter of plot_ohlc. The following example bins BTC and ETH trades together, computes OHLC values for each instrument and interval, and plots one series per instrument:

All the series share one Y axis. ETH trades at about one-fourteenth of BTC's price, so its series looks flat next to BTC's. To give each series its own Y axis, plot them separately, as the next section shows.

Multiple series with separate Y axes

Like XY series plots, OHLC plots can show multiple series on one chart. The following example repeats the BTC binning from the OHLC example, adds a second series for ETH, and uses x_twin to give ETH its own Y axis:

The second plot_ohlc call adds an ETH series built the same way as the BTC series. The x_twin call between them gives ETH its own Y axis, so the left Y axis shows the BTC price scale and the right Y axis shows the ETH price scale.

Pie

Use the plot_pie method to create pie charts, which show each category's share of the total as a slice of a circle.

To create a pie chart from a table, use the following syntax:

  • plot_pie is the method used to create a pie chart.
  • series_name is the name (as a string) you want to use to identify the series on the plot itself.
  • t is the table that holds the data you want to plot.
  • category is the name of the column that holds the categories to place in the pie chart.
  • y is the name of the column that holds the value for each category.
  • show returns a displayable version of the figure. Assign the result to a variable to see the plot in the console.

Treemaps

Use the plot_treemap method to create treemap charts, which show hierarchical data as nested rectangles, where each item's area is proportional to its value.

From a formatted table

Your table may already have the structure plot_treemap requires. It needs at least two columns:

  • An ID column, which identifies each item.
  • A parent column, which names the item each one sits under.

Following the parents up from any item must end at one top-level value, called the root. The root doesn't need a row of its own. In the examples below, root appears only in the parent column. When your table has this structure, use the following syntax:

  • series_name is the name (as a string) you want to use to identify the series on the plot itself.
  • t is the table that holds the data you want to plot.
  • id is the name of the column that holds the item IDs to use in structuring the treemap chart.
  • parent is the name of the column that holds each item's parent ID.
  • show returns a displayable version of the figure. Assign the result to a variable to see the plot in the console.

You can interact with this plot. Click a group header to expand that group, and click it again to return to the full view. Click an individual cell to expand its contents.

By default, each item takes its color from the theme, and its label is its ID.

The same table also has value, hover text, and color columns. The following example uses them to display more information:

  • label is the name of the column that holds the display text to use instead of the raw ID.
  • value is the name of the column that sets how large each item is compared to the others.
  • hover_text is the name of the column that holds the text to display when hovering over an item.
  • color is the name of the column that holds the color to use for each item.

The table's Text column holds each stock's percent change rather than a name, so the example passes the ID column, Label, as label too. With a column of display names, pass that column instead.

From a raw data table

Raw data often lacks the ID and parent columns that plot_treemap needs. The following example builds them with view, select_distinct, and merge:

  • financials is a view of the raw source table with only the Symbol, Name, Sector, and MarketCap columns.
  • sectors is a table of the top-level distinct sectors in the raw data.
  • sector_items is a table of the top-level sectors with label and color information associated with them.
  • heatmap_daily_perc is a function that returns a color based on the percent change provided.
  • stock_items is a table with all the stock items and their associated labels.
  • items merges sector_items and stock_items into the single table that plot_treemap plots.
  • s_and_p_treemap is a treemap plot created with the plot_treemap method.

Subplots

A Figure can contain subplots: separate charts arranged in a grid by row and column number.

  • A single plot with several series draws them all in one plot area, sharing at least one axis.
  • Subplots give each chart its own section of the figure.

Use subplots when the datasets have different units or scales and you don't need to read them against each other. To overlay two series with different scales on one plot instead, see Multiple X or Y axes.

Define subplots

Set the number of subplots with the rows and cols parameters when you create a Figure.

n and m are the number of rows and columns, respectively.

For a figure with only one plot, specifying rows and cols is optional. If a figure has more than one subplot, these arguments are required.

Add subplots to a figure

After defining the figure's grid, add each subplot with a new_chart call. Each new_chart call creates a chart at a given row and column in the figure, and the plotting methods that follow it draw on that chart. Row and column indices start at 0.

For example, to stack two subplots vertically, give the figure two rows and one column.

One row, two columns

Let's instead place the subplots side by side by giving the figure one row and two columns. The series Y appears on the left, and Z on the right.

Different plot types

A single figure can handle multiple plot types as well as both static and real-time data.

The above dynamic plot updating as the source table ticks

Dynamic plots

A dynamic plot redraws to show only the rows that match the value a user picks in a paired filter control: Deephaven's Input Filter, Linker, or Dropdown Filter. To make a plot dynamic, use a SelectableDataSet as its data source.

one_click

The one_click method creates a SelectableDataSet from a table:

  • t is the table containing the data.
  • by is a list of one or more column names to make available for input filtering. It defaults to None, but one_click raises an error unless you pass at least one column.
  • require_all_filters controls whether the plot waits for filters. When True, the plot displays data only after the user sets a filter on every by column. When False (the default), data displays before the user sets every filter.

The following example sets require_all_filters=True, so the plot shows no data until you pick an Instrument value. Until then, the plot displays a message with Add Input Filters and Open Linker Tool buttons:

The plot waits for a filter on the Instrument column and shows the Add Input Filters and Open Linker Tool buttons

To create a SelectableDataSet from a partitioned table instead, use one_click_partitioned_table.

Input filters

To pair your dynamic plot with an Input Filter, choose Input Filter from the Controls menu in the upper right-hand corner of the UI. If the plot is waiting for a filter, you can also click Add Input Filters on the plot itself.

A user selects the Input Filter option from the Controls menu

Note

The Input Filter feature filters every open table in the dashboard that has a column with the same name and type as the filter column. It also filters every dynamic plot whose SelectableDataSet filters on such a column. That column is either a column in the by parameter of one_click, or a key column of the partitioned table passed to one_click_partitioned_table.

Linker tool

As an alternative to an Input Filter, you can use the Linker tool to pair a dynamic plot with a table. Choose Linker from the Controls menu. If the plot is waiting for a filter, you can also click Open Linker Tool on the plot.

The Linker option highlighted in the Controls menu

A user pairs a dynamic plot with a table using the Linker tool

In the Linker, the table you filter from is the source table, and the dynamic plot is the target. Click the matching column in the source table, then click the column button on the dynamic plot. After that, double-clicking a row in the source table filters the plot to that row's value in the linked column.

You can also pair a dynamic plot with a Dropdown Filter, which filters on a value you choose from a list:

  1. Choose Dropdown Filter from the Controls menu.
  2. Under Source Column, select a column to supply the list of values.
  3. Under Filter Column, choose the column the plot's SelectableDataSet filters on.
  4. Click Save, then pick a value from the list.