如何设置绘图的背景颜色和网格线的颜色?

这是我的代码:


import plotly.graph_objects as go


fig = go.Figure()

fig.add_trace(go.Bar(

    name='Group 1',

    x=['Var 1', 'Var 2', 'Var 3'], y=[3, 6, 4],

    error_y=dict(type='data', array=[1, 0.5, 1.5]),

    width=0.15

))

fig.add_trace(go.Bar(

    name='Group 2',

    x=['Var 1', 'Var 2', 'Var 3'], y=[4, 7, 3],

    error_y=dict(type='data', array=[0.5, 1, 2]),

    width=0.15

))

fig.update_layout(barmode='group')

fig.show()

输出:

http://img2.mukewang.com/6412d36d0001388406560323.jpg

我读过这篇文章,但来自的代码似乎已贬值并且不起作用。

问题:如何设置图的背景色和网格线的颜色?


jeck猫
浏览 159回答 1
1回答

慕神8447489

这应该有帮助import plotly.graph_objects as gofrom plotly.graph_objects import Layout# Set layout with background color you want (rgba values)# This one is for white backgroundlayout = Layout(plot_bgcolor='rgba(0,0,0,0)')# Use that layout herefig = go.Figure(layout=layout)fig.add_trace(go.Bar(    name='Group 1',    x=['Var 1', 'Var 2', 'Var 3'], y=[3, 6, 4],    error_y=dict(type='data', array=[1, 0.5, 1.5]),    width=0.15))fig.add_trace(go.Bar(    name='Group 2',    x=['Var 1', 'Var 2', 'Var 3'], y=[4, 7, 3],    error_y=dict(type='data', array=[0.5, 1, 2]),    width=0.15))fig.update_layout(barmode='group')# Change grid color and axis colorsfig.update_xaxes(showline=True, linewidth=2, linecolor='black', gridcolor='Red')fig.update_yaxes(showline=True, linewidth=2, linecolor='black', gridcolor='Red')fig.show()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python