如何设置不同 "sizes" 的独立 x 尺度?
how to set independent x scales with different "sizes"?
我试图通过给 x 轴不同的“大小”来改进下图:
Figure
Manager1和Manager2的周期不同(第一个持续5年左右,第二个持续1.5年左右)。我想要条形图的大小来反映这种差异。
数据如下:
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)
代码如下:
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')
我认为没有办法单独更改多面图表的大小。您可以通过不解析比例来更改域,但共享大小仍然相等。
如果您想要不同的图表大小,我相信您必须单独创建它们,然后将它们连接在一起,您可以这样做:
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)
如果您有更多组,您可以改进此解决方案并通过根据时间范围设置宽度而不是像本例中那样使用固定数字来使其更加自动化。
我试图通过给 x 轴不同的“大小”来改进下图: Figure Manager1和Manager2的周期不同(第一个持续5年左右,第二个持续1.5年左右)。我想要条形图的大小来反映这种差异。 数据如下:
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)
代码如下:
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')
我认为没有办法单独更改多面图表的大小。您可以通过不解析比例来更改域,但共享大小仍然相等。
如果您想要不同的图表大小,我相信您必须单独创建它们,然后将它们连接在一起,您可以这样做:
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)
如果您有更多组,您可以改进此解决方案并通过根据时间范围设置宽度而不是像本例中那样使用固定数字来使其更加自动化。