Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example of left aligning title with axis line via frame=group #3402

Closed
joelostblom opened this issue Apr 12, 2024 · 2 comments · Fixed by #3537
Closed

Add example of left aligning title with axis line via frame=group #3402

joelostblom opened this issue Apr 12, 2024 · 2 comments · Fixed by #3537

Comments

@joelostblom
Copy link
Contributor

joelostblom commented Apr 12, 2024

What is your suggestion?

I didn't know about frame='group' until vega/vega#3903. I have been avoiding using left aligned titles because I don't like the aesthetics of them going past the y-axis line. Now that's no longer an issue and it looks so great when it's aligned with the axis line:

import altair as alt
from vega_datasets import data


source = data.movies.url

title = alt.Title('A title for my histogram', anchor='start')
chart = alt.Chart(source, title=title, height=200).mark_bar().encode(
    alt.X("IMDB_Rating:Q", bin=True),
    y='count()',
)

title2 = alt.Title('A title for my histogram', anchor='start', frame='group')

chart | chart.properties(title=title2)

image

Have you considered any alternative solutions?

No response

@dsmedia
Copy link
Contributor

dsmedia commented Aug 13, 2024

@joelostblom Do you have something like this in mind for the example gallery?

import altair as alt
from vega_datasets import data

source = data.movies.url

# Base chart
base = alt.Chart(source).mark_bar().encode(
    alt.X("IMDB_Rating:Q", bin=True),
    y='count()',
).properties(
    width=200,
    height=200
)

# Chart 1: Left align to axis (title starts at axis line)
chart1 = base.encode(
    color=alt.value('steelblue')
).properties(
    title=alt.Title('Left Aligned to Axis', anchor='start', frame='group')
)

# Chart 2: Right align to axis, y-axis on the right
chart2 = base.encode(
    alt.Y('count()', axis=alt.Axis(orient='right')),
    color=alt.value('steelblue')
).properties(
    title=alt.Title('Right Aligned to Axis', anchor='end', frame='group')
)

# Chart 3: Left align to chart (title starts at chart edge)
chart3 = base.encode(
    color=alt.value('orange')
).properties(
    title=alt.Title('Left Aligned to Chart', anchor='start')
)

# Chart 4: Right align to chart edge, y-axis on the right
chart4 = base.encode(
    alt.Y('count()', axis=alt.Axis(orient='right')),
    color=alt.value('orange')
).properties(
    title=alt.Title(['Right Aligned to Chart'], anchor='end')
)

# Combine charts in a 2x2 grid
grid = (alt.vconcat(
    alt.hconcat(chart1, chart2),
    alt.hconcat(chart3, chart4)
)).resolve_scale(
)

grid

viz

@joelostblom
Copy link
Contributor Author

Thanks @dsmedia! After reviewing the docs I'm thinking it would be more helpful if we add this example to the title section of the User Guide rather than to the example gallery. In general I think it is easier to find when we manage to fit information and examples into the user guide, but it is not always easy to find the right spot.

Maybe you example can be appended to the section I linked above? The last example currently is showing the "caption" going out all the way to the left of the frame, so it I think it could fit naturally to explain how to do different alignments after that. Instead of the histogram you can use the same energy area chart as in the other examples. What do you think?

dsmedia added a commit to dsmedia/altair that referenced this issue Aug 13, 2024
explains what is meant in this case by 'group', expands on meaning of other title attributes, and changes doc link reference from "customization" to "configuration", where the chart title properties are discussed.
Resolves vega#3402
joelostblom added a commit that referenced this issue Aug 13, 2024
* docs: Adds frame attribute.
explains what is meant in this case by 'group', expands on meaning of other title attributes, and changes doc link reference from "customization" to "configuration", where the chart title properties are discussed.
Resolves #3402

* Shorten line length

* Make a clearer distinction of what happens when the title frame is changed

* Fix typo

---------

Co-authored-by: Joel Ostblom <joel.ostblom@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants