Skip to main content
Version: Python

How to create XY series plots

This guide shows you how to use the plot method to create XY series plots.

XY series plots are generally used to show values over a continuum, such as time. XY Series plots can be represented as a line, a bar, an area or as a collection of points. The X axis is used to show the domain, while the Y axis shows the related values at specific points in the range.

Data sourcing

XY Series plots can be created using data from Deephaven tables, arrays and functions.

From a table

When data is sourced from a Deephaven table, the following syntax can be used to create an XY series plot:

.plot_xy("series_name", t, "x", "y").show()

  • 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 of data to be used for the X value.
  • "y" is the name of the column of data to be used for the Y value.
  • show tells Deephaven to draw the plot in the console.

The example query below will create an XY series plot that shows the high of Bitcoin for September 8, 2021.

from deephaven import read_csv
from deephaven.plot.figure import Figure

# source the data
source = read_csv("https://media.githubusercontent.com/media/deephaven/examples/main/MetricCentury/csv/metriccentury.csv")

# plot the data
figure = Figure()
plot_single = figure.plot_xy(series_name="Distance", t=source.where(filters=["SpeedKPH > 0"]), x="Time", y="DistanceMeters").show()

.plot("series_name", function).show()

  • plot 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.
  • function is a mathematical operation that maps one value to another. Examples of Groovy functions and their formatting follow:
    • {x->x+100} adds 100 to the value of x.
    • {x->x*x} squares the value of x.
    • {x->1/x} uses the inverse of x.
    • {x->x*9/5+32} Fahrenheit to Celsius conversion.
  • show tells Deephaven to draw the plot in the console.

If you are plotting a function in a plot by itself, consider applying a range for the function using the funcRange or xRange method. Otherwise, the default value ([0,1]) will be used, which may not meet your requirements:

.plot("Function", {x->x*x} ).funcRange(0,10).show()

If the function is being plotted with other data series, the funcRange method is not needed, and the range will be obtained from the other data series.

When using a function plot, you may also want to increase or decrease the granularity of the plot by declaring the number of points to include in the range. This is configurable using the funcNPoints method:

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 are plotted, thereby creating two line graphs on the same plot.

# plot the data
figure = Figure()
plot_shared_axis = figure.plot_xy(series_name="Altitude", t=source, x="Time", y="AltitudeMeters")\
.plot_xy(series_name="Speed", t=source, x="Time", y="SpeedKPH")\
.show()
tip

You can choose to hide one or more series in the plot. Simply click the name of the series at the right of the plot to hide that series; click the name again to restore it.

Subsequent series can be added to the plot by adding additional plot_xy methods to the query.

Multiple X or Y Axes

When plotting multiple series in a single plot, the range of the Y axis is an important factor to watch. As the range of the Y axis increases, value changes become harder to assess.

When the scale of the Y axis needs to cover an extremely wide range, the plot may result in relatively flat lines with barely distinguishable differences in values or trend.

This issue can be easily remedied by adding a second Y axis to the plot via the x_twin method.

x_twin

The x_twin method 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:

PlotName = figure().plot_xy(...).x_twin().plot_xy(...).show()

  • The plot(s) for the series placed before the twinX() method share a common Y axis (on the left).
  • The plot(s) for the series listed after the twinX() method share a common Y axis (on the right).
  • All plots share the same X axis.
figure = Figure()
plot_shared_twin_x = figure.plot_xy(series_name="Altitude", t=source, x="Time", y="AltitudeMeters")\
.x_twin()\
.plot_xy(series_name="Speed", t=source, x="Time", y="SpeedKPH")\
.show()

The value range for the high value is shown on the left axis and the value range for the low value is shown on the right axis.

twinY

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:

plot_name = figure().plot_xy(...).y_twin().plot_xy(...).show()

  • The plot(s) for the series placed before the y_twin() method use the lower X axis.
  • The plot(s) for the series listed after the y_twin() method use the upper X axis.

Plot styles

The XY series plot in Deephaven defaults to a line plot. However, Deephaven's plot_style method can be used to format XY series plots as area charts, stacked area charts, bar charts, stacked bar charts, scatter charts and step charts.

In any of the examples below, you can simply swap out the plot_style argument with the appropriate name; e.g., ("area"), ("stacked_area"), ("step"), etc.

XY Series as a stacked area plot

In any of the examples below, you can simply swap out the plot_style argument with the name ("BAR"), ("STACKED_BAR"), ("LINE"), ("AREA"), ("STACKED_AREA"), ("ERROR_BAR"), etc.

from deephaven.plot import PlotStyle

figure = Figure()
plot_single_stacked_area = figure.axes(plot_style=PlotStyle.STACKED_BAR)\
.plot_xy(series_name="Heart_rate", t=source, x="Time", y="HeartRate").show()

XY Series as a scatter plot

In the example below, the .plot_style argument has the name ("SCATTER"). Other parameters are defined to show the fine tuning detail under control.

from deephaven.plot.figure import Figure
from deephaven.plot import PlotStyle
from deephaven.plot import Color, Colors
from deephaven.plot import font_family_names, Font, FontStyle, Shape

figure = Figure()
plotXYScatter = figure\
.plot_xy(series_name="Speed", t=source, x="Time", y="SpeedKPH")\
.axes(plot_style=PlotStyle.SCATTER)\
.point(shape=Shape.SQUARE, size=10, label="Big Point", color=Colors.RED)\
.x_twin()\
.axes(plot_style=PlotStyle.SCATTER)\
.plot_xy(series_name="Distance", t=source, x="Time", y="DistanceMeters")\
.point(shape=Shape.DIAMOND, size=16, label="Big Triangle",color=Colors.BLUE )\
.show()

XY Series as a step plot

In the example below, the .plot_style argument has the name ("STEP"). Other parameters are defined to show the fine tuning detail under control.

from deephaven.plot import LineEndStyle, LineJoinStyle, LineStyle

figure = Figure()
plot_step = figure\
.axes(plot_style=PlotStyle.STEP)\
.plot_xy(series_name="HeartRate", t=source, x="Time", y="HeartRate")\
.line(style=LineStyle(width=1.0, end_style=LineEndStyle.ROUND))\
.show()