You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python复刻R中facet_grid生成的表格可视化效果?

复刻R中facet_grid可视化的Python方案需求

我尝试用Python的plotnine库复刻R中基于facet_grid(结合CBRegion与CBIndustry变量)实现的表格可视化,R端已生成理想效果,但plotnine输出效果存在以下问题:

  • y轴标签出现重叠
  • 仅含单个类别的面板(如Europe、Community Groups)占用了与多类别面板相同的不必要空间,调整宽高比无法解决

我的plotnine代码

myplot = ggplot(data = df_data_bar) + aes(x = "CCR100PDMid %" ,y = "CBSector")+ \
    geom_segment(aes(yend="CBSector", xend=0), colour="black", size = 2) +\
    geom_text(aes(label = "label")) + \
    theme(panel_grid_major_y = element_blank()) + \
    facet_grid('CBIndustry ~ CBRegion',scales="free_y",space="free") + \
    labs(x="", y = "", title=title) + \
    theme_bw() + \
    theme(plot_title = element_text(linespacing=0.8, face="bold", size=20, va="center"), 
        axis_text_x = element_text(colour="#333333",size=12,rotation=0,ha="center",va="top",face="bold"), 
        axis_text_y = element_text(colour="#333333",size=12,rotation=0,ha="right",va="center",face="bold"), 
        axis_title_x = element_blank(), 
        axis_title_y = element_blank(),
        legend_position="none", 
        strip_text_x = element_text(size = 12, face="bold", colour = "black", angle = 0), 
        strip_text_y = element_text(size = 8, face="bold", colour = "black", angle = 0, ha = "left"),
        strip_background_y = element_text(width = 0.2),
        figure_size=(30,20))

数据集前10行

{'CBRegion': {0: 'Europe', 1: 'Europe', 2: 'Europe', 3: 'Europe', 4: 'Europe', 5: 'Europe', 6: 'Europe', 7: 'Europe', 8: 'Europe', 9: 'Europe'}, 'CBSector': {0: 'Aerospace & Defense', 1: 'Alternative Energy', 2: 'Automobiles & Parts', 3: 'Banks', 4: 'Beverages', 5: 'Chemicals', 6: 'Colleges & Universities', 7: 'Community Groups', 8: 'Construction & Materials', 9: 'Electricity'}, 'CBIndustry': {0: 'Industrials', 1: 'Oil & Gas', 2: 'Consumer Goods', 3: 'Financials', 4: 'Consumer Goods', 5: 'Basic Materials', 6: 'NPO', 7: 'Community Groups', 8: 'Industrials', 9: 'Utilities'}, 'CCR100PDMid': {0: 0.015545818181818181, 1: 0.003296, 2: 0.012897471223021583, 3: 0.008079544600938968, 4: 0.008716597402597401, 5: 0.0094617476340694, 6: 0.008897475862068967, 7: 0.000821, 8: 0.012205547455295736, 9: 0.0050264210526315784}, 'CCR100PDMid %': {0: 1.554581818181818, 1: 0.3296, 2: 1.2897471223021584, 3: 0.8079544600938968, 4: 0.8716597402597401, 5: 0.9461747634069401, 6: 0.8897475862068966, 7: 0.0821, 8: 1.2205547455295735, 9: 0.5026421052631579}, 'label': {0: '1.6%', 1: '0.3%', 2: '1.3%', 3: '0.8%', 4: '0.9%', 5: '0.9%', 6: '0.9%', 7: '0.1%', 8: '1.2%', 9: '0.5%'}}

需求

希望得到能复刻R中可视化效果的方案,不局限于plotnine,也可使用其他Python可视化工具。

内容的提问来源于stack exchange,提问作者Carpe Diem

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 03:31:06