如何在 Plotly-Python 的图中定位图例?
在本文中,我们将学习如何使用 Plotly Express 和 Plotly 隐藏图例。图例是描述图形元素的区域。在情节图例中,用于在轴上放置图例。
示例 1:
在这个例子中,我们在 fig.update_layout() 方法的帮助下将图例定位在图中,通过将位置传递为x=0.3 和 y=0.1 。
Python3
# importing packages
import plotly.graph_objects as go
# using medals_wide dataset
fig = go.Figure()
# plotting the scatter chart
fig.add_trace(go.Scatter(
x=[1, 2, 3, 4, 5],
y=[1, 2, 3, 4, 5],
))
# plotting the scatter chart
fig.add_trace(go.Scatter(
x=[1, 2, 3, 4, 5],
y=[5, 4, 3, 2, 1],
))
# position legends inside a plot
fig.update_layout(
legend=dict(
x=0.3, # value must be between 0 to 1.
y=.1, # value must be between 0 to 1.
traceorder="normal",
font=dict(
family="sans-serif",
size=12,
color="black"
),
)
)
fig.show()
Python3
# importing packages
import plotly.graph_objects as go
# using medals_wide dataset
fig = go.Figure()
# plotting the scatter chart
fig.add_trace(go.Scatter(
x=[1, 2, 3, 4, 5],
y=[1, 2, 3, 4, 5],
))
# plotting the scatter chart
fig.add_trace(go.Scatter(
x=[1, 2, 3, 4, 5],
y=[5, 4, 3, 2, 1],
visible='legendonly'
))
# position legends inside a plot
fig.update_layout(
legend=dict(
x=0.6, # value must be between 0 to 1.
y=1, # value must be between 0 to 1.
traceorder="normal",
font=dict(
family="sans-serif",
size=12,
color="black"
),
)
)
fig.show()
Python3
import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Scatter(
x=[1, 2, 3, 4, 5],
y=[1, 2, 3, 4, 5],
mode='markers',
marker={'size': 10}
))
# plotting the scatter chart
fig.add_trace(go.Scatter(
x=[1, 2, 3, 4, 5],
y=[5, 4, 3, 2, 1],
mode='markers',
marker={'size': 100}
))
# position legends inside a plot
fig.update_layout(
legend=dict(
x=.9, # value must be between 0 to 1.
y=1, # value must be between 0 to 1.
traceorder="normal",
font=dict(
family="sans-serif",
size=12,
color="black"
),
)
)
fig.update_layout(legend={'itemsizing': 'constant'})
fig.show()
输出:
示例 2:
在这个例子中,我们在fig.update_layout()方法的帮助下将图例定位在图中,通过将位置传递为x=0.6 和 y=1 。
Python3
# importing packages
import plotly.graph_objects as go
# using medals_wide dataset
fig = go.Figure()
# plotting the scatter chart
fig.add_trace(go.Scatter(
x=[1, 2, 3, 4, 5],
y=[1, 2, 3, 4, 5],
))
# plotting the scatter chart
fig.add_trace(go.Scatter(
x=[1, 2, 3, 4, 5],
y=[5, 4, 3, 2, 1],
visible='legendonly'
))
# position legends inside a plot
fig.update_layout(
legend=dict(
x=0.6, # value must be between 0 to 1.
y=1, # value must be between 0 to 1.
traceorder="normal",
font=dict(
family="sans-serif",
size=12,
color="black"
),
)
)
fig.show()
输出:
示例 3:
在这个例子中,我们在 fig.update_layout() 方法的帮助下将图例定位在图中,通过将位置传递为x=0.9 和 y=1 。
Python3
import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Scatter(
x=[1, 2, 3, 4, 5],
y=[1, 2, 3, 4, 5],
mode='markers',
marker={'size': 10}
))
# plotting the scatter chart
fig.add_trace(go.Scatter(
x=[1, 2, 3, 4, 5],
y=[5, 4, 3, 2, 1],
mode='markers',
marker={'size': 100}
))
# position legends inside a plot
fig.update_layout(
legend=dict(
x=.9, # value must be between 0 to 1.
y=1, # value must be between 0 to 1.
traceorder="normal",
font=dict(
family="sans-serif",
size=12,
color="black"
),
)
)
fig.update_layout(legend={'itemsizing': 'constant'})
fig.show()
输出: