1717from .util import GraphicsGenerator
1818
1919
20- @Timer ("eval_Plot3D" )
21- def eval_Plot3D (
22- plot_options ,
23- evaluation : Evaluation ,
24- ):
25- graphics = GraphicsGenerator (dim = 3 )
20+ def make_plot (plot_options , evaluation : Evaluation , dim : int ):
21+ graphics = GraphicsGenerator (dim )
2622
2723 # pull out plot options
2824 _ , xmin , xmax = plot_options .ranges [0 ]
@@ -104,17 +100,39 @@ def compute_over_grid(nx, ny):
104100 # transpose and flatten to ((nx-1)*(ny-1), 4) array, suitable for use in GraphicsComplex
105101 quads = quads .T .reshape (- 1 , 4 )
106102
107- # choose a color
108- rgb = palette [i % len (palette )]
109- rgb = [c / 255.0 for c in rgb ]
110- # graphics.add_color(SymbolRGBColor, rgb)
111- graphics .add_directives ([SymbolRGBColor , * rgb ])
112-
113- # add a GraphicsComplex displaying a surface for this function
114- graphics .add_complex (xyzs , lines = None , polys = quads )
115-
116- # if requested by the Mesh attribute create a mesh of lines covering the surfaces
117- if nmesh :
103+ # Plot3D
104+ if dim == 3 :
105+ # choose a color
106+ rgb = palette [i % len (palette )]
107+ rgb = [c / 255.0 for c in rgb ]
108+ # graphics.add_color(SymbolRGBColor, rgb)
109+ graphics .add_directives ([SymbolRGBColor , * rgb ])
110+
111+ # add a GraphicsComplex displaying a surface for this function
112+ graphics .add_complex (xyzs , lines = None , polys = quads )
113+
114+ # DensityPlot
115+ elif dim == 2 :
116+ # Fixed palette for now
117+ # TODO: accept color options
118+ with Timer ("compute colors" ):
119+ zs = xyzs [:, 2 ]
120+ z_min , z_max = min (zs ), max (zs )
121+ zs = zs [:, np .newaxis ] # allow broadcasting
122+ c_min , c_max = [0.5 , 0 , 0.1 ], [1.0 , 0.9 , 0.5 ]
123+ c_min , c_max = (
124+ np .full ((len (zs ), 3 ), c_min ),
125+ np .full ((len (zs ), 3 ), c_max ),
126+ )
127+ colors = ((zs - z_min ) * c_max + (z_max - zs ) * c_min ) / (z_max - z_min )
128+
129+ # flatten the points and add the quads
130+ graphics .add_complex (xyzs [:, 0 :2 ], lines = None , polys = quads , colors = colors )
131+
132+ # If requested by the Mesh attribute create a mesh of lines covering the surfaces
133+ # For now only for Plot3D
134+ # TODO: mesh for DensityPlot?
135+ if nmesh and dim == 3 :
118136 # meshes are black for now
119137 graphics .add_directives ([SymbolRGBColor , 0 , 0 , 0 ])
120138
@@ -132,15 +150,17 @@ def compute_over_grid(nx, ny):
132150 return graphics
133151
134152
135- #
136- #
137- #
153+ @Timer ("eval_Plot3D" )
154+ def eval_Plot3D (
155+ plot_options ,
156+ evaluation : Evaluation ,
157+ ):
158+ return make_plot (plot_options , evaluation , dim = 3 )
138159
139160
161+ @Timer ("eval_DensityPlot" )
140162def eval_DensityPlot (
141163 plot_options ,
142164 evaluation : Evaluation ,
143165):
144- # TODO
145- # see plot3d.eval_DensityPlot for possible info on handling colors
146- pass
166+ return make_plot (plot_options , evaluation , dim = 2 )
0 commit comments