|
10 | 10 | from mpl_toolkits.axes_grid1 import make_axes_locatable |
11 | 11 |
|
12 | 12 | from SuPyMode.Plotting.PlotsUtils import FieldMap, MidPointNorm |
| 13 | +from SuPyMode.Tools.utils import ToList |
13 | 14 |
|
14 | 15 |
|
15 | 16 | try: |
|
18 | 19 | except ImportError: |
19 | 20 | logging.warning('Mayavi package could not be loaded! Not 3D rendering available.') |
20 | 21 |
|
| 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 | + |
21 | 246 |
|
22 | 247 | class Scene2D: |
23 | 248 | UnitSize = (10, 3) |
|
0 commit comments