剧情传奇标题
Posted
技术标签:
【中文标题】剧情传奇标题【英文标题】:Plotly legend title 【发布时间】:2018-01-15 06:04:24 【问题描述】:我希望能够在以下代码中为图例添加标题。不过看了docs,我觉得没有办法解决这个问题。
import plotly.plotly as py
import plotly.graph_objs as go
trace0 = go.Scatter(
x=[1, 2, 3, 4, 5],
y=[1, 2, 3, 4, 5],
)
trace1 = go.Scatter(
x=[1, 2, 3, 4, 5],
y=[5, 4, 3, 2, 1],
)
data = [trace0, trace1]
fig = go.Figure(data=data)
py.iplot(fig, filename='default-legend')
【问题讨论】:
【参考方案1】:我以前通过制作无数据跟踪来做到这一点
import plotly.plotly as py
import plotly.graph_objs as go
dummy_trace = go.Scatter(
x=[None], y=[None],
name='<b>Legend Heading</b>',
# set opacity = 0
line='color': 'rgba(0, 0, 0, 0)'
)
trace0 = go.Scatter(
x=[1, 2, 3, 4, 5],
y=[1, 2, 3, 4, 5],
)
trace1 = go.Scatter(
x=[1, 2, 3, 4, 5],
y=[5, 4, 3, 2, 1],
)
data = [dummy_trace, trace0, trace1]
fig = go.Figure(data=data)
py.iplot(fig)
【讨论】:
【参考方案2】:更新:
如果不定义图例但具有注释定位属性,请使用以下代码。
import plotly.offline as py_offline
import plotly.graph_objs as go
py_offline.init_notebook_mode()
trace0 = go.Scatter(
x=[1, 2, 3, 4, 5],
y=[1, 2, 3, 4, 5],
)
trace1 = go.Scatter(
x=[1, 2, 3, 4, 5],
y=[5, 4, 3, 2, 1],
)
data = [trace0, trace1]
layout = go.Layout(
annotations=[
dict(
x=1.12,
y=1.05,
align="right",
valign="top",
text='Legend Title',
showarrow=False,
xref="paper",
yref="paper",
xanchor="center",
yanchor="top"
)
]
)
fig = go.Figure(data=data, layout = layout)
py_offline.iplot(fig)
注意事项:
您需要使用此方法为注释定义x
和y
位置,用于不同的图例。
您可以在text
属性中使用html(例如:text='Legend Title<br>kinda lengthy',
)
之前的尝试:
另一种方法是创建图例并使用注释将标题添加到图例中。前提是您不在可编辑模式下使用图表。所以在下面的例子中,图例设置为 x=0 和 y=1,因为我希望我的图例标题高于我的实际图例,所以我将注释位置设置为 x = 0,y = 1.5。 x-ref 和 y-ref 需要设置为纸张。这将给出一个很好的注释,例如
代码:
import plotly.plotly as py
import plotly.graph_objs as go
trace0 = go.Scatter(
x=[1, 2, 3, 4, 5],
y=[1, 2, 3, 4, 5],
)
trace1 = go.Scatter(
x=[1, 2, 3, 4, 5],
y=[5, 4, 3, 2, 1],
)
data = [trace0, trace1]
layout = go.Layout(
legend=dict(
x=0,
y=1,
traceorder='normal',
font=dict(
family='sans-serif',
size=12,
color='#000'
),
bgcolor='#E2E2E2',
bordercolor='#FFFFFF',
borderwidth=2
),
annotations=[
dict(
x=0,
y=1.05,
xref='paper',
yref='paper',
text='Legend Title',
showarrow=False
)
]
)
fig = go.Figure(data=data, layout = layout)
py.iplot(fig)
【讨论】:
@blueprince13 这个问题可以结束吗? 您能否建议在不专门设置图例的情况下如何做到这一点?我希望图例出现在正常位置。 @blueprince13 如果其他解决方案不能满足您的问题,请查看我的更新答案!【参考方案3】:只需在已经提出的解决方案中稍微添加name
属性,
import plotly
import plotly.plotly as py
import plotly.graph_objs as go
plotly.offline.init_notebook_mode(connected=True)
trace0 = go.Scatter(
x=[1, 2, 3, 4, 5],
y=[1, 2, 3, 4, 5],
name="Data1")
data = [trace0]
layout = go.Layout(
legend=dict(
x=0,
y=1,
traceorder='normal',
font=dict(
family='sans-serif',
size=12,
color='#000'
),
bgcolor='#E2E2E1',
bordercolor='#FFFFFF',
borderwidth=2
),
annotations=[
dict(
x=0,
y=1.05,
xref='paper',
yref='paper',
text='Legend Title',
showarrow=False
)
])
fig = go.Figure(data=data, layout = layout)
plotly.offline.iplot(fig)
name
属性有助于将自定义名称添加到定义的图例中。
【讨论】:
【参考方案4】:很容易通过包装袖扣与 jupyter 进行绘图。
安装这些依赖项:
!pip install pandas cufflinks plotly
根据您的数据创建数据框。
import pandas as pd
加载数据
x=[1, 2, 3, 4, 5]
y=[1, 2, 3, 4, 5]
将第二个 y 列表添加到 y1
x=[1, 2, 3, 4, 5]
y1=[5, 4, 3, 2, 1]
从您的数据列表中创建数据框
data = pd.DataFrame("x": x, "y": y, "y1": y1).set_index("x")
加载袖扣及其配置
import cufflinks as cf
cf.set_config_file(offline=True)
绘制数据框
data.iplot()
您将 x 在轴上,y,y1 在 y 轴上。您可以显示和隐藏从右侧单击图例的线。
参考文献:
https://plotly.com/javascript/configuration-options/【讨论】:
【参考方案5】:从 plotly v4.5 开始,添加了图例标题。
您可以通过以下方式为图例添加标题
fig.update_layout(legend_title_text='Legend title')
在文档here 中查找示例。
【讨论】:
【参考方案6】:离散数据的另一个(现在可能没有实际意义)选项是使用数据框的列标题之一作为图例标题。下面是一个由三个 (Geo)DataFrames 组成的等值线图示例。
df1['Legend'] = 'Label1'
df2['Legend'] = 'Label2'
df3['Legend'] = 'Label3'
merged = df1.append(df2)
merged = df2.append(df3)
fig = px.choropleth(merged, geojson=merged.geometry, locations=merged.index, color=merged['Legend'], color_discrete_map=
'Label1':'red',
'Label2':'lightgrey',
'Label3':'lightblue')
这里是一个使用 mapbox choropleth 的示例
【讨论】:
以上是关于剧情传奇标题的主要内容,如果未能解决你的问题,请参考以下文章