I'm working off this example
https://altair-viz.github.io/user_guide/configuration.html#config-title
but I can't seem to get the given result even with (almost direct) replication.
I don't have the vega dataset so i just pinched the URL and used
source = "https://vega.github.io/vega-datasets/data/cars.json"
chart = alt.Chart(source).mark_point().encode(
x='Horsepower:Q',
y='Miles_per_Gallon:Q',
).properties(
title='Cars Data'
)
chart.configure_title(
fontSize=20,
font='Courier',
anchor='start',
color='gray'
)
chart.save('test.html')
chart.save('test.json')
However, the title is unchanged, and when i produce the JSON to see if it's a rendering thing i get
{"config": {"view": {"width": 400, "height": 300}, "mark": {"tooltip": null}}, "data": {"url": "https://vega.github.io/vega-datasets/data/cars.json"}, "mark": "point", "encoding": {"x": {"type": "quantitative", "field": "Horsepower"}, "y": {"type": "quantitative", "field": "Miles_per_Gallon"}}, "title": "Cars Data", "$schema": "https://vega.github.io/schema/vega-lite/v3.4.0.json"}
which seems to suggest the configure title isn't working, or am i missing something?
A related question: is putting the title in the chart definition equivalent to setting it via .property? I really like Altair so far, but one of my major gripes with most plotting in Python (especially mpl) is there are multiple ways of doing effectively the same thing and the examples tend to be mixed, which makes trying to gain any kind of intuition to determine what syntax might make sense when trying something new really difficult.
chart.configure_title(**kwds), like all Altair methods, does not modify the chart in place, but rather returns an updated copy of the chart. In the example you linked to, the updated copy is visualized directly.
If you store the result of the update, your code will work as you expect:
source = "https://vega.github.io/vega-datasets/data/cars.json"
chart = alt.Chart(source).mark_point().encode(
x='Horsepower:Q',
y='Miles_per_Gallon:Q',
).properties(
title='Cars Data'
)
chart = chart.configure_title( # <----- this is the only difference
fontSize=20,
font='Courier',
anchor='start',
color='gray'
)
chart.save('test.html')
chart.save('test.json')
Regarding your second question, yes, the following two specifications are equivalent:
chart1 = alt.Chart(data, title="blah").mark_point().encode(x='x')
chart2 = alt.Chart(data).mark_point().encode(x='x').properties(title="blah")
As much as possible, we try to use the second option in our examples.
We added properites because the typical pattern is to pass data only to the chart constructor, and it's useful to be able to update the chart properties at the end.
Aha, right you are, thanks! Rookie mistake there (thought I tried reassigning the object).
The next question w.r.t this is I want to do modify the title formats of each of the subplots in this
hist_all = alt.Chart(df).mark_bar().encode(
alt.X("p_alive:Q", bin=True),
y='count()',
tooltip=['count()']
).properties(title="Cohort")
hist_single = alt.Chart(df[df['frequency']==1]).mark_bar().encode(
alt.X("p_alive:Q", bin=True),
y='count()',
tooltip=['count()']
).properties(title="Once-off")
hist_repeat = alt.Chart(df[df['frequency']!=1]).mark_bar().encode(
alt.X("p_alive:Q", bin=True),
y='count()',
tooltip=['count()']
).properties(title='Recurring')
chart = (hist_all | hist_single | hist_repeat)
chart.save('chart.html')
However if I try to set the config on the subplots i get
ValueError: Objects with "config" attribute cannot be used within HConcatChart. Consider defining the config attribute in the HConcatChart object instead.
I have tried setting it on the concatenated chart it also fails. How would I go about nicely formatting the titles of the subplots? Am I forced to combined all the data into 1 frame with a dummy column and wrap things around?
Thanks,
Z
config is only supported at the top level (it's essentially a global theme). So you can call config on the final chart, e.g.
chart = (hist_all | hist_single | hist_repeat).configure_title(
fontSize=20,
font='Courier',
anchor='start',
color='gray'
)