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

Plotly绘制含65个X轴标签的柱状图,标签显示不全如何解决?

解决Plotly柱状图X轴标签显示不全问题

针对X轴标签过多导致重叠、显示不清晰的问题,可通过以下几种方式调整:

方法1:旋转X轴标签并调整底部边距

将X轴标签倾斜一定角度,同时增大底部边距避免标签被截断,是最常用的解决方式:

library(plotly)
library(data.table)
df1<-structure(list(code = c("01", "02", "03", "10-12", 
                              "13-15", "16", "17", "18", "19", "20", "21", "22", "23", "24", 
                              "25", "26", "27", "28", "29", "30", "31-32", "33", "35", "36", 
                              "37-39", "45", "46", "47", "49", "50", "51", "52", "53", "58", 
                              "59-60", "61", "62-63", "64", "65", "66", "68А", "68B", 
                              "69-70", "71", "72", "73", "74-75", "77", "78", "79", "80-82", 
                              "84", "85", "86", "87-88", "90-92", "93", "94", "95", "96", "B", 
                              "F", "I", "T"), rate = c(0.18, 0.18, 0.18, 0.25, 
                                                        0.18, 0.12, 0.11, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 
                                                        0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 
                                                        0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 
                                                        0.18, 0.13, 0.13, 0, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 
                                                        0.18, 0.18, 0.14, 0.15, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 
                                                        0.18, 0.18, 0.18, 0.15, 0.18)), row.names = c(NA, -64L), class = "data.frame")

df1<-data.frame(df1)

plt <- plot_ly(df1) %>% 
  add_trace(x = ~code, y = ~rate, type = 'bar', name = 'Test') %>% 
  layout(
    xaxis = list(
      title = '',
      tickangle = -45,  # 标签向左倾斜45度
      tickfont = list(size = 10),  # 可选:微调标签字体大小
      automargin = TRUE  # 自动调整边距避免截断
    ), 
    yaxis = list(title = ''),
    legend = list(x = 0.9, y = 0.99),
    barmode = 'group',
    margin = list(b = 80)  # 增大底部边距,单位为像素
  )

plt

方法2:缩小X轴标签字体尺寸

如果不想旋转标签,可直接减小标签字体大小,让更多标签能在一行显示:

plt <- plot_ly(df1) %>% 
  add_trace(x = ~code, y = ~rate, type = 'bar', name = 'Test') %>% 
  layout(
    xaxis = list(
      title = '',
      tickfont = list(size = 8)  # 缩小字体到8号
    ), 
    yaxis = list(title = ''),
    legend = list(x = 0.9, y = 0.99),
    barmode = 'group'
  )

plt

方法3:启用X轴标签自动换行

对于较长的标签,可设置自动换行,结合旋转效果更佳:

plt <- plot_ly(df1) %>% 
  add_trace(x = ~code, y = ~rate, type = 'bar', name = 'Test') %>% 
  layout(
    xaxis = list(
      title = '',
      tickangle = -45,
      tickfont = list(size = 10),
      tickformat = '<br>'  # 自动拆分较长标签换行显示
    ), 
    yaxis = list(title = ''),
    legend = list(x = 0.9, y = 0.99),
    barmode = 'group',
    margin = list(b = 100)
  )

plt

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 00:25:31