Plotly图表坐标轴仅显示整数配置问题求助
解决Plotly子图坐标轴只显示整数刻度的问题
嘿,我懂你的困扰!你已经尝试用xaxis_tickformat=',d'让坐标轴只显示整数,但这个设置只在第一个图表生效,底部的第二个图表还是冒出了.5这类小数刻度对吧?
问题出在多子图的轴标识逻辑上:当你用Plotly做垂直排列的子图(比如上下两个图表)时,每个坐标轴都有独立的ID——第一个图表的x轴是xaxis,底部第二个图表的x轴是xaxis2。你之前只设置了xaxis_tickformat,自然不会影响到第二个轴。
这里有两种简单直接的解决方法:
方法1:在update_layout里直接指定第二个x轴的格式
修改你的update_layout参数,加上xaxis2_tickformat=',d',明确针对底部图表的x轴设置规则:
def create_chart(): # Put data together into an interactive chart fig.update_layout( height=500, width=800, yaxis_tickprefix='$', hovermode='x unified', xaxis_tickformat=',d', # 顶部图表的x轴整数格式 xaxis2_tickformat=',d', # 底部图表的x轴整数格式 template=symbol_template, separators=".", title_text=(df.columns[DATA_COL_1]) + " & Units 2015-2019" )
方法2:用update_xaxes单独指定目标子图的轴
如果你的子图是按行排列的(比如rows=2, cols=1),用update_xaxes并指定row=2会更直观,尤其是子图结构复杂时:
def create_chart(): # Put data together into an interactive chart fig.update_layout( height=500, width=800, yaxis_tickprefix='$', hovermode='x unified', template=symbol_template, separators=".", title_text=(df.columns[DATA_COL_1]) + " & Units 2015-2019" ) # 单独给第二行(底部)的x轴设置整数刻度 fig.update_xaxes(tickformat=',d', row=2, col=1)
这两种方法都能让底部图表的x轴只显示整数刻度。如果你的子图不是垂直排列的,只需要对应调整轴的ID(比如xaxis3对应第三列)或者row/col参数就行。
内容的提问来源于stack exchange,提问作者Mulcch
相关产品推荐
相关产品推荐

