Skip to content

Commit f03bcae

Browse files
committed
update Plottings almost finished god damn it
1 parent 3bdb7ac commit f03bcae

10 files changed

Lines changed: 863 additions & 262 deletions

File tree

ExportedPDF.pdf

266 KB
Binary file not shown.

SuPyMode/Plotting/Plots.py

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from mpl_toolkits.axes_grid1 import make_axes_locatable
1111

1212
from SuPyMode.Plotting.PlotsUtils import FieldMap, MidPointNorm
13+
from SuPyMode.Tools.utils import ToList
1314

1415

1516
try:
@@ -18,6 +19,230 @@
1819
except ImportError:
1920
logging.warning('Mayavi package could not be loaded! Not 3D rendering available.')
2021

22+
import matplotlib
23+
matplotlib.style.use('ggplot')
24+
25+
26+
27+
28+
29+
class AddShapely:
30+
def __init__(self, Object, Text=None):
31+
self.Object = Object
32+
self.Text = Text
33+
34+
def Render(self, Ax):
35+
if isinstance(Object, Point):
36+
Ax.scatter(Object.x, Object.y, linewidth=7)
37+
38+
if isinstance(Object, (Polygon, MultiPolygon) ):
39+
Image = Ax.add_patch( PolygonPatch(Object, alpha=0.5) )
40+
41+
if Text:
42+
Ax.text(Object.x, Object.y, Text)
43+
44+
45+
46+
47+
48+
class Contour:
49+
def __init__(self, X, Y, Scalar, ColorMap='viridis', Title=None, xLabel=None, yLabel=None, IsoLines=None):
50+
self.X = X
51+
self.Y = Y
52+
self.Scalar = Scalar
53+
self.ColorMap = ColorMap
54+
self.Label = Label
55+
self.IsoLines = IsoLines
56+
57+
58+
def Render(self, Ax):
59+
Image = Ax.contour(self.X,
60+
self.Y,
61+
self.Scalar,
62+
level = self.IsoLines,
63+
colors="black",
64+
linewidth=.5 )
65+
66+
Image = Ax.contourf(self.X,
67+
self.Y,
68+
self.Scalar,
69+
level = self.IsoLines,
70+
cmap=self.ColorMap,
71+
norm=colors.LogNorm() )
72+
73+
74+
75+
class Mesh:
76+
def __init__(self, X, Y, Scalar, ColorMap='viridis', DiscretNorm=None, Label=''):
77+
self.X = X
78+
self.Y = Y
79+
self.Scalar = Scalar
80+
self.ColorMap=ColorMap
81+
self.Label = Label
82+
83+
#norm = colors.BoundaryNorm(DiscretNorm, ColorMap) if DiscretNorm is not None else None
84+
85+
def Render(self, Ax):
86+
Image = Ax.pcolormesh(self.X,
87+
self.Y,
88+
self.Scalar,
89+
cmap=self.ColorMap,
90+
shading='auto',
91+
vmin=-np.max(np.abs(self.Scalar)),
92+
vmax=+np.max(np.abs(self.Scalar)))
93+
94+
return Image
95+
96+
97+
98+
class Line:
99+
def __init__(self, X, Y, Label=None, Fill=False, Color=None):
100+
self.X = X
101+
self.Y = Y
102+
self.Fill = Fill
103+
self.Label = Label
104+
self.Color = Color
105+
106+
def Render(self, Ax):
107+
108+
Ax.plot(self.X, self.Y, label=self.Label)
109+
110+
if self.Fill:
111+
Ax.fill_between(self.X, self.Y.min(), self.Y, color=self.Color, alpha=0.7)
112+
113+
114+
115+
class Axis:
116+
def __init__(self, Row, Col, xLabel, yLabel, Title, Grid=True, Legend=True, xScale='linear', yScale='linear', xLimits=None, yLimits=None, Equal=False, ColorBar=False, ColorbarPosition='bottom'):
117+
self.Row = Row
118+
self.Col = Col
119+
self.xLabel = xLabel
120+
self.yLabel = yLabel
121+
self.Title = Title
122+
self.Legend = Legend
123+
self.Artist = []
124+
self.MPLAxis = None
125+
self.Grid = Grid
126+
self.xScale = xScale
127+
self.yScale = yScale
128+
self.xLimits = xLimits
129+
self.yLimits = yLimits
130+
self.Equal = Equal
131+
self.ColorBar = ColorBar
132+
self.ColorbarPosition = ColorbarPosition
133+
134+
135+
def AddArtist(self, *Artist):
136+
for art in Artist:
137+
self.Artist.append(art)
138+
139+
def Render(self):
140+
for art in self.Artist:
141+
Image = art.Render(self.MPLAxis)
142+
143+
if self.Legend:
144+
self.MPLAxis.legend()
145+
146+
self.MPLAxis.grid(self.Grid)
147+
148+
if self.xLimits is not None: self.MPLAxis.set_xlim(self.xLimits)
149+
if self.yLimits is not None: self.MPLAxis.set_ylim(self.yLimits)
150+
151+
self.MPLAxis.set_xlabel(self.xLabel)
152+
self.MPLAxis.set_ylabel(self.yLabel)
153+
self.MPLAxis.set_title(self.Title)
154+
155+
self.MPLAxis.set_xscale(self.xScale)
156+
self.MPLAxis.set_yscale(self.yScale)
157+
158+
if self.Equal:
159+
self.MPLAxis.set_aspect("equal")
160+
161+
if self.ColorBar:
162+
divider = make_axes_locatable(self.MPLAxis)
163+
cax = divider.append_axes(self.ColorbarPosition, size="5%", pad=0.05)
164+
plt.colorbar(Image, cax=cax, )
165+
166+
167+
168+
169+
class Scene:
170+
UnitSize = (10, 3)
171+
plt.rcParams['ytick.labelsize'] = 8
172+
plt.rcParams['xtick.labelsize'] = 8
173+
plt.rcParams["font.size"] = 8
174+
plt.rcParams["font.family"] = "serif"
175+
plt.rcParams['axes.edgecolor'] = 'black'
176+
plt.rcParams['axes.linewidth'] = 1.5
177+
178+
def __init__(self, Title='', UnitSize=None):
179+
self.Axis = []
180+
self.Title = Title
181+
self.nCols = 1
182+
self.nRows = None
183+
if UnitSize is not None: self.UnitSize = UnitSize
184+
185+
186+
def AddAxes(self, *Axis):
187+
for ax in Axis:
188+
self.Axis.append(ax)
189+
190+
191+
def GetMaxColsRows(self):
192+
RowMax, ColMax = 0,0
193+
for ax in self.Axis:
194+
RowMax = ax.Row if ax.Row > RowMax else RowMax
195+
ColMax = ax.Col if ax.Col > ColMax else ColMax
196+
197+
return RowMax, ColMax
198+
199+
200+
def GenerateAxis(self):
201+
RowMax, ColMax = self.GetMaxColsRows()
202+
203+
self.nRows = len(self.Axis)
204+
205+
FigSize = [ self.UnitSize[0]*(ColMax+1), self.UnitSize[1]*(RowMax+1) ]
206+
207+
self.Figure, Ax = plt.subplots(ncols=ColMax+1, nrows=RowMax+1, figsize=FigSize)
208+
209+
if not isinstance(Ax, np.ndarray): Ax = np.asarray([[Ax]])
210+
if Ax.shape == 1: Ax = np.asarray([A])
211+
212+
self.Figure.suptitle(self.Title)
213+
214+
for ax in self.Axis:
215+
ax.MPLAxis = Ax[ax.Row, ax.Col]
216+
217+
218+
def Render(self):
219+
self.GenerateAxis()
220+
221+
for ax in self.Axis:
222+
ax.Render()
223+
224+
plt.tight_layout()
225+
226+
227+
def Show(self):
228+
self.Render()
229+
plt.show()
230+
231+
232+
233+
234+
235+
236+
237+
238+
239+
240+
241+
242+
243+
244+
245+
21246

22247
class Scene2D:
23248
UnitSize = (10, 3)

SuPyMode/SuperMode.py

Lines changed: 72 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
from mayavi import mlab
88

99
from SuPyMode.Tools.BaseClass import SetProperties, SetPlottings
10-
from SuPyMode.Plotting.Plots import Scene2D
10+
from SuPyMode.Plotting.Plots import Scene2D, Scene, Axis, Line, Mesh
11+
from SuPyMode.Plotting.PlotsUtils import FieldMap, MidPointNorm
1112

1213
Mlogger = logging.getLogger(__name__)
1314

@@ -418,54 +419,89 @@ def Betas(self):
418419
return self._Betas
419420

420421

422+
def _PlotIndex(self, Ax):
423+
artist = Line(X=self.ITRList, Y=self.Index, Label=None, Fill=False)
424+
425+
Ax.AddArtist(artist)
426+
427+
428+
def _PlotBetas(self, Ax):
429+
artist = Line(X=self.ITRList, Y=self.Betas, Label=self.Name, Fill=False)
430+
431+
Ax.AddArtist(artist)
432+
433+
421434
def PlotIndex(self):
422-
Scene = Scene2D(nCols=1, nRows=1, ColorBar=False)
435+
Fig = Scene('SuPyMode Figure', UnitSize=(10,4))
423436

