Skip to content

Commit 51feaee

Browse files
committed
Density plot
1 parent f8a4919 commit 51feaee

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

mathics/eval/drawing/plot3d_vectorized.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121
def eval_Plot3D(
2222
plot_options,
2323
evaluation: Evaluation,
24+
density = False
2425
):
25-
graphics = GraphicsGenerator(dim=3)
26+
graphics = GraphicsGenerator(dim = 2 if density else 3)
2627

2728
# pull out plot options
2829
_, xmin, xmax = plot_options.ranges[0]
@@ -104,14 +105,30 @@ def compute_over_grid(nx, ny):
104105
# transpose and flatten to ((nx-1)*(ny-1), 4) array, suitable for use in GraphicsComplex
105106
quads = quads.T.reshape(-1, 4)
106107

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])
108+
if not density:
112109

113-
# add a GraphicsComplex displaying a surface for this function
114-
graphics.add_complex(xyzs, lines=None, polys=quads)
110+
# choose a color
111+
rgb = palette[i % len(palette)]
112+
rgb = [c / 255.0 for c in rgb]
113+
# graphics.add_color(SymbolRGBColor, rgb)
114+
graphics.add_directives([SymbolRGBColor, *rgb])
115+
116+
# add a GraphicsComplex displaying a surface for this function
117+
graphics.add_complex(xyzs, lines=None, polys=quads)
118+
119+
else:
120+
121+
with Timer("compute colors"):
122+
zs = xyzs[:,2]
123+
z_min, z_max = min(zs), max(zs)
124+
zs = zs[:, np.newaxis] # allow broadcasting
125+
c_min, c_max = [0.5, 0, 0.1], [1.0, 0.9, 0.5]
126+
c_min, c_max = np.full((len(zs),3), c_min), np.full((len(zs),3), c_max)
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+
115132

116133
# if requested by the Mesh attribute create a mesh of lines covering the surfaces
117134
if nmesh:
@@ -141,6 +158,4 @@ def eval_DensityPlot(
141158
plot_options,
142159
evaluation: Evaluation,
143160
):
144-
# TODO
145-
# see plot3d.eval_DensityPlot for possible info on handling colors
146-
pass
161+
return eval_Plot3D(plot_options, evaluation, density=True)

mathics/eval/drawing/util.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
SymbolGraphicsComplex,
1515
SymbolLine,
1616
SymbolPolygon,
17+
SymbolRGBColor,
1718
SymbolRule,
1819
SymbolVertexColors,
1920
)
@@ -75,14 +76,18 @@ def cvt(d):
7576
expr = cvt(d)
7677
self.graphics.append(expr)
7778

78-
def add_complex(self, xyzs, lines=None, polys=None):
79+
def add_complex(self, xyzs, lines=None, polys=None, colors=None):
7980
complex = [NumericArray(xyzs)]
8081
if polys is not None:
8182
polys_expr = Expression(SymbolPolygon, NumericArray(polys))
8283
complex.append(polys_expr)
8384
if lines is not None:
8485
lines_expr = Expression(SymbolLine, NumericArray(lines))
8586
complex.append(lines_expr)
87+
if colors is not None:
88+
colors_expr = Expression(SymbolRGBColor, NumericArray(colors))
89+
rule_expr = Expression(SymbolRule, SymbolVertexColors, colors_expr)
90+
complex.append(rule_expr)
8691
gc_expr = Expression(SymbolGraphicsComplex, *complex)
8792
self.graphics.append(gc_expr)
8893

0 commit comments

Comments
 (0)