How does one set a logarithmic scale for both x and y axes in ContourPlot in Mathematica?
4 Answers
One possibility is to plot the contour plot with linear scales using ContourPlot and use ListLogLogPlot to transform this plot to one with logarithmic scales:
pl = Normal@ ContourPlot[ Sin[3 x] + Cos[3 y] == 1/2, {x, .01 Pi, 3 Pi}, {y, .01 Pi, 3 Pi}, PlotPoints -> 30] 
ListLogLogPlot[Cases[pl, Line[a_, b___] :> a, Infinity], Joined -> True, Frame -> True, PlotRange -> All, AspectRatio -> 1, PlotStyle -> ColorData[1][1]] 
- 1$\begingroup$ I keep missing the fun questions. Nice answer. +1 $\endgroup$Mr.Wizard– Mr.Wizard2012-05-07 08:56:37 +00:00Commented May 7, 2012 at 8:56
- $\begingroup$ Great idea! I never thought of that. $\endgroup$sebhofer– sebhofer2012-05-07 15:15:52 +00:00Commented May 7, 2012 at 15:15
- 6$\begingroup$ One disadvantage this method might have is that of point sampling. The right hand and the upper parts are better sampled than the left hand and lower parts of the plot. This might lead to curves that are less smooth on one side than on the other. $\endgroup$Sjoerd C. de Vries– Sjoerd C. de Vries2012-05-07 17:33:58 +00:00Commented May 7, 2012 at 17:33
- 1$\begingroup$ I think that there are actually a lot of other limitations... It's a pity the scaling functions are still not available to this type of plot... For instance, as long as we choose a simple option like ContourLabels, everything becomes more messy... $\endgroup$P. Fonseca– P. Fonseca2016-05-01 10:29:03 +00:00Commented May 1, 2016 at 10:29
- $\begingroup$ Hahllelujah! Now if only they would provide better Tick control options for Log Plots $\endgroup$Paul Drake– Paul Drake2018-04-08 15:58:49 +00:00Commented Apr 8, 2018 at 15:58
Instead of doing some transformation on the original ContourPlot we can do an exponential rescaling of the original variables in the ContourPlot, so this is somewhat different approach to get roughly the same result :
ContourPlot[ Sin[ 3 Exp@x] + Cos[ 3 Exp@y ] == 1/2, {x, Log[0.01 Pi], Log[3 Pi]}, {y, Log[0.01 Pi], Log[3 Pi]}, PlotPoints -> 30] 
The only difference is a different coordinate system.
- $\begingroup$ The downside is the ticks placement and labels. $\endgroup$a06e– a06e2017-10-03 23:37:42 +00:00Commented Oct 3, 2017 at 23:37
- $\begingroup$ @becko If you had read the answer below you wouldn't have mentioned this at all. $\endgroup$Artes– Artes2017-10-04 07:37:31 +00:00Commented Oct 4, 2017 at 7:37
As of M11, you can use the ScalingFunctions option:
ContourPlot[ Sin[3 x]+Cos[3 y]==1/2, {x,.01 Pi,3 Pi}, {y,.01 Pi,3 Pi}, ScalingFunctions->{"Log","Log"} ] - $\begingroup$ It appears that I'm six months late to this answer. :) $\endgroup$rcollyer– rcollyer2018-07-10 20:02:52 +00:00Commented Jul 10, 2018 at 20:02
- $\begingroup$ Unfortunately this option is just a patch job, it just log's the data and relabels the axes in one step, therefore you cannot combine this plot with other true log plots e.g. LogListPlot. $\endgroup$Jay– Jay2019-10-08 18:48:20 +00:00Commented Oct 8, 2019 at 18:48
- $\begingroup$ @Jay Can you provide an example where plots can't be combined? Perhaps in another question. $\endgroup$Carl Woll– Carl Woll2019-10-08 19:12:35 +00:00Commented Oct 8, 2019 at 19:12
- 1$\begingroup$ There is a simple workaround: use scaling functions for ListPlot instead of LogListPlot.. I don't know if this warrants it's own question. $\endgroup$Jay– Jay2019-10-09 20:36:00 +00:00Commented Oct 9, 2019 at 20:36
- 1$\begingroup$ What if I wanted to Log only one axis? what would I replace "Log" with for the one I wanted to stay linear? $\endgroup$Rudyard– Rudyard2021-03-30 13:43:21 +00:00Commented Mar 30, 2021 at 13:43
As a slight variation of the nice suggestion above add FrameTicks to get the tick labels you want.
ContourPlot[ Sin[3 Exp[x]] + Cos[3 Exp[y]] == 1/2, {x, Log[0.01 Pi], Log[3 Pi]}, {y, Log[0.01 Pi], Log[3 Pi]}, PlotPoints -> 30, FrameTicks -> {Table[{y, ToString[Round[10^y, 0.001]]}, {y, Log[10, 0.001], Log[10, 100]}], Table[{y, ToString[Round[10^y, 0.001]]}, {y, Log[10, 0.001], Log[10, 100]}]}] - 6$\begingroup$ Your example uses a base-E log scale for the plotting and a base-10 log scale for the ticks. Shouldn't these be consistent? $\endgroup$Sean– Sean2013-11-16 04:16:34 +00:00Commented Nov 16, 2013 at 4:16