424-
Scene.AddLine(Row = 0,
425-
Col = 0,
426-
x = self.ITRList,
427-
y = self.Index,
428-
Fill = False,
429-
Legend = self.Name,
430-
xLabel = r'ITR',
431-
yLabel = r'Effective refractive index n$_{eff}$')
437+
ax = Axis(Row = 0,
438+
Col = 0,
439+
xLabel = 'ITR',
440+
yLabel = r'Effective refraction index',
441+
Title = None,
442+
Grid = True,
443+
xScale = 'linear',
444+
yScale = 'linear')
432445

433-
Scene.SetAxes(0, 0, Equal=False, Legend=True, yLimits=[self.Geometry.MinIndex/1.005, self.Geometry.MaxIndex], LegendTitle='Mode')
434-
Scene.Show()
446+
self._PlotIndex(ax)
447+
448+
Fig.AddAxes(ax)
449+
450+
Fig.Show()
435451

436452

437453
def PlotBetas(self):
438-
Scene = Scene2D(nCols=1, nRows=1, ColorBar=False)
454+
Fig = Scene('SuPyMode Figure', UnitSize=(10,4))
439455

440-
Scene.AddLine(Row = 0,
441-
Col = 0,
442-
x = self.ITRList,
443-
y = self.Betas,
444-
Fill = False,
445-
Legend = self.Name,
446-
xLabel = r'ITR',
447-
yLabel = r'Propagation constante $\beta$')
456+
ax = Axis(Row = 0,
457+
Col = 0,
458+
xLabel = 'ITR',
459+
yLabel = r'Propagation constante $\beta$',
460+
Title = None,
461+
Grid = True,
462+
xScale = 'linear',
463+
yScale = 'linear')
448464

449-
Scene.SetAxes(0, 0, Equal=False, Legend=True, LegendTitle='Mode')
450-
Scene.Show()
465+
self._PlotBetas(ax)
466+
467+
Fig.AddAxes(ax)
468+
469+
Fig.Show()
470+
471+
472+
def _PlotFields(self, Ax, slice):
473+
artist = Mesh(X = self.FullxAxis,
474+
Y = self.FullyAxis,
475+
Scalar = self.FullFields[slice].T,
476+
ColorMap = FieldMap,
477+
DiscretNorm = True,
478+
)
479+
480+
Ax.AddArtist(artist)
451481

452482

453483
def PlotFields(self, Slice: list):
454-
Scene = Scene2D(nCols=len(Slice), nRows=1, ColorBar=False, UnitSize=[3,3])
484+
Fig = Scene('SuPyMode Figure', UnitSize=(10,4))
455485

456486
for n, slice in enumerate(Slice):
457-
Scene.AddMesh(Row = 0,
458-
Col = n,
459-
x = self.FullxAxis,
460-
y = self.FullyAxis,
461-
Scalar = self.FullFields[slice].T,
462-
xLabel = r'X-Direction [$\mu m$]',
463-
yLabel = r'Y-direction [$\mu m$]',
464-
Title = self.Name)
465-
466-
Scene.SetAxes(Row=0, Col=n, Equal=True, Legend=False)
467-
Scene.Show()
468-
487+
ax = Axis(Row = 0,
488+
Col = n,
489+
xLabel = r'X-Direction [$\mu m$]',
490+
yLabel = r'Y-direction [$\mu m$]',
491+
Title = f'{self.Name} [ITR: {self.ITRList[slice]:.2f}]',
492+
Legend = False,
493+
ColorBar=True,
494+
ColorbarPosition = 'right',
495+
Grid = True,
496+
Equal = True,
497+
xScale = 'linear',
498+
yScale = 'linear')
499+
500+
self._PlotFields(ax, slice)
501+
502+
Fig.AddAxes(ax)
503+
504+
Fig.Show()
469505

470506

471507
@property

0 commit comments

Comments
 (0)