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

plotnine中facet_grid右侧坐标轴隐藏方法咨询(保留行自由y轴)

Solution for Hiding Right-Side Y-Axes in Plotnine Facet Grid

Got it, let's fix this up for you. You're already on the right track with scales='free_y' to get independent y-axis ranges per row—we just need to adjust the theme settings to hide the right-side y-axis elements while keeping the left ones visible.

Here's the modified code that does exactly what you want:

import pandas as pd
from plotnine import *

df = pd.DataFrame({
    'A': [1,1,1,1],
    'B': [1,9,5,11],
    'C': ['x','x','y','y'],
    'D': ['a','b','a','b']
})

p = ggplot(df, aes('A', 'B'))
(
    p + geom_bar(stat="identity") 
      + facet_grid('D~C', scales='free_y')
      + theme(
          # Hide right-side y-axis tick marks
          axis_ticks_y_right=element_blank(),
          # Hide right-side y-axis text labels
          axis_text_y_right=element_blank(),
          # Hide right-side y-axis title (if present)
          axis_title_y_right=element_blank()
      )
)

Key Explanations:

  • scales='free_y': This remains because it enables each row in your facet grid to have its own y-axis scale, which aligns with your requirement of not using scales='fixed'.
  • Theme Adjustments: We're specifically targeting the right-side y-axis components and setting them to element_blank()—this removes them entirely without affecting the left-side y-axis that you want to keep.

If you also want to remove the right-side axis line (it's often not visible by default, but just in case), add this line to the theme() call:

axis_line_y_right=element_blank()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 08:32:10