# Smooth out contour in RegionPlot/ContourPlot

I would like to smooth out the jagged edges in the outermost contours in the following plot: I have data of values as a function of variables (x,y) in the form

data = {{{x1,y1},z1},{{x2,y2},z2},...};


The figure is produced by

data = << "data.dat";
fnct = Interpolation[data];
RegionPlot[{fnct[x, y] > 2, fnct[x, y] > 100, fnct[x, y] > 140}, {x, 10^-2, 10^1}, {y, 0, 3},MaxRecursion->5]


The outer edges seem jagged I guess because the data is not very good there. I need to fit a smooth contour instead. I've looked at some other questions like Smoothing ListContourPlot contours but that uses ListContourPlot which for me takes ages and produces a bad result even for the original figure.

Any help appreciated.

The data can be found here:

• The moving average method will damage the data a lot, so I recommend to use Gaussian Filter to do the smoothing, which will primarily get rid of the noises without changing the figure too much. – Yuncong Ma Mar 1 '16 at 23:46
• I recommend presenting the computation and its result. – bbgodfrey Mar 2 '16 at 0:44

First we import the data, which OP is kind enough to leave in his Dropbox for these past 18 months.

data = N@Get["https://dl.dropboxusercontent.com/u/34796693/data.dat"];


Next we look at the data and notice that it is on a rectangular grid,

Dimensions[data]
Dimensions@Gather[data[[All, 1, 1]]]
(* {180901, 2} *)
(* {601, 301} *)


This is going to make all the difference here (if it weren't on a rectangular grid than you wouldn't have been able to do a decent Interpolation anyway).

The plan here is to apply a MovingAverage along the rows and columns of the grid. MovingAverage likes to work on matrices or vectors, not lists like {{{x1,y1},z1},{{x2,y2},z2}....}, so there are a couple of reshaping steps thrown in here for good measure.

npts = 10;
smootheddata = {{#1, #2}, #3} & @@@
Flatten[
Transpose[
MovingAverage[#, npts] & /@
Transpose[
MovingAverage[#, npts] & /@
Partition[Flatten /@ data, 301]
]
]
, 1];


Now you just do as you were doing before, create an interpolation function then apply RegionPlot to it, but now you must be aware that you have a smaller range of gridpoints after applying the averaging.

{{xmin, xmax}, {ymin, ymax}} =
MinMax /@ Transpose[smootheddata[[All, 1]]]
fnct = Interpolation[smootheddata];
RegionPlot[{fnct[x, y] > 2, fnct[x, y] > 100, fnct[x, y] > 140}, {x,
xmin, xmax}, {y, ymin, ymax}, MaxRecursion -> 5]

(* {{0.0107256, 9.66286}, {0.045, 2.955}} *) You can see that it is probably fine to just average the rows, but the best results come from averaging both rows and columns. Above I've used 10 as the number of elements to average over, but you can get a smoother plot by including more elements. This will have the effect of decreasing the size of the grid points so you have to choose the best option. Here are a few values for npts • Thank you for an informative (if somewhat belated) solution. – Philo Jan 14 '16 at 16:30
{"ft_lang_label":"__label__en","ft_lang_prob":0.8806481,"math_prob":0.9155136,"size":1781,"snap":"2021-31-2021-39","text_gpt3_token_len":510,"char_repetition_ratio":0.08497468,"word_repetition_ratio":0.006756757,"special_character_ratio":0.31836048,"punctuation_ratio":0.17886178,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.97196126,"pos_list":[0,1,2,3,4,5,6,7,8],"im_url_duplicate_count":[null,5,null,5,null,5,null,5,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-07-28T20:02:56Z\",\"WARC-Record-ID\":\"<urn:uuid:6df985d0-4159-4855-844f-9b52acf9deba>\",\"Content-Length\":\"170552\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:1446f0d0-fe27-4431-b17c-a04ec82a5b59>\",\"WARC-Concurrent-To\":\"<urn:uuid:1fcb5b55-5973-45e3-9055-2373bd386d27>\",\"WARC-IP-Address\":\"151.101.193.69\",\"WARC-Target-URI\":\"https://mathematica.stackexchange.com/questions/51518/smooth-out-contour-in-regionplot-contourplot/104068\",\"WARC-Payload-Digest\":\"sha1:OYUEZD54QZYLOLYWLISSVDJOBLNARMML\",\"WARC-Block-Digest\":\"sha1:UGW4GNOROR3BDQ52FLZHSVPU5HJCJQZZ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-31/CC-MAIN-2021-31_segments_1627046153791.41_warc_CC-MAIN-20210728185528-20210728215528-00342.warc.gz\"}"}