Hi,
I’m trying to create a shape to set a label like this :
import plotly.graph_objects as go
import plotly.express as px
import pandas as pd
margin_factor = 1.6
data = {'x': [1.5, 1.6, -1.2],
'y': [21, -16, 46],
'circle-size': [10, 5, 6],
'circle-color': ["red","red","green"],
'tttt': ["PO%. with test cool","PI%. with test dom","HI%. with AQ"],
}
# Create DataFrame
df = pd.DataFrame(data)
fig = px.scatter(
df,
x="x",
y="y",
color="circle-color",
size='circle-size'
)
fig.update_layout(
{
'xaxis': {
"range": [-100, 100],
'zerolinewidth': 3,
"zerolinecolor": "blue",
"tick0": -100,
"dtick": 25,
'scaleanchor': 'y'
},
'yaxis': {
"range": [-100, 100],
'zerolinewidth': 3,
"zerolinecolor": "green",
"tick0": -100,
"dtick": 25
},
"width": 500,
"height": 500
}
)
# x_pad = (max(df.x) - min(df.x)) / 8
# y_pad = (max(df.y) - min(df.y)) / 30
x_pad = 100
y_pad = 6
for x0, y0 in zip(data['x'], data['y']):
fig.add_shape(type="rect",
x0=x0 + (x_pad)/100,
y0=y0 - y_pad,
x1=x0 + x_pad,
y1=y0 + y_pad,
xref='x', yref='y',
line=dict(
color="yellow",
width=2,
),
fillcolor="yellow",
layer="below"
)
fig.add_trace(
go.Scatter(
x=df["x"].values + (x_pad)/2,
y=df["y"],
text=df["tttt"],
mode="text",
showlegend=False
)
)
fig.update_traces(textposition='middle right', textfont_size=14, textfont_color='black', textfont_family="Inter", hoverinfo="skip", hovertemplate=None, hoverlabel_bgcolor="white", hoverlabel_font_color="black", hoverlabel_font_family="Inter", hoverlabel_font_size=12)
fig.show()
the result :
but what I’m looking for is something ike this :
matbe I need to recalculate the x_pad and y_pad , .does anyme dis that before please ?
thanks