简体   繁体   中英

altair grouped bar chart format axis and padding

I am working with Altair and loving it for quick visualization. I am struggling a bit regarding fine-tuning the formatting though, especially regarding padding of the grouped bar charts and trying to get a non-repeating title.

Please see the plot so far: Plot so far and the code generating the plot:

dfc = pd.DataFrame({'Alternative': {0: 0, 1: 1, 2: 0, 3: 1, 4: 0, 5: 1, 6: 0, 7: 1},
 'Time': {0: 1979,
  1: 1979,
  2: 1980,
  3: 1980,
  4: 1980,
  5: 1980,
  6: 1980,
  7: 1980},
 'Sim #': {0: 2688, 1: 3564, 2: 86, 3: 424, 4: 743, 5: 884, 6: 1530, 7: 1612},
 'Cost_MNOK': {0: .5,
  1: .4,
  2: 2,
  3: 3,
  4: 30,
  5: 40,
  6: 200,
  7: 300},
 'freq': {0: 5.555555555555556e-06,
  1: 5.555555555555556e-05,
  2: 5.555555555555556e-06,
  3: 5.555555555555556e-05,
  4: 5.555555555555556e-06,
  5: 5.555555555555556e-05,
  6: 5.555555555555556e-06,
  7: 5.555555555555556e-05},
 'Bin_text': {0: '0-1 MNOK',
  1: '0-1 MNOK',
  2: '1-10 MNOK',
  3: '1-10 MNOK',
  4: '10-100 MNOK',
  5: '10-100 MNOK',
  6: '100+ MNOK',
  7: '100+ MNOK'},
 'Bin': {0: 1, 1: 1, 2: 2, 3: 2, 4: 3, 5: 3, 6: 4, 7: 4}})


heatmap_colors = ['#edf8fb', '#b2e2e2', '#66c2a4', 'red']
alt.Chart(dfc).mark_bar().encode(
    alt.Column('Bin_text', header=alt.Header(title='Occurrence of failures grouped by cost')),
    alt.X('Alternative:N', axis=alt.Axis(orient='bottom', ), title='Alt'),
    alt.Y('sum(freq)', axis=alt.Axis(grid=False, format='%', title='Frequency')),
    alt.Color('Bin_text', scale=alt.Scale(range=heatmap_colors), legend=alt.Legend(title='Bins')),
    alt.Tooltip('sum(freq)', format='.2%')
).configure_view(strokeOpacity=0)

What I am trying to achieve is the following:

  • At the top row, the titles of my bar chart groups are truncated. I would like to prevent that, but also prevent overlap. I expect that this should be done by increasing the distance between the groups, but I cannot figure out how to do this. I tried among others adding a Scale object to the Column object, but this is not an allowed argument anymore (seemed to be ok in altair v1). EDIT: I have found a way to do this: adding .properties(width=200) at the very end of the plot provides more spacing, and the plot titles become readable. The other part of the question still stands though.
  • At the bottom of the plot, the title 'Alt' repeats for all groups. I would rather have one title here instead of the same text repeating.

Any ideas and tips are welcome!

As of Altair v2.1 / Vega-Lite v2.6, there is no way to change the padding between facets, or to customize facet attributes such as titles.

The relevant issue to track this feature is here: https://github.com/vega/vega-lite/issues/2446

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM