0

I would like to achieve this:

"$schema": "https://vega.github.io/schema/vega-lite/v4.json",
  "mark": {"type": "bar", "tooltip": true},
  "encoding": {

    "x": {"field": "Creative Type", "type": "nominal", "aggregate": null},
    "y": {"field": "Creative Type", "type": "nominal", "aggregate": "count"}

I am passing the values from Python to vegalite. Since Python does not have a null keyword, I don't know how to set the value for aggregate as null in the x axis. Any help is greatly appreciated. Thank you!

jakevdp
  • 77,104
  • 11
  • 125
  • 160
vid1505
  • 43
  • 3

2 Answers2

0

Equivalent of null keyword in Python will be None. Please check if that works.

Daniel Bibik
  • 51
  • 1
  • 4
0

In general, passing None in Python/Altair will result in null in JSON/vega. For example, when hiding a legend ("legend": null in Vega-Lite) you can use legend=None in Altair, as mentioned in Adjusting the Legend.

In the case of aggregates, however, that will not work: according to the Vega-Lite schema, null is not a valid argument for aggregate, and so attempting this in Altair will result in a SchemaValidationError.

If for some reason you really want to produce this invalid specification, you can do so by passing validate=False to alt.Chart.to_json:

import altair as alt
chart = alt.Chart().mark_bar(tooltip=True).encode(
    x=alt.X('Creative Type:N', aggregate=None),
    y=alt.Y('Creative Type:N', aggregate='count')
)
print(chart.to_json(validate=False))
# {
#   "$schema": "https://vega.github.io/schema/vega-lite/v4.8.1.json",
#   "config": {
#     "view": {
#       "continuousHeight": 300,
#       "continuousWidth": 400
#     }
#   },
#   "encoding": {
#     "x": {
#       "aggregate": null,
#       "field": "Creative Type",
#       "type": "nominal"
#     },
#     "y": {
#       "aggregate": "count",
#       "field": "Creative Type",
#       "type": "nominal"
#     }
#   },
#   "mark": {
#     "tooltip": true,
#     "type": "bar"
#   }
# }
jakevdp
  • 77,104
  • 11
  • 125
  • 160