# Plotting: substitute before evaluation

When plotting, the Mathematica seems to substitute the value of the variable only after evaluating the plotted function. To use a simple example where I'm trying to examine how changing certain parameters affect a model:

randomData = RandomReal[{0, 1}, {10, 2}];
LogLinearPlot[
LinearModelFit[randomData, x, x, Weights -> (# + q &)][1],
{q, 1, 5}
]


gives lots of errors saying:

LinearModelFit::wtsnn: One or more weight values are not non-negative numbers. Weight values must be non-negative.


This seems to be because (# + q &) is being evaluated before LogLinearPlot substitutes the value of q, and LinearModelFit doesn't like receiving non-numerical weights.

When I use Plot, this can be fixed by putting it into a Module,

(* This works fine *)
Plot[
Module[{}, LinearModelFit[randomData, x, x, Weights -> (# + q &)][1]],
{q, 0, 5}
]


but this doesn't seem to work with LogLinearPlot for some reason.

How do I make LogLinearPlot substitute the variable before any evaluation has taken place? How do I ensure that the value of q is substituted before (# + q &) is evaluated?

• Your first code runs fine with no errors. Maybe you have some old definitions. Try clearing the variables or quiting the kernel altogether. – corey979 Nov 14 '16 at 15:54
• Nope. Tried again in a new notebook after restarting the application, same result. It does give an output though, so maybe it's actually a warning instead of error. But it still slows down the computation noticeably and generates lots of spam messages, so still needs to be fixed. – goweon Nov 14 '16 at 17:04
• I get no error as well with MMA 11.0. What version are you using? – anderstood Nov 14 '16 at 18:16
• Hmm... interesting. I'm still on v10.0. I'll try upgrading and see if that fixes it. – goweon Nov 14 '16 at 18:23
• No error on V10.3. – anderstood Nov 14 '16 at 20:19

Seems to be a bug in v10.0 that LogLinearPlot has different behavior from Plot. Problem fixed by upgrading to v11.0