2
$\begingroup$

This question already has an answer here:

I would like to shade the area between a function, and a line, but only over a specific range. This range is smaller than the range of the entire plot. The example code and plot below make my goal more clear.

R4 = 8.3144621;(*m^3 Pa K^-1mol^-1 *)
pvDw[T_, V_, Rr_, a_, b_] = 
p /. Solve[(p + a/V^2) (V - b) == Rr T, p] // First

aAr = 0.1355; (*a*)
bAr = .000032;(*b*)
TAr = 130;
guesspv = 2.6*10^6;
Show[Plot[
  Evaluate[pvDw[TAr, Vm, R4, aAr, 
    bAr], {Vm, (3.2*10^-5), (1.0*10^-3)}], 
  PlotStyle -> {Thick, Black}, PlotRange -> Automatic, 
  Filling -> {1 -> {guesspv, Yellow}}]]

This code generates this figure

plot

I would like to be able to show the shading only in the 2nd and third regions not the first and fouth. How would I go about doing this?

I realize that using the Filling option may not be the best way to do this also, so I am open to hearing about other options, plot types ect. I used Filling in the title because that is what I am using in my flawed solution.

$\endgroup$

marked as duplicate by Mr.Wizard Oct 18 '14 at 18:11

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

5
$\begingroup$
{min, max} = {Min@#, Max@#} &@(Last @@@ NSolve[pvDw[TAr, Vm, R4, aAr, bAr] == guesspv, {Vm}]); 

Plot[{ConditionalExpression[guesspv, min < Vm < max], 
      Evaluate[pvDw[TAr, Vm, R4, aAr, bAr]]}, {Vm, (3.2*10^-5), (1.0*10^-3)},
 PlotStyle -> {Directive[{Thin, Yellow}], Directive[{Thick, Black}]},
 PlotRange -> Automatic, Filling -> {1 -> {{2}, Yellow}}]

enter image description here

Explanatory notes:

Find the intersection of the curve with guesspv:

soln = NSolve[pvDw[TAr, Vm, R4, aAr, bAr] == guesspv, {Vm}] ;
(* {{Vm -> 0.000285782},{Vm -> 0.000107816},{Vm -> 0. 0000541249}} *)

See Apply:

intersections = Last @@@ soln   (* same as  Vm /. soln  to get the RHSs of ->s*)
(* {0.000285782,0.000107816,0.0000541249}

See Prefix, Slot and Function:

Min@intersections (* same as Min[intersections]*)
(* 0.0000541249 *)
Max@intersections (* same as Max[intersections]*)
(* 0.000285782 *)

Define a function to do the previous two steps in a single step:

function = {Min@#, Max@#} &  ;(* same as {Min[#], Max[#]}& *)
function@intersections (* same as function[intersections] *)
(* {0.0000541249,0.000285782} *)
$\endgroup$
  • $\begingroup$ Thank you, Could you explain the syntax you use on the first line of your answer, or provide a reference where I could figure it out? $\endgroup$ – Ajay Oct 15 '14 at 23:06
  • $\begingroup$ I use ConditionalExpression to define function that takes the value guesspv if Vm falls in regions 2 and 3 (that is, if min < Vm < max, where min and max are the smallest and largest Vm values where your curve intersects the line at guesspv - these are obtained using Solve), the value Indeterminate otherwise. We plot the two functions in a single plot. $\endgroup$ – kglr Oct 15 '14 at 23:16
  • $\begingroup$ ... Filling->{1->{{2},Yellow} says use Yellow filling between the first and second functions in the list of functions in Plots first argument (see Filling for more examples. $\endgroup$ – kglr Oct 15 '14 at 23:17
  • $\begingroup$ I am sorry I meant specifically the line where you define Min and max, for example what does @# mean in the line {min, max} = {Min@#, Max@#} &@(Last @@@ NSolve[pvDw[TAr, Vm, R4, aAr, bAr] == guesspv, {Vm}]); ? $\endgroup$ – Ajay Oct 15 '14 at 23:26
  • $\begingroup$ Oh I see.. give me a few minutes to add some references. $\endgroup$ – kglr Oct 15 '14 at 23:35
2
$\begingroup$

You can do this rather easily using Show. Here is a simple example.

f[x_] := x^2;
plot1 = Plot[f[x], {x, 0, 1}];
plot2 = Plot[f[x], {x, 1/3, 2/3}, Filling -> 1/3];
Show[plot1, plot2]

Mathematica graphics

$\endgroup$
2
$\begingroup$

Another way is to show two plots one with filling and one without.

{min, max} = {Min@#, Max@#} &@(Last @@@ 
     NSolve[pvDw[TAr, Vm, R4, aAr, bAr] == guesspv, {Vm}]);
Show[Plot[
  Evaluate[pvDw[TAr, Vm, R4, aAr, 
    bAr]], {Vm, (3.2*10^-5), (1.0*10^-3)}, 
  PlotStyle -> {Thick, Black}, PlotRange -> Automatic], 
 Plot[Evaluate[pvDw[TAr, Vm, R4, aAr, bAr]], {Vm, min, max}, 
  PlotStyle -> {Thick, Black}, PlotRange -> Automatic, 
  Filling -> {1 -> {guesspv, Yellow}}]]

Or you can do it like this:

Plot[Evaluate[
   pvDw[TAr, Vm, R4, aAr, bAr], {Vm, (3.2*10^-5), (1.0*10^-3)}], 
  PlotStyle -> {Thick, Black}, PlotRange -> Automatic, 
  Filling -> {1 -> {guesspv, Yellow}}] /. 
 GraphicsGroup[{Polygon[__], Polygon[y__]}] :> 
  GraphicsGroup[{Polygon[y]}]

enter image description here

$\endgroup$

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