Altair: Increase size of legend

Created on 27 Jun 2018  路  8Comments  路  Source: altair-viz/altair

Is there a property to set the size of a legend? Right now my legends (irrespective the 'orient' placement) contain text labels that are cut-off.

question

Most helpful comment

encode(
  color=alt.Color('name',
    legend=alt.Legend(title='Name', orient = 'right'), 
    scale=scale)
).configure_legend(labelLimit= 0)

visualization 12

Perfect, thanks!

All 8 comments

Can you show an example?

Yes!
Altair code:

names = df_t['name'].unique().tolist()
scale = alt.Scale(domain=names, range=['#808080', '#FFD200'])
chart = alt.Chart(df_t).mark_bar().encode(
y=alt.Y('week:O'),
x=alt.X('amount:Q', scale=alt.Scale(domain=[0,40]) ),
color=alt.Color('name', legend=alt.Legend(title='Nname', orient = 'right'), scale=scale))
chart

Generated JSON:

{
  "config": {"view": {"width": 400, "height": 300}},
  "data": {
    "values": [
      {"name": "123456789 - Long name", "amount": 19, "week": "w0"},
      {"name": "123456789 - Long name", "amount": 19, "week": "w1"},
      {
        "name": "123456789123456789123456789 - Even longer name",
        "amount": 2,
        "week": "w0"
      },
      {
        "name": "123456789123456789123456789 - Even longer name",
        "amount": 2,
        "week": "w1"
      },
      {
        "name": "123456789123456789123456789 - Even longer name",
        "amount": 4,
        "week": "w2"
      },
      {
        "name": "123456789123456789123456789 - Even longer name",
        "amount": 5,
        "week": "w3"
      }
    ]
  },
  "mark": "bar",
  "encoding": {
    "color": {
      "type": "nominal",
      "field": "name",
      "legend": {"orient": "right", "title": "Nname"},
      "scale": {
        "domain": [
          "123456789 - Long name",
          "123456789123456789123456789 - Even longer name"
        ],
        "range": ["#808080", "#FFD200"]
      }
    },
    "x": {
      "type": "quantitative",
      "field": "amount",
      "scale": {"domain": [0, 40]}
    },
    "y": {"type": "ordinal", "field": "week"}
  },
  "$schema": "https://vega.github.io/schema/vega-lite/v2.4.1.json"
}

Resulting graph:
visualization 11

Although this is a ridiculously long name, I would like to show it in full in the legend.

This is a good question for the vega-lite folks CC/ @domoritz @kanitw

You can set

"config": {
  "legend": {
    "labelLimit": 0
  }
}
encode(
  color=alt.Color('name',
    legend=alt.Legend(title='Name', orient = 'right'), 
    scale=scale)
).configure_legend(labelLimit= 0)

visualization 12

Perfect, thanks!

Thanks!

You can set
"config": {
"legend": {
"labelLimit": 0
}
}

Thank You domoritz you saved me :)

Also worked for me. Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

maxgerma picture maxgerma  路  3Comments

LukeMathWalker picture LukeMathWalker  路  3Comments

firasm picture firasm  路  3Comments

Juan-132 picture Juan-132  路  3Comments

fischcheng picture fischcheng  路  4Comments