python - 在 Plotly 3.0 中设置图例文本颜色

标签 python plotly plotly.js

我刚刚安装了最新的 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的属性包括capitalise、isdigit类方法等。

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

非常感谢任何帮助。

谢谢。

Legend text unchanged.

最佳答案

因为您使用的是 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")

enter image description here

关于python - 在 Plotly 3.0 中设置图例文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51210306/

相关文章:

python - 'type'对象不可下标错误

python - Dash-Plotly 应用程序身份验证 - 登录详细信息保存为 Cookie/Heroku 托管 'Type' 对象错误

python - “系列”对象没有属性 'iplot'

python - 如何叠加不同日期的图?

javascript - 如何减少 plotly js 中标题和圆环图之间的空间?

python - Django中的YouTube搜索-> manage.py:错误:无法识别的参数:shell

python - 在PyQt5中用于显示图像的类

python - Plotly 图表的下拉菜单

python - 使绘图中的 X 轴更宽,Y 轴更窄

javascript - 如何在 plotly.js 中隐藏/删除 slider 刻度/步骤及其标签?