Skip to content

Commit 4e17dff

Browse files
author
Benjamin Chrétien
committed
plot: split contour and contourf.
1 parent c56ecd8 commit 4e17dff

2 files changed

Lines changed: 25 additions & 16 deletions

File tree

src/roboptim/core/visualization/plotter.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
import numpy as np
77

88
class PlotStyle2D(object):
9-
Contour, PColorMesh = range(2)
9+
Contour, Contourf, PColorMesh = range(3)
1010

1111
class PlotStyle3D(object):
12-
Contour, Wireframe, Triangle = range(3)
12+
Contour, Contourf, Wireframe, Triangle = range(4)
1313

1414
class Plotter(object):
1515
"""
@@ -77,20 +77,24 @@ def plot(self, f, plot_style=PlotStyle2D.PColorMesh, cmap=None, *args, **kwargs)
7777

7878
if cmap is not None:
7979
if self.isstr(cmap):
80-
cmap = cm.get_cmap(cmap)
80+
kwargs['cmap'] = cm.get_cmap(cmap)
81+
else:
82+
kwargs['cmap'] = cmap
8183

8284
Z = self.compute_z(f,X,Y)
8385

8486
# Contour plotting
8587
if plot_style == PlotStyle2D.Contour:
86-
self.ax.contourf(X, Y, Z, 1, cmap=cmap, *args, **kwargs)
87-
if 'levels' in kwargs:
88-
self.ax.contour(X, Y, Z, max(self.x_res, self.y_res),
89-
*args, **kwargs)
88+
cs = self.ax.contour(X, Y, Z, max(self.x_res, self.y_res),
89+
*args, **kwargs)
90+
if 'label' in kwargs and kwargs['label'] is True:
91+
plt.clabel(cs, *args, **kwargs)
92+
# Filled contour plotting
93+
elif plot_style == PlotStyle2D.Contourf:
94+
self.ax.contourf(X, Y, Z, 1, *args, **kwargs)
9095
# Color mesh plotting
9196
elif plot_style == PlotStyle2D.PColorMesh:
92-
mesh = self.ax.pcolormesh(X, Y, Z, cmap=cmap,
93-
*args, **kwargs)
97+
mesh = self.ax.pcolormesh(X, Y, Z, *args, **kwargs)
9498
plt.colorbar(mesh)
9599
else:
96100
return
@@ -117,7 +121,9 @@ def plot(self, f, plot_style=PlotStyle3D.Triangle, cmap=None,
117121

118122
if cmap is not None:
119123
if self.isstr(cmap):
120-
cmap = cm.get_cmap(cmap)
124+
kwargs['cmap'] = cm.get_cmap(cmap)
125+
else:
126+
kwargs['cmap'] = cmap
121127

122128
Z = self.compute_z(f,X,Y)
123129

@@ -127,12 +133,13 @@ def plot(self, f, plot_style=PlotStyle3D.Triangle, cmap=None,
127133
# Triangle plotting
128134
elif plot_style == PlotStyle3D.Triangle:
129135
self.ax.plot_trisurf(X.flatten(), Y.flatten(), Z.flatten(),
130-
cmap=cmap, *args, **kwargs)
136+
*args, **kwargs)
137+
# Filled contour plotting
138+
elif plot_style == PlotStyle3D.Contourf:
139+
self.ax.contourf(X, Y, Z, 1, *args, **kwargs)
131140
# Contour plotting
132141
elif plot_style == PlotStyle3D.Contour:
133-
self.ax.contourf(X, Y, Z, 1, cmap=cmap, *args, **kwargs)
134-
if 'levels' in kwargs:
135-
self.ax.contour(X, Y, Z, max(self.x_res, self.y_res),
136-
*args, **kwargs)
142+
self.ax.contour(X, Y, Z, max(self.x_res, self.y_res),
143+
*args, **kwargs)
137144
else:
138145
return

tests/plot.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ def test_2d_plot(self):
2626
plotter = Plotter2D([-10,10],[-10,10])
2727
plotter.x_res = 10
2828
plotter.y_res = 10
29-
plotter.plot(f, plot_style=PlotStyle2D.Contour)
29+
plotter.plot(f, plot_style=PlotStyle2D.Contour, label=True)
30+
plotter.plot(f, plot_style=PlotStyle2D.Contourf)
3031
plotter.plot(f, plot_style=PlotStyle2D.PColorMesh)
3132
plotter.add_marker([0], [0], color="black", marker="x",
3233
markersize=10, markeredgewidth=2)
@@ -38,6 +39,7 @@ def test_3d_plot(self):
3839
plotter.x_res = 10
3940
plotter.y_res = 10
4041
plotter.plot(f, plot_style=PlotStyle3D.Contour)
42+
plotter.plot(f, plot_style=PlotStyle3D.Contourf)
4143
plotter.plot(f, plot_style=PlotStyle3D.Wireframe)
4244
plotter.plot(f, plot_style=PlotStyle3D.Triangle)
4345
plotter.add_marker([0], [0], color="black", marker="x",

0 commit comments

Comments
 (0)