【问题标题】:Setting legend text colour in Plotly 3.0在 Plotly 3.0 中设置图例文本颜色
【发布时间】:2018-12-15 01:22:48
【问题描述】:

我刚刚安装了最新的 Plotly (3.0),但无法设置图例文本颜色。

这是我的代码:

import plotly.graph_objs as go
import numpy as np

x = np.random.randn(1000)
y = np.random.randn(1000)

fig = go.FigureWidget({'x':x,'y':y,'type':'histogram2dcontour','colorscale':'Viridis'}],
layout=go.Layout(title='test',width=700,plot_bgcolor='rgba(0,0,0,0)',
paper_bgcolor='rgba(0,0,0,0)'))

fig.layout.titlefont.color = 'orange'
fig.layout.xaxis.color = 'white'
fig.layout.yaxis.color = 'white'
fig.layout.legend.font.size = 2000
fig.layout.legend.font.color = 'red'

fig

如下所示,下面的图例文本保持不变。奇怪的是 fig.layout.legend.font.color 的属性包括大写、isdigit 类方法等。

这是一个错误还是我遗漏了什么?

非常感谢任何帮助。

谢谢。

【问题讨论】:

    标签: python plotly plotly.js


    【解决方案1】:

    因为您使用的是histogram2contour,所以右侧的颜色条不是图例,实际上是一个名为colorbar 的对象。要更新它,您可以在跟踪中配置它的属性。我在下面有一个示例,我将刻度标记设为橙色,标题设为红色。我使用 Jupyter Notebooks 来创建示例,因此我必须将其配置为离线,但您也没有。 Here 是彩条对象的文档。

     import plotly.graph_objs as go
     from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
     init_notebook_mode(connected=True)
    
     import numpy as np
    
     x = np.random.uniform(-1, 1, size=500)
     y = np.random.uniform(-1, 1, size=500)
    
     trace = [go.Histogram2dContour(
             x = x,
             y = y,
         colorbar=dict(
             title='Colorbar',
             tickfont={'color':'#E90' },
             titlefont={"color":'#FF0000'}
         ),
     )]
    
     iplot(trace, filename = "Basic Histogram2dContour")
    

    【讨论】:

    • 谢谢,太好了。没有意识到色阶是图例的一个单独属性。非常感谢!
    猜你喜欢
    • 2021-12-26
    • 1970-01-01
    • 2020-10-06
    • 1970-01-01
    • 2015-07-10
    • 1970-01-01
    • 1970-01-01
    • 2020-12-04
    • 2021-02-22
    相关资源
    最近更新 更多