Can I make an arrow disappear when I click on the legend of the marker using Plotly?
Question:
import plotly.graph_objects as go
fig = go.Figure()
x = np.array([0,1,2,3])
x_s = np.array([0,1,2,3])
y = np.array([1,1,2,3])
y_s = np.array([0,0,0,0])
fig.add_trace(go.Scatter(
x=x_s,
y=y_s,
name='Test',
marker=dict(
size=10,
cmax=1,
cmin=0,
color='black',
colorscale="Rainbow_r",
symbol='circle'
# line=dict(color=line_colors, width=3)
),
showlegend=True,
hoverinfo='text',
hoverlabel=dict(bgcolor="white", font_color="black"),
mode='markers',
textposition="top center"))
fig.add_annotation(
x=0,
y=1,
ax=0,
ay=0,
xref='x',
yref='y',
axref='x',
ayref='y',
text='xxxx',
showarrow=True,
arrowhead=3,
arrowsize=1,
arrowwidth=1,
arrowcolor='black'
)
This code produces the following chart:
Is it possible to make it so that when you click on the marker’s legend, the arrow disappears along with it?
Read https://plotly.com/python/text-and-annotations/ and https://plotly.com/python/legend/ .
Answers:
This can be achieved by setting the arrow and text annotations in the same legend group. The group name is to have the scatter, arrow, and annotation in the same group name.
import plotly.graph_objects as go
import numpy as np
fig = go.Figure()
x = np.array([0,1,2,3])
x_s = np.array([0,1,2,3])
y = np.array([1,1,2,3])
y_s = np.array([0,0,0,0])
fig.add_trace(go.Scatter(
x=x_s,
y=y_s,
name='Test',
marker=dict(
size=10,
cmax=1,
cmin=0,
color='black',
colorscale="Rainbow_r",
symbol='circle'
# line=dict(color=line_colors, width=3)
),
showlegend=True,
hoverinfo='text',
hoverlabel=dict(bgcolor="white", font_color="black"),
mode='markers',
textposition="top center",
legendgroup='001'
))
fig.add_trace(go.Scatter(x=[0,0],
y=[0,1],
#text='xxxx',
marker=dict(
size=10,
symbol='arrow-bar-up',
angleref='previous'
),
#textposition='bottom center',
line_color='black',
legendgroup='001',
showlegend=False
))
fig.add_trace(go.Scatter(mode='text',
x=[0,0],
y=[0,0],
text='xxxx',
textposition='bottom center',
legendgroup='001',
showlegend=False
))
fig.show()
import plotly.graph_objects as go
fig = go.Figure()
x = np.array([0,1,2,3])
x_s = np.array([0,1,2,3])
y = np.array([1,1,2,3])
y_s = np.array([0,0,0,0])
fig.add_trace(go.Scatter(
x=x_s,
y=y_s,
name='Test',
marker=dict(
size=10,
cmax=1,
cmin=0,
color='black',
colorscale="Rainbow_r",
symbol='circle'
# line=dict(color=line_colors, width=3)
),
showlegend=True,
hoverinfo='text',
hoverlabel=dict(bgcolor="white", font_color="black"),
mode='markers',
textposition="top center"))
fig.add_annotation(
x=0,
y=1,
ax=0,
ay=0,
xref='x',
yref='y',
axref='x',
ayref='y',
text='xxxx',
showarrow=True,
arrowhead=3,
arrowsize=1,
arrowwidth=1,
arrowcolor='black'
)
This code produces the following chart:
Is it possible to make it so that when you click on the marker’s legend, the arrow disappears along with it?
Read https://plotly.com/python/text-and-annotations/ and https://plotly.com/python/legend/ .
This can be achieved by setting the arrow and text annotations in the same legend group. The group name is to have the scatter, arrow, and annotation in the same group name.
import plotly.graph_objects as go
import numpy as np
fig = go.Figure()
x = np.array([0,1,2,3])
x_s = np.array([0,1,2,3])
y = np.array([1,1,2,3])
y_s = np.array([0,0,0,0])
fig.add_trace(go.Scatter(
x=x_s,
y=y_s,
name='Test',
marker=dict(
size=10,
cmax=1,
cmin=0,
color='black',
colorscale="Rainbow_r",
symbol='circle'
# line=dict(color=line_colors, width=3)
),
showlegend=True,
hoverinfo='text',
hoverlabel=dict(bgcolor="white", font_color="black"),
mode='markers',
textposition="top center",
legendgroup='001'
))
fig.add_trace(go.Scatter(x=[0,0],
y=[0,1],
#text='xxxx',
marker=dict(
size=10,
symbol='arrow-bar-up',
angleref='previous'
),
#textposition='bottom center',
line_color='black',
legendgroup='001',
showlegend=False
))
fig.add_trace(go.Scatter(mode='text',
x=[0,0],
y=[0,0],
text='xxxx',
textposition='bottom center',
legendgroup='001',
showlegend=False
))
fig.show()