3
$\begingroup$

I assumed that the following plot for Sin would be red between .5 and .7, but it's all blue. Am I misunderstanding what Exclusions do? The Help is rather uninformative.

Plot[Sin[x], {x, 0, 2 π}, ExclusionsStyle -> Red, 
 Exclusions -> {.5 < Sin[x] < .7}]
$\endgroup$
4
  • 1
    $\begingroup$ 1. Did you only read the description at the beginning of document of Exclusions, etc. or even only check ?Exclusions? It's important to read the examples in the document by pressing F1 when using Mathematica. If you check them, you'll probably notice that, what you need is ColorFunction. (A strongly related example can be found in Options section of Plot document. ) 2. Why do you mention python's eval in the title? Is it another question? $\endgroup$
    – xzczd
    Sep 18, 2022 at 3:08
  • $\begingroup$ Exclusions is to exclude singularities and documentation shows only equations, not inequalities, and some strings like "Discontinuities". To exclude part of a graph, you can use the option RegionFunction or ConditionalExpression[Sin[x], Not[.5 < Sin[x] < .7]], but the excluded domain won't be colored. To color part of a graph, you can use MeshFunctions and MeshShading. Can you indicate what you want? $\endgroup$
    – Michael E2
    Sep 18, 2022 at 3:13
  • $\begingroup$ @MichaelE2 Plot[Sin[x], {x, 0, 2 π}, ColorFunction -> Function[{x, y}, If[.5 < y < .7, Red, Automatic]], ColorFunctionScaling -> False], I guess. $\endgroup$
    – xzczd
    Sep 18, 2022 at 3:19
  • $\begingroup$ BTW, python's eval is amount to ToExpression of Mathematica, if I understand the document of eval correctly. $\endgroup$
    – xzczd
    Sep 18, 2022 at 5:49

3 Answers 3

9
$\begingroup$
Plot[Sin[x], {x, 0, 2 π}, Mesh -> {{.5, .7}}, 
 MeshFunctions -> Function[{x, y}, y], 
 MeshShading -> {Automatic, Red, Automatic}, MeshStyle -> Green]

enter image description here

$\endgroup$
8
$\begingroup$
Clear["Global`*"]

Exclusions can be defined by equations but not by inequalities

Plot[Sin[x], {x, 0, 2 π},
 Exclusions -> {Sin[x] == 0.5, Sin[x] == 0.7},
 ExclusionsStyle -> {AbsolutePointSize[6], Red}]

enter image description here

Use ColorFunction and ColorFunctionScaling

Plot[Sin[x], {x, 0, 2 π},
 ColorFunction -> Function[{x, y},
   Piecewise[{{Red, 0.5 < y < 0.7}}, Blue]],
 ColorFunctionScaling -> False]

enter image description here

$\endgroup$
6
$\begingroup$

Another variation could be to plot a regular curve p1 and superimpose the same curve on top of it constrained with a RegionFunction such as p2 as follows.

p1 = Plot[Sin[x]
  , {x, 0, 2 π}
  , PlotStyle -> {Thick, Blue}
  ];

p2 = Plot[Sin[x]
  , {x, 0, 2 π}
  , PlotRange -> All
  , RegionFunction -> Function[{x}, 0.5 < Sin[x] < 0.7]
  , PlotStyle -> {Thick, Red}
  ];

Show[p1, p2]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.