- This topic has 6 replies, 2 voices, and was last updated 4 years, 8 months ago by
bastien.
-
AuthorPosts
-
22 May 2020 at 11:54 #6145
bastien
MemberHi,
I’d like to get the matplotlib figure corresponding to a layout so that I can make a plot showing my overall layout design and close-ups of some critical features in various subplots. Is there a way to do that?
Also, is there a way to use a different plotting library? I’d love to be able to use plotly in a jupyter notebook.
Thanks,
Bastien
22 May 2020 at 12:04 #6147Ronald
KeymasterDear Bastien,
For matplotlib export use
nd.export_plt()
or aliasnd.export_png()
.
Notice that after export the design will be cleared. So in the next example the second export would be empty:nd.export_gds() nd.export_plt() # will be empty
To keep the design alive you can use clear=False in the call:
nd.export_gds(clear=False) nd.export_plt() # export to png as expected
This behaviour is specifically introduced for working in Jupyter notebooks. So when you rerun a cell you do not stack design upon design by default, but if you run stuff in a sequence of cells you can set the clear=False.
Matplotlib is flexible but notoriously on the slow side. If you put a lot of frames in you color definition things will slow down.
For svg export you need to have svgwrite installed and simply use
nd.export_svg()
. With svg you can still edit some details for a presentation for example.More layout export types can be added relatively easy. Each type resides in separate class that receives the same layout info from the Nazca layout engine, such as polygons, lines and annotations, but exports it to the specific format.
Ronald
28 May 2020 at 22:37 #6153bastien
MemberHi Ronald,
Thanks for your answer. I am aware of export_plt() and of the clear=False option and this isn’t exactly what I was looking for. Thanks for pointing at the class, I looked a bit into it and saw that you just need the coordinates of the vertices making up a cell and with help of that post I came up with something that works pretty well for what I had in mind (adding annotation to document a library of layouts or having two subplots side by side). Here is a code snippet demonstrating it in case someone’s interested:
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() coord = [] for NT in nd.cell_iter(nd.cfg.defaultcell, flat=True): if NT.cell_start: print(f"\ncell: '{NT.cell.cell_name}'") for i, (pgon, points, bbox) in enumerate(NT.iters['polygon']): #print(f"{i}: points: {points}, layer: '{pgon.layer}'") coord.append(points) x0 = [i[0] for i in coord[0]] x0.append(x0[0]) #to connect last point to first point in the plot y0 = [i[1] for i in coord[0]] y0.append(y0[0]) #to connect last point to first point in the plot x1 = [i[0] for i in coord[1]] x1.append(x1[0]) y1 = [i[1] for i in coord[1]] y1.append(y1[0]) fig = go.Figure([ go.Scatter(x=x0, y=y0, fill="toself", marker=dict(opacity=0), name="arc layer1"), go.Scatter(x=x1, y=y1, fill="toself", marker=dict(opacity=0) ) ]) 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 ) fig.show() nd.clear_layout()
Bastien
7 June 2020 at 11:19 #6159Ronald
KeymasterDear 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
9 June 2020 at 18:18 #6161bastien
MemberDear Ronald,
Plotly is indeed more oriented for use in a web browser and it’s my visualization tool of choice in Jupyter lab. You might need to install an extension to get it to work in Jupyter though. You do not need an account to do all the basic plotting, as far as I understand you need an account only if you want to save plots on their servers (I remember this really confused me as well when I started using it a couple years ago but that has been somewhat clarified since version 4).
It should be fairly easy to implement a class with the methods you mentioned and I could contribute code when I’ve some time. Is there a good place for that?
Bastien
9 June 2020 at 19:13 #6162Ronald
KeymasterHi Bastien,
It seems plotly gets installed via Anaconda as well. Anyway, I already implemented a plotly export class as it was quite easy to do and it allows for some faster panning and zooming than matplotllib, probably. Hence, in Nazca.0.5.12 you can try
nd.export_plotly()
in a Jupyter Notebook. I implemented the basics for polygons and the color schema.Ronald
9 June 2020 at 19:33 #6163bastien
MemberGreat!
Would it be possible to have a kwarg to return the figure in
nd.export_plotly()
so that it can be updated later on? For instance:fig = nd.export_plotly(return_figure = True) fig.add_annotation(...)
return_figure = False
could be set as default to keep it consistent with other export functions.Bastien
-
AuthorPosts
- You must be logged in to reply to this topic.