ContourPlot is slow and unwieldy and generates a large-data graphic

I'm constructing a single contour showing the points in a plane that satisfy a function. The general form is

ContourPlot[f[x, y] == 0, {x, 0, .5}, {y, 0.2, .5}]


The function f is an explicit but complicated formula that was generated from operations involving analytic integration and differentiation applied to a large algebraic function. It evaluates quite rapidly though, given values of its arguments.

My problem is that the plot takes an unduly long time to generate, perhaps 5 to 10 minutes. Also, and this may be related, the graphic object that it produces has a very large amount of data associated with it -- if I copy it as text and paste it into Word, it exceeds 10,000 pages! The text looks like it is describing how the function is evaluated (it includes function calls that are part of the definition of f), rather than a simple list of data points and formatting information. If I try to Show the output, to combine it with other graphics, it takes a long time to generate, as if it is recomputing the graph again. Also, I get a "Reformatting notebook" message any time I touch the object (e.g., to select it), and this takes maybe 15 or 20 seconds to go away. The plot itself isn't particularly complicated:

I can make a ContourPlot of a simple function (e.g., x^2+y^2==1) and it plots quickly, and cut/paste it as text into Word occupies less than two pages.

• Perhaps the issue depends on f, then? I'm not sure how someone can help without f. Perhaps ListContourPlot? Welcome to mathematica.stackexchange, btw. – Michael E2 Sep 5 '14 at 1:28
• Does it help if you add Evaluated -> True to the plot, or if you define g[x_, y_] = Simplify[f[x, y]] and plot it instead? (Note, that's =, not :=.) – user484 Sep 5 '14 at 1:55
• Thanks for the quick response. Sorry I can't provide more detail about the function. Basically it's a very large algebraic function of x and y. I'm particularly puzzled by why the graphic comes out as much more than the data and formatting. I tried Rahul's suggestions (though I didn't wait for Simplify to finish, and just used assignment of f to g). Neither that nor Evaluated->True helped. I've now turned to ListContourPlot and I think that will work for me. I can make a grid of values sufficiently quickly and the ListContour provides a good representation of the contour I want. – Dave Sep 5 '14 at 3:03
• This question appears to be off-topic because the OP asserts he can not provide information critical to working out an answer. – m_goldberg Sep 5 '14 at 10:35
• I'm not sure what other information is needed. It is a very large algebraic function. It returns a real when reals are entered to it from the relevant domain. I don't understand how that detail would affect what is happening. Maybe by answering that I'd better understand what to look for to solve my problem. ContourPlot produces a correct contour for this function, so something is working correctly. I was hoping with this question to get insight into how Mma represents graphics, and what would cause it to represent it in terms of anything but the computed data and the associated formatting – Dave Sep 5 '14 at 11:46

I have a guess: ContourPlot is storing your unwieldy function as a Tooltip. You can turn that off with ContourLabels -> None:

ContourPlot[f[x, y] == 0, {x, 0, .5}, {y, 0.2, .5}, ContourLabels -> None]


Update: Sufficient information?

There are these clues, including my own experience of ContourPlot:

1. The function expression is extremely large. (OP)

2. The function expression appears in the graphics code when pasted into Word. (OP)

3. Simple function plots work fine. (OP)

4. ContourPlot produces a Tooltip, which requires CPU time whenever the mouse passes over the curve. (Me)

5. I have had the front end tied up by extremely long tooltip messages, frequently accompanied by a "Reformatting notebook" message. (Me)

These I think point strongly to the solution above. The other bit of evidence about Show was difficult to weigh without having the complete code to investigate. It is consistent with the typesetting of the tooltip taking a long time, but I do not know if that happens when the graphics are displayed; it might only happen when the tooltip is displayed. It seemed less likely that the OP's f was doing something odd, given the OP's description of the function.

• That's it!! I added the option you suggest, and now it produces a graphic object that is quite reasonable. When I copy it as text it is less than 4 Word pages, rather than 10,000+. I can also use it with Show to merge it with other graphs. Thank you very much!! – Dave Sep 6 '14 at 2:31
• @Dave You're welcome. :) – Michael E2 Sep 7 '14 at 18:03
• Nice work debugging, and I think your sleuthing makes this question worthy of reopening as it should be of some value to future visitors. +1 – bobthechemist Sep 7 '14 at 18:21
• Thanks all for your patience and perseverance with my question. In the future I'll make sure to pose my question in a way that allows others to reproduce it fully. I didn't think that possible in this case, given that it involved a function too large to post. On further thought I realize I could have set up a trial function of comparable complexity that is defined by the manipulations that produce it, in lieu of a verbatim copy of the function itself. Again, thanks everyone, and particularly @MichaelE2, for your expert assistance. – Dave Sep 7 '14 at 19:40