Search code examples
pythonvisualizationaltair

How to specify explicit axis values with Altair labelExpr. Expression Parse Error


I am trying to implement the hexbin plot example. However, I am running into issues with my data being fractional. In the example, they use

.transform_calculate(
    # This field is required for the hexagonal X-Offset
    xFeaturePos='(day(datum.' + yField + ') % 2) / 2 + month(datum.' + xField + ')'
)

to create the staggered offset. I found with fractional x and y values this causes the hexagons to be adjusted increasingly to the right.

enter image description here

I solved this problem by creating two index columns that are integers that represent my x and y values.

Hexagonal bin plot with integer axis

I found the values parameter in the alt.Axis() object with I have tried using to overwrite the axis labels.

enter image description here

Unfortunately, scaling is incorrect. I know a regular rectangular heat map wouldn't have this issue. But because each hexagon is from a mean of binned data, I like to think that the hexagon helps remind the audience the data is binned. (was also planning to add that tidbit of info in the x-axis or on the title)

Also, in all cases the x-axis resolution is not high enough, though that I think would be a simpler problem.


update I've found I can use the labelExpr parameter in alt.Axis to append strings to the value.

enter image description here

Unfortunately I cannot seem too completely rewrite them. I've take approaches from this post. I am trying to use vega expressions to modify just the first label.

labelExpr="datum.label == 1.5 ? '0.5'"

enter image description here


Solution

  • Solved. Missing the default value after the vega expression for else, :