Plotly中轴标题‘Boundary [0,1]’与‘Solution Profile’字号无法增大求助
Plotly轴标题字号设置不生效的解决方案
你尝试通过titlefont参数设置X轴标题“Boundary [0,1]”和Y轴标题“Solution Profile”的字号,但始终无法生效,使用的代码如下:
layout = go.Layout( title={'text': f'cbits={cbits}, f = {f:.2e}, d = {d}', 'x': 0.5,'y': 0.95, 'xanchor': 'center', 'yanchor': 'top', 'font': {'size': 24}}, xaxis=dict(title='Boundary [0,1]', showgrid= True, gridcolor= 'lightgrey', titlefont= {'size': 24}, tickfont ={'size': 24} ), yaxis=dict(title='Solution Profile', showgrid= True, gridcolor='lightgrey', titlefont={'size': 24}, tickfont = {'size': 24} ), width=650, height=500, margin=dict(l=50, r=50, b=50, t=50), legend = dict( font = dict(size=24), orientation ="h", x =0.5, y = -0.1 , xanchor = "center", yanchor ="top" ), plot_bgcolor = 'white', shapes=[ # Add a rectangular border around the plot { 'type': 'rect', 'xref': 'paper', 'yref': 'paper', 'x0': 0, 'y0': 0, 'x1': 1, 'y1': 1, 'line': { 'color': 'darkgray', 'width': 1, }, 'fillcolor': 'rgba(0,0,0,0)', } ] )
解决方法
问题根源在于轴标题的定义方式:当直接用字符串设置title时,单独的titlefont参数无法被Plotly正确解析。需要将title改为字典格式,在字典内同时指定文本内容和字体属性。
修正后的轴设置代码如下:
xaxis=dict( title={'text': 'Boundary [0,1]', 'font': {'size': 24}}, showgrid=True, gridcolor='lightgrey', tickfont={'size': 24} ), yaxis=dict( title={'text': 'Solution Profile', 'font': {'size': 24}}, showgrid=True, gridcolor='lightgrey', tickfont={'size': 24} ),
替换原代码中对应的xaxis和yaxis部分后,轴标题的字号设置即可正常生效。此外,建议检查Plotly版本,确保使用最新稳定版,避免版本兼容问题导致参数失效。
内容的提问来源于stack exchange,提问作者Manu
相关产品推荐
相关产品推荐

