虚拟数据:
sub categories
图:
import pandas as pd
new_data = {
'date': pd.date_range('2022-01-01', periods=11, freq="ME"),
'parent_category': ['Electronics', 'Electronics', 'Fashion', 'Fashion', 'Home Goods', 'Electronics', 'Fashion','Electronics','Electronics','Electronics','Electronics'],
'child_category': ['Smartphones', 'Laptops', 'Shirts', 'Pants', 'Kitchenware','Laptops', 'Shirts', 'Smartphones','PS4','Oven','Vaccum cleaner']
}
new_data = pd.DataFrame(new_data)
sissue:
现在,当我使用此绘制时,即使某些父级类别没有相同数量的子类别,它也会为每个父类别创建相同高度的方面。因此,这在图中创建了一个不必要的空白空间行,以使孩子类别较少。我希望的是并且无法做到:如果子类别较小,则没有空白行间距/分类段。
表示任何帮助或建议:
下面显示的示例图:没有较少子类别的类别中没有空白。
来自link
一个选项是切换到
import plotnine as p9
from plotnine import *
(ggplot(new_data, aes(x="date", y="child_category", group="child_category")) +
geom_line(size=1, color="pink") +
geom_point(size=3, color="grey") +
facet_wrap("parent_category", ncol=1, scales="free_y") +
theme_538() +
theme(axis_text_x=element_text(angle=45, hjust=1),
panel_grid_major=element_blank(),
figure_size=(8, 6))
)
facet
facet_grid
允许使用space="free_y"
ggforce::facet_col
)。)。
space="free_y"