I'm trying to improve the following figure by giving the x axis different "sizes": Figure The periods of Manager1 and Manager2 are different (the first lasts around 5 years, and the 2nd, around 1.5 years). I'd like the sizes of the bar charts to refflect this difference. The data is the following:
import pandas as pd
from pandas import Timestamp
example_data = {'Responsible': {516: 'p1',
517: 'p2',
518: 'p3',
701: 'p4',
702: 'p5',
703: 'p6',
704: 'p7',
705: 'p8',
706: 'p9'},
'Manager': {516: 'Manager1',
517: 'Manager1',
518: 'Manager2',
701: 'Manager1',
702: 'Manager1',
703: 'Manager1',
704: 'Manager2',
705: 'Manager2',
706: 'Manager2'},
'start': {516: Timestamp('2011-07-28 00:00:00'),
517: Timestamp('2013-07-24 00:00:00'),
518: Timestamp('2016-07-28 00:00:00'),
701: Timestamp('2011-10-21 00:00:00'),
702: Timestamp('2013-07-24 00:00:00'),
703: Timestamp('2014-02-24 00:00:00'),
704: Timestamp('2016-07-28 00:00:00'),
705: Timestamp('2017-07-27 00:00:00'),
706: Timestamp('2018-01-09 00:00:00')},
'end': {516: Timestamp('2013-07-22 00:00:00'),
517: Timestamp('2016-07-28 00:00:00'),
518: Timestamp('2018-03-23 00:00:00'),
701: Timestamp('2013-07-22 00:00:00'),
702: Timestamp('2014-02-24 00:00:00'),
703: Timestamp('2016-07-28 00:00:00'),
704: Timestamp('2017-07-27 00:00:00'),
705: Timestamp('2018-01-09 00:00:00'),
706: Timestamp('2018-04-02 00:00:00')},
'gender': {516: 'M',
517: 'F',
518: 'M',
701: 'F',
702: 'F',
703: 'F',
704: 'F',
705: 'F',
706: 'M'},
'position': {516: 'Place1',
517: 'Place1',
518: 'Place1',
701: 'Place2',
702: 'Place2',
703: 'Place2',
704: 'Place2',
705: 'Place2',
706: 'Place2'}}
new_df = pd.DataFrame.from_dict(example_data)
The code is the following:
import altair as alt
rad = 10
alt.Chart(new_df).mark_bar(cornerRadiusTopLeft= rad,
cornerRadiusTopRight = rad,
cornerRadiusBottomLeft = rad,
cornerRadiusBottomRight = rad, ).encode(
x= alt.X('start', title = 'Period per person'),
x2= 'end',
y=alt.Y('position'),
color=alt.Color('gender'),
tooltip=['Responsible'],
column='Manager'
).resolve_scale(x = 'independent')
I don't think there is a way to change the size of facetted charts individually. You could change the domain by not resolving the scale but the shared sizes would still be equal.
If you want different chart sizes, I belive you have to create them separetely and then concatenate them together, which you could do like this:
charts = []
widths = {'Manager1': 400, 'Manager2': 100}
for group_name, group_df in new_df.groupby('Manager'):
charts.append(
alt.Chart(group_df, width=widths[group_name]).mark_bar(
cornerRadiusTopLeft=rad,
cornerRadiusTopRight=rad,
cornerRadiusBottomLeft=rad,
cornerRadiusBottomRight=rad
).encode(
x=alt.X('start', title='Period per person'),
x2='end',
y=alt.Y('position'),
color=alt.Color('gender'),
tooltip=['Responsible'],
)
)
alt.hconcat(*charts)
If you have more groups you could imporove upon this solution and make it more automatic by setting the width depending on the time range instead of using fixed numbers as in this example.