Home Forums Nazca how to get the matplolib figure? Reply To: how to get the matplolib figure?

#6159
Ronald
Keymaster

Dear Bastien,

The cell iterator would indeed be a good way to get access all the layout info.

I am not familiar with plotly, but I noticed it has to run in a browser to show the plot (JavaScript output) and it needs an offline mode if not run through a plotly account. I adjusted the code a bit to make it run for me in a notebook (see code below).

More generic in Nazca, the layout to the various formats (gds, svg, png) receives input from the iterator in layout.py. Full Nazca-plotly integration would need a small class with methods for fig open, plot shapes for polygons, polylines and annotations, and a figure close/show, similar to e.g. class ClsMatplotlib.

import nazca as nd
from plotly.offline import plot
import plotly.graph_objects as go
nd.clear_layout()

a = nd.strt(width=5, layer=1).put()
b = nd.bend( width=5, layer=1).put()

X, Y, lay = [], [], []
for NT in nd.cell_iter(nd.cfg.defaultcell, flat=True):
    if NT.cell_start:
        print(f"cell: '{NT.cell.cell_name}'")
        for k, (pgon, points, bbox) in enumerate(NT.iters['polygon']):
            X.append([xy[0] for xy in points])
            Y.append([xy[1] for xy in points])
            lay.append(nd.get_layer_tuple(pgon.layer)[0]) # tuple: (layer, datatype, technology)
  
fig = go.Figure()

for x, y, l in zip(X, Y, lay):
    fig.add_trace(
        go.Scatter(x=x, y=y, fill="toself", marker=dict(opacity=0), fillcolor='red'),
    )

fig.add_annotation(
    x=5,
    y=2.5,
    text="L",
    ax=0,
    ay=-40
)

fig.add_annotation(
    x=0,
    y=0,
    text="W",
    ax=-20,
    ay=-40
)

fig.update_annotations(dict(
    xref="x",
    yref="y",
    showarrow=True,
    arrowhead=7
))

fig.update_layout(
    yaxis = dict(
        scaleanchor = "x",
        scaleratio = 1,
    ),
    showlegend = True,
    width=1000, 
    height=1000,
    autosize=False
)

plot(fig)

Ronald