question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Chart sizing through custom theme doesn't work

See original GitHub issue

Hello Altair community, I am trying to set the width and height of chart through theme setting, however it doesn’t seem to be working. Am I setting the wrong keys or is this a bug? Please see below for detail:

# custom theme
def long_chart():
    return {
        'view': {
            'width'  : 500,
            'height' : 150      
        }
    }
alt.themes.register('long_chart', long_chart)

with alt.themes.enable('long_chart'):
    chart1 = alt.Chart(population).mark_point().encode(
        x='year:O',
        y='sum(people)',
        color='sex:N'
    ).properties(
        title='Chart sizing thru custom theme'
    )
    
chart2 = alt.Chart(
    population,
    width=500,
    height=150).mark_point().encode(
    x='year:O',
    y='sum(people)',
    color='sex:N'
    ).properties(
    title='Chart sizing thru Chart()'
    )

chart1 | chart2 

visualization I was expecting chart1 to look exactly the same as chart2. Looking at the json spec of chart1, it seems that the width and height is still the default value: chart1.to_dict()

{'config': {'view': {'width': 400, 'height': 300}},
 'data': {'name': 'data-45e35b5faa42c7034b347e8bcfc8b805'},
 'mark': 'point',
 'encoding': {'color': {'type': 'nominal', 'field': 'sex'},
  'x': {'type': 'ordinal', 'field': 'year'},
  'y': {'type': 'quantitative', 'aggregate': 'sum', 'field': 'people'}},
 'title': 'Chart sizing thru custom theme',
 '$schema': 'https://vega.github.io/schema/vega-lite/v2.6.0.json',
 'datasets': {'data-45e35b5faa42c7034b347e8bcfc8b805': [{'age': 0,
    'people': 1483789,
    'sex': 1,
    'year': 1850},
   {'age': 0, 'people': 1450376, 'sex': 2, 'year': 1850},
...

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jakevdpcommented, Nov 19, 2018

And note that themes, and configuration settings in general, can only be applied to top-level charts. There is no way in Vega-Lite to set different global configuration options for different sub-charts.

1reaction
FlorianGDcommented, Nov 19, 2018

When exiting the context manager, the settings are reset, even for saved charts. I do not know if it is normal, but you can see it this way

import altair as alt
from vega_datasets import data
from pprint import pprint

population = data.population.url

def long_chart():
    return {
        'config': {
            'view': {
                'height': 150,
                'width': 500,
            },
        }
    }

alt.themes.register('long_chart', long_chart)
#> <function __main__.long_chart()>

with alt.themes.enable('long_chart'):
    chart1 = alt.Chart(population).mark_point().encode(
        x='year:O',
        y='sum(people):Q',
        color='sex:N'
    ).properties(
        title='Chart sizing thru custom theme'
    )
    print('Inside of the context manager')
    pprint(chart1.to_dict())
#> Inside of the context manager
#> {'$schema': 'https://vega.github.io/schema/vega-lite/v2.6.0.json',
#>  'config': {'view': {'height': 150, 'width': 500}},
#>  'data': {'url': 'https://vega.github.io/vega-datasets/data/population.json'},
#>  'encoding': {'color': {'field': 'sex', 'type': 'nominal'},
#>               'x': {'field': 'year', 'type': 'ordinal'},
#>               'y': {'aggregate': 'sum',
#>                     'field': 'people',
#>                     'type': 'quantitative'}},
#>  'mark': 'point',
#>  'title': 'Chart sizing thru custom theme'}

print('Oustide of the context manager')
#> Oustide of the context manager
pprint(chart1.to_dict())
#> {'$schema': 'https://vega.github.io/schema/vega-lite/v2.6.0.json',
#>  'config': {'view': {'height': 300, 'width': 400}},
#>  'data': {'url': 'https://vega.github.io/vega-datasets/data/population.json'},
#>  'encoding': {'color': {'field': 'sex', 'type': 'nominal'},
#>               'x': {'field': 'year', 'type': 'ordinal'},
#>               'y': {'aggregate': 'sum',
#>                     'field': 'people',
#>                     'type': 'quantitative'}},
#>  'mark': 'point',
#>  'title': 'Chart sizing thru custom theme'}

Created on 2018-11-19 by the reprexpy package

Read more comments on GitHub >

github_iconTop Results From Across the Web

Shopify tutorial on size chart doesn't work (using a free theme)
Re: Shopify tutorial on size chart doesn't work (using a free theme) · 1. Go to Online Store > Actions > Edit Code....
Read more >
Custome Theme doesnt overwrite changes made in the report
Hi,. i preapared simple global rule to change fonts on all charts. ... Unfortunately when i import it into PowerBI Desktop it changes...
Read more >
Increasing font size with a custom theme - Qlik Community
I've been able to adjust the title font size by following the instructions found at . This code works to adjust the title...
Read more >
How to get a custom design theme for your visualizations
The charts, maps, and tables you create with Datawrapper can be fully customized to fit in with your organization's design style guide. To...
Read more >
Motion Support: How do I set up a product size chart?
Setup your size chart block Open the theme editor and navigate to a ... Create new alternate product templates by selecting Products in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found