Using SymPy, I can create a contour plot manually using the following code (there isn't yet a built-in contour plotting function Update: SymPy now has a contour plotting function):
from sympy import init_session
init_session()
from sympy.plotting.plot import Plot, ContourSeries
# show plot centered at 0,0
x_min = -7
x_max = 7
y_min = -5
y_max = 5
# contour plot of inverted cone
my_plot = Plot(
ContourSeries(
sqrt(x**2 + y**2),
(x,x_min,x_max),
(y,y_min,y_max)
)
)
my_plot.show()
Currently, when SymPy calls contour()
, it does not appear to be saving the returned ContourSet (Update: I have filed a issue to see if the ContourSet can be saved):
class MatplotlibBackend(BaseBackend):
...
def process_series(self):
...
for s in self.parent._series:
# Create the collections
...
elif s.is_contour:
self.ax.contour(*s.get_meshes()) # returned ContourSet not saved by SymPy
In other examples where modifications are performed to the plot, such as adding inline labels using clabel()
, the ContourSet (CS
) is needed:
# Create a simple contour plot with labels using default colors.
plt.figure()
CS = plt.contour(X, Y, Z) # CS is the ContourSet
plt.clabel(CS, inline=1, fontsize=10)
plt.title('Simplest default with labels')
Going back to the SymPy example, my_plot._backend
does provide access to the figure and axes; what workarounds are possible to keep or obtain access to the ContourSet?