introduce an array
mark utilizing the heatmap transform for array data
#9389
Labels
array
mark utilizing the heatmap transform for array data
#9389
This feature request proposes the addition of a new
array
mark to Vega-Lite.This mark aims to improve support for the visualization of various types of 2D data, including heatmaps, image data, and other matrix-based representations, with built-in support for color scales, axis labels, and faceting. I see this is an initial step towards #6043, as this focus on just a single transform in Vega, but many issues discussed in that issue also apply to this issue.
The following variants are an exploration on how the heatmap transform within Vega behaves, and how data can be prepared for ingestion within the specification. This is an initial attempt that can hopefully serve as a starting point to explore this field a bit more with the hope that someone is brave enough to turn this into an attempt for a PR.
variants explored so far
Note: in the specs below, I've reduced the length of the grid values. In the accompanying Vega-Editor links all values of the grids are included.
heatmap transform values only
A basic implementation using numpy to generate a heatmap from a single array, displaying it with Vega. The image is rendered with opacity levels only.
We can make it work using the
heatmap
transform in Vega, using the following specification (Vega-Editor):The result looks like this:
It seems this is the image drawn with opacity levels only.
heatmap transform with color scale
Adding a color scale to the heatmap to enhance visual differentiation of values. This example replicates a grayscale image using Vega's color scale functionality.
Let's add a color scale (Vega-Editor):
The result will look like this:
Using this approach, I also can reproduce the grayscale image like in python using
plt.imshow()
.By modifying the color scale as such (Vega-Editor):
heatmap transform with color scale and axis
Enhancing the previous example by including axis labels, providing context to the grid values. This facilitates interpretation of the data.
Next step is to add axis to the image.
The Vega specification now looks as such (Vega-Editor):
So far so good.
heatmap transform double array faceted with color scale and axis
Faceting multiple grids within a single visualization. This example demonstrates handling of two separate arrays with independent color scales and axis labels.
Are we able to facet grids, if we have for example two grids as input?
I've adapted my python code to prepare the data arrays:
And modified the Vega specification. This now looks as such (Vega-Editor):
Not bad!
heatmap transform single array with non-zero x and y scale
Handling grids with custom scales, such as geographical data. This example showcases the challenges of aligning non-zero axes with grid dimensions and values.
This variant is still a bit difficult. The array is in unit degrees and goes on the x-axis from
-180
to180
longitude and on the y-axis from-81
to87
latitude. The step-size is 1 degrees in both directions.See Vega-Editor:
This results in:
Basically, for the grid only use the
height
andwidth
to allocate the canvas size and iterate over the 1D array to colorize each pixel.For the
X_SCALE
andY_SCALE
we use the information ofx1
/x2
andy1
/y2
(still manually). We use the"datum.grid.width"
and"datum.grid.height"
assignal
for within the image mark encoding. Since the scales also need a width and height, the globalwidth
/height
are currently still set to the same witdth and height of the grid.But if I change the grid input object to:
(removing the appended
_
fromx1
/x2
/y1
/y2
)The result is this:
I've the feeling all negative values of our scales malfunction in the iterator within heatmap.js (here). But then it seems the drawn y-axis is reversed for the canvas iterator. If I add a
"reverse":true
to the scaleY_SCALE
then it becomes more clear that only positive values are colorized in the canvas:But then the latitude values on the y-axis does not match the input array.
heatmap transform double array with non-zero x and y scale
A more complex scenario with faceted charts using custom scales. This variant highlights the issues with global versus array-specific dimensions and independent color scales.
Lets make it a bit more complex. A facetted chart with non-zero x and y scales. Lets start with data preparation in python:
When prepararing a vega chart for this as such, See Vega-Editor:
Two issues become clear from this:
width
andheight
and the array-definedgrid.width
andgrid.height
.Proposed Specification
This is already more discussed within #6043, but something as such should be sufficient for many things (notice there is no need for an
x
andy
encoding channel, as the 2D array data comes prepared).With a new
array
mark it is hoped we can simplify syntax to specify array data, simultaneously still support handling of color schemes, with options for customization including integration with Vega-Lite's axis and scale system, supporting both zero and non-zero scales.More over it is shown that faceting of multiple arrays is a real possibility even though maintaining independent scales and axes is something to be explored more deeply.
Performance optimization has not been part of this exploration, but it is to be noted that it would be great if the result of a heatmap transform, a canvas image, can be included within the JSON specification, meaning that the application of the heatmap transform can be done server-side. Currently it is unclear if this is accepted within the JSON standard.
This issue is one of the results of a spontaneous attempt to bring vega/altair#891 further. Thanks for brainstorming on this topic @kanitw, @timtreis, @melonora and @joelostblom!
The text was updated successfully, but these errors were encountered: