Plotly:如何检查和更改 plotly 图形?
Plotly: How to inspect and make changes to a plotly figure?
相关问题已经被问过,例如
但这些问题的答案受到以下事实的限制,即并非所有参数都可以通过 Python 获得,这意味着真正的答案隐藏在 JavaScript 的某个地方。但是对于较新版本的 plotly,您如何检查和编辑 plotly 图形?例如,你怎么能找出图形的背景颜色是什么?然后再改?从上面第二个link可以看出,fig.show
和print(fig)
会揭示图形结构的一些细节。但肯定不是全部。下面的代码片段将生成以下图:
剧情:
代码:
import plotly.graph_objects as go
import plotly.express as px
df = px.data.gapminder().query("country=='Canada'")
fig = px.line(df, x="year", y="lifeExp", title='Life expectancy in Canada')
fig.show()
运行 fig.show
现在将以字典的形式部分显示图形的结构:
<bound method BaseFigure.show of Figure({
'data': [{'hovertemplate': 'year=%{x}<br>lifeExp=%{y}<extra></extra>',
'legendgroup': '',
'line': {'color': '#636efa', 'dash': 'solid'},
'mode': 'lines',
'name': '',
'orientation': 'v',
'showlegend': False,
'type': 'scatter',
'x': array([1952, 1957, 1962, 1967, 1972, 1977, 1982, 1987, 1992, 1997, 2002, 2007],
dtype=int64),
'xaxis': 'x',
'y': array([68.75 , 69.96 , 71.3 , 72.13 , 72.88 , 74.21 , 75.76 , 76.86 , 77.95 ,
78.61 , 79.77 , 80.653]),
'yaxis': 'y'}],
'layout': {'legend': {'tracegroupgap': 0},
'template': '...',
'title': {'text': 'Life expectancy in Canada'},
'xaxis': {'anchor': 'y', 'domain': [0.0, 1.0], 'title': {'text': 'year'}},
'yaxis': {'anchor': 'x', 'domain': [0.0, 1.0], 'title': {'text': 'lifeExp'}}}
})
但正如您自己所见,缺少了很多细节。那么如何进行更完整的图形自省呢?
从 4.10 版开始,plotly 开发人员引入了他们谈论 here 的很棒的 fig.full_figure_for_development()
功能。在那里你会看到:
fig.full_figure_for_development()
function will return a new go.Figure
object, prepopulated with the same values you provided, as well as all
the default values computed by Plotly.js, to allow you to learn more
about what attributes control every detail of your figure and how you
can customize them. This function is named “for development” because
it’s not necessary to use it to produce figures, but it can be really
handy to explore figures while you’re figuring out how to build them.
因此,基于问题中的示例,以下代码片段将产生大约 180 行的输出,其中包含大量其他细节,以及关于图形布局的这一部分:
'margin': {'autoexpand': True, 'b': 80, 'l': 80, 'pad': 0, 'r': 80, 't': 100},
'modebar': {'activecolor': 'rgba(68, 68, 68, 0.7)',
'bgcolor': 'rgba(255, 255, 255, 0.5)',
'color': 'rgba(68, 68, 68, 0.3)',
'orientation': 'h'},
'newshape': {'drawdirection': 'diagonal',
'fillcolor': 'rgba(0,0,0,0)',
'fillrule': 'evenodd',
'layer': 'above',
'line': {'color': '#444', 'dash': 'solid', 'width': 4},
'opacity': 1},
'paper_bgcolor': 'white',
'plot_bgcolor': '#E5ECF6',
'separators': '.,',
'showlegend': False,
'spikedistance': 20,
在那里您还可以看到绘图的背景颜色为 'plot_bgcolor': '#E5ECF6'
。您可能知道可以使用 设置 背景颜色,例如 'grey'
使用 fig.update_layout(plot_bgcolor='grey')
。但现在您也知道如何得到它了:
# In:
fig.layout.plot_bgcolor
# Out:
'#E5ECF6'
知道如何执行此操作后,您就知道如何获取和设置绘图图形的几乎所有属性。如果您使用 plotly.graph_objects
或 plotly.express
构建图形并不重要
相关问题已经被问过,例如
但这些问题的答案受到以下事实的限制,即并非所有参数都可以通过 Python 获得,这意味着真正的答案隐藏在 JavaScript 的某个地方。但是对于较新版本的 plotly,您如何检查和编辑 plotly 图形?例如,你怎么能找出图形的背景颜色是什么?然后再改?从上面第二个link可以看出,fig.show
和print(fig)
会揭示图形结构的一些细节。但肯定不是全部。下面的代码片段将生成以下图:
剧情:
代码:
import plotly.graph_objects as go
import plotly.express as px
df = px.data.gapminder().query("country=='Canada'")
fig = px.line(df, x="year", y="lifeExp", title='Life expectancy in Canada')
fig.show()
运行 fig.show
现在将以字典的形式部分显示图形的结构:
<bound method BaseFigure.show of Figure({
'data': [{'hovertemplate': 'year=%{x}<br>lifeExp=%{y}<extra></extra>',
'legendgroup': '',
'line': {'color': '#636efa', 'dash': 'solid'},
'mode': 'lines',
'name': '',
'orientation': 'v',
'showlegend': False,
'type': 'scatter',
'x': array([1952, 1957, 1962, 1967, 1972, 1977, 1982, 1987, 1992, 1997, 2002, 2007],
dtype=int64),
'xaxis': 'x',
'y': array([68.75 , 69.96 , 71.3 , 72.13 , 72.88 , 74.21 , 75.76 , 76.86 , 77.95 ,
78.61 , 79.77 , 80.653]),
'yaxis': 'y'}],
'layout': {'legend': {'tracegroupgap': 0},
'template': '...',
'title': {'text': 'Life expectancy in Canada'},
'xaxis': {'anchor': 'y', 'domain': [0.0, 1.0], 'title': {'text': 'year'}},
'yaxis': {'anchor': 'x', 'domain': [0.0, 1.0], 'title': {'text': 'lifeExp'}}}
})
但正如您自己所见,缺少了很多细节。那么如何进行更完整的图形自省呢?
从 4.10 版开始,plotly 开发人员引入了他们谈论 here 的很棒的 fig.full_figure_for_development()
功能。在那里你会看到:
fig.full_figure_for_development()
function will return a newgo.Figure
object, prepopulated with the same values you provided, as well as all the default values computed by Plotly.js, to allow you to learn more about what attributes control every detail of your figure and how you can customize them. This function is named “for development” because it’s not necessary to use it to produce figures, but it can be really handy to explore figures while you’re figuring out how to build them.
因此,基于问题中的示例,以下代码片段将产生大约 180 行的输出,其中包含大量其他细节,以及关于图形布局的这一部分:
'margin': {'autoexpand': True, 'b': 80, 'l': 80, 'pad': 0, 'r': 80, 't': 100},
'modebar': {'activecolor': 'rgba(68, 68, 68, 0.7)',
'bgcolor': 'rgba(255, 255, 255, 0.5)',
'color': 'rgba(68, 68, 68, 0.3)',
'orientation': 'h'},
'newshape': {'drawdirection': 'diagonal',
'fillcolor': 'rgba(0,0,0,0)',
'fillrule': 'evenodd',
'layer': 'above',
'line': {'color': '#444', 'dash': 'solid', 'width': 4},
'opacity': 1},
'paper_bgcolor': 'white',
'plot_bgcolor': '#E5ECF6',
'separators': '.,',
'showlegend': False,
'spikedistance': 20,
在那里您还可以看到绘图的背景颜色为 'plot_bgcolor': '#E5ECF6'
。您可能知道可以使用 设置 背景颜色,例如 'grey'
使用 fig.update_layout(plot_bgcolor='grey')
。但现在您也知道如何得到它了:
# In:
fig.layout.plot_bgcolor
# Out:
'#E5ECF6'
知道如何执行此操作后,您就知道如何获取和设置绘图图形的几乎所有属性。如果您使用 plotly.graph_objects
或 plotly.express