import plotly.graph_objects as go
# Node positions
nodes = {
'O': (-1, -1.3),
'A': (0, 0),
'B': (0, -1.8),
'C': (0.5, -4),
}
# Undirected edges with weights (duplicates removed)
edges = [
('O', 'A', 4),
('O', 'B', 6),
('O', 'C', 20),
('A', 'B', 3),
('A', 'C', 17),
('B', 'C', 15),
]
fig = go.Figure()
# Draw edges
for src, dst, weight in edges:
x0, y0 = nodes[src]
x1, y1 = nodes[dst]
fig.add_trace(go.Scatter(
x=[x0, x1], y=[y0, y1],
mode='lines',
line=dict(width=2, color='#636efa'),
hoverinfo='none',
showlegend=False
))
# Weight label at midpoint
fig.add_annotation(
x=(x0 + x1) / 2, y=(y0 + y1) / 2,
text=f"<b>{weight}</b>",
showarrow=False,
font=dict(size=12, color='#ff7f0e'),
bgcolor='white',
bordercolor='#ff7f0e',
borderwidth=1,
borderpad=3,
)
# Draw nodes
fig.add_trace(go.Scatter(
x=[pos[0] for pos in nodes.values()],
y=[pos[1] for pos in nodes.values()],
mode='markers+text',
marker=dict(size=40, color='#1f77b4', line=dict(width=2, color='white')),
text=list(nodes.keys()),
textfont=dict(size=16, color='white', family='Arial Black'),
textposition='middle center',
hoverinfo='text',
hovertext=list(nodes.keys()),
showlegend=False
))
fig.update_layout(
title=dict(text='Gráfico esquemático (Valores em euros)', font=dict(size=20)),
xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
yaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
plot_bgcolor='#f9f9f9',
paper_bgcolor='white',
margin=dict(l=40, r=40, t=60, b=40),
width=750,
height=550,
)
fig.show()