Skip to content

Histogram2d

Overview

The histogram2d trace type is used to create 2D histograms, where data is binned along both the x and y axes, producing a heatmap-like visualization. This allows you to visualize the relationship between two numerical variables by counting how many data points fall into each bin.

You can customize the binning along both axes, as well as the colorscale and aggregation method, to better represent your data.

Common Uses

  • Joint Distributions: Visualizing the relationship between two variables.
  • Density Plots: Showing the density of data points across two dimensions.
  • Statistical Analysis: Identifying patterns, correlations, or anomalies in bivariate data.

Check out the Attributes for the full set of configuration options

Examples

Common Configurations

Here's a simple histogram2d plot showing how data is distributed across two dimensions:

You can copy this code below to create this chart in your project:

models:
  - name: histogram2d-data
    args:
      - echo
      - |
        x,y
        1,2
        2,3
        3,4
        2,2
        1,3
        3,2
        4,4
        5,6
        4,2
traces:
  - name: Simple 2D Histogram Plot
    model: ref(histogram2d-data)
    props:
      type: histogram2d
      x: ?{x}
      y: ?{y}
      colorscale: "Viridis"
charts:
  - name: Simple 2D Histogram Chart
    traces:
      - ref(Simple 2D Histogram Plot)
    layout:
      title:
        text: Simple 2D Histogram<br><sub>Data Distribution Across Two Dimensions</sub>
      xaxis:
        title:
          text: "X Axis"
      yaxis:
        title:
          text: "Y Axis"

This example demonstrates a histogram2d plot with custom binning along the x and y axes:

Here's the code:

models:
  - name: histogram2d-data-bins
    args:
      - echo
      - |
        x,y
        1,5
        2,6
        3,7
        2,4
        1,5
        3,3
        4,8
        5,9
        4,4
traces:
  - name: 2D Histogram with Custom Bins
    model: ref(histogram2d-data-bins)
    props:
      type: histogram2d
      x: ?{x}
      y: ?{y}
      xbins:
        size: 1
      ybins:
        size: 1
      colorscale: "Blues"
charts:
  - name: 2D Histogram with Custom Bins
    traces:
      - ref(2D Histogram with Custom Bins)
    layout:
      title:
        text: 2D Histogram with Custom Bins<br><sub>Custom Binning Along X and Y Axes</sub>
      xaxis:
        title:
          text: "X Axis"
      yaxis:
        title:
          text: "Y Axis"

Here's a histogram2d plot that uses density mode, displaying the relative density of data points across the grid:

Here's the code:

models:
  - name: histogram2d-data-density
    args:
      - echo
      - |
        x,y
        1,1
        2,2
        3,3
        2,1
        3,2
        4,3
        5,4
        4,1
        5,2
traces:
  - name: 2D Histogram with Density Mode
    model: ref(histogram2d-data-density)
    props:
      type: histogram2d
      x: ?{x}
      y: ?{y}
      histnorm: "density"
      colorscale: "Jet"
charts:
  - name: 2D Histogram with Density Mode
    traces:
      - ref(2D Histogram with Density Mode)
    layout:
      title:
        text: 2D Histogram with Density Mode<br><sub>Density Distribution of Data</sub>
      xaxis:
        title:
          text: "X Axis"
      yaxis:
        title:
          text: "Y Axis"

Attributes

These attributes apply to traces where trace.props.type is set to histogram2d. You would configure these attributes on the trace with the trace.props object.

autobinx: 'boolean' 
autobiny: 'boolean' 
autocolorscale: 'boolean' 
bingroup: 'string' 
coloraxis: 'subplotid' 
colorbar:
  bgcolor: 'color' 
  bordercolor: 'color' 
  borderwidth: 'number greater than or equal to 0' 
  dtick: 'number or categorical coordinate string' 
  exponentformat: 'enumerated , one of ( "none" | "e" | "e" | "power" | "si" | "b"
    )' 
  len: 'number greater than or equal to 0' 
  lenmode: 'enumerated , one of ( "fraction" | "pixels" )' 
  minexponent: 'number greater than or equal to 0' 
  nticks: 'integer greater than or equal to 0' 
  orientation: 'enumerated , one of ( "h" | "v" )' 
  outlinecolor: 'color' 
  outlinewidth: 'number greater than or equal to 0' 
  separatethousands: 'boolean' 
  showexponent: 'enumerated , one of ( "all" | "first" | "last" | "none" )' 
  showticklabels: 'boolean' 
  showtickprefix: 'enumerated , one of ( "all" | "first" | "last" | "none" )' 
  showticksuffix: 'enumerated , one of ( "all" | "first" | "last" | "none" )' 
  thickness: 'number greater than or equal to 0' 
  thicknessmode: 'enumerated , one of ( "fraction" | "pixels" )' 
  tick0: 'number or categorical coordinate string' 
  tickangle: 'angle' 
  tickcolor: 'color' 
  tickfont:
    color: color
    family: 'string' 
    size: number greater than or equal to 1
  tickformat: 'string' 
  tickformatstops:
    dtickrange: 'array' 
    enabled: 'boolean' 
    name: 'string' 
    templateitemname: 'string' 
    value: 'string' 
  ticklabeloverflow: 'enumerated , one of ( "allow" | "hide past div" | "hide past
    domain" )' 
  ticklabelposition: 'enumerated , one of ( "outside" | "inside" | "outside top" |
    "inside top" | "outside left" | "inside left" | "outside right" | "inside right"
    | "outside bottom" | "inside bottom" )' 
  ticklabelstep: 'integer greater than or equal to 1' 
  ticklen: 'number greater than or equal to 0' 
  tickmode: 'enumerated , one of ( "auto" | "linear" | "array" )' 
  tickprefix: 'string' 
  ticks: 'enumerated , one of ( "outside" | "inside" | "" )' 
  ticksuffix: 'string' 
  ticktext: 'data array' 
  tickvals: 'data array' 
  tickwidth: 'number greater than or equal to 0' 
  title:
    font:
      color: color
      family: 'string' 
      size: number greater than or equal to 1
    side: 'enumerated , one of ( "right" | "top" | "bottom" )' 
    text: 'string' 
  x: 'number between or equal to -2 and 3' 
  xanchor: 'enumerated , one of ( "left" | "center" | "right" )' 
  xpad: 'number greater than or equal to 0' 
  y: 'number between or equal to -2 and 3' 
  yanchor: 'enumerated , one of ( "top" | "middle" | "bottom" )' 
  ypad: 'number greater than or equal to 0' 
colorscale: 'colorscale' 
customdata: 'data array' 
histfunc: 'enumerated , one of ( "count" | "sum" | "avg" | "min" | "max" )' 
histnorm: 'enumerated , one of ( "" | "percent" | "probability" | "density" | "probability
  density" )' 
hoverinfo: 'flaglist string. any combination of "x", "y", "z", "text", "name" joined
  with a "+" or "all" or "none" or "skip".' 
hoverlabel:
  align: 'enumerated or array of enumerateds , one of ( "left" | "right" | "auto"
    )' 
  bgcolor: 'color or array of colors' 
  bordercolor: 'color or array of colors' 
  font:
    color: color or array of colors
    family: 'string or array of strings' 
    size: number or array of numbers greater than or equal to 1
  namelength: 'integer or array of integers greater than or equal to -1' 
hovertemplate: 'string or array of strings' 
ids: 'data array' 
legendgroup: 'string' 
legendgrouptitle:
  font:
    color: color
    family: 'string' 
    size: number greater than or equal to 1
  text: 'string' 
legendrank: 'number' 
legendwidth: 'number greater than or equal to 0' 
marker:
  color: 'data array' 
meta: 'number or categorical coordinate string' 
nbinsx: 'integer greater than or equal to 0' 
nbinsy: 'integer greater than or equal to 0' 
opacity: 'number between or equal to 0 and 1' 
reversescale: 'boolean' 
showlegend: 'boolean' 
showscale: 'boolean' 
textfont:
  color: color
  family: 'string' 
  size: number greater than or equal to 1
texttemplate: 'string' 
type: histogram2d
uirevision: 'number or categorical coordinate string' 
visible: 'enumerated , one of ( true | false | "legendonly" )' 
x: 'data array' 
xaxis: 'subplotid' 
xbingroup: 'string' 
xbins:
  end: 'number or categorical coordinate string' 
  size: 'number or categorical coordinate string' 
  start: 'number or categorical coordinate string' 
xcalendar: 'enumerated , one of ( "chinese" | "coptic" | "discworld" | "ethiopian"
  | "gregorian" | "hebrew" | "islamic" | "jalali" | "julian" | "mayan" | "nanakshahi"
  | "nepali" | "persian" | "taiwan" | "thai" | "ummalqura" )' 
xgap: 'number greater than or equal to 0' 
xhoverformat: 'string' 
y: 'data array' 
yaxis: 'subplotid' 
ybingroup: 'string' 
ybins:
  end: 'number or categorical coordinate string' 
  size: 'number or categorical coordinate string' 
  start: 'number or categorical coordinate string' 
ycalendar: 'enumerated , one of ( "chinese" | "coptic" | "discworld" | "ethiopian"
  | "gregorian" | "hebrew" | "islamic" | "jalali" | "julian" | "mayan" | "nanakshahi"
  | "nepali" | "persian" | "taiwan" | "thai" | "ummalqura" )' 
ygap: 'number greater than or equal to 0' 
yhoverformat: 'string' 
z: 'data array' 
zauto: 'boolean' 
zhoverformat: 'string' 
zmax: 'number' 
zmid: 'number' 
zmin: 'number' 
zsmooth: 'enumerated , one of ( "fast" | "best" | false )'