Message Boards Message Boards

0
|
5448 Views
|
2 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Combining Legends

Posted 4 years ago

Hello everybody!

I am trying to create a graph displaying the substitution effects of a change (decrease) in the price of good Y and I am trying to combine the legends for each function. I am somewhat new to Mathematica and have reached a roadblock. It seems like I need to use PlotLegends but I don't understand what I need to do. Here are two versions of my attempts:

f[x_] := 4/x
g[x_] := 9/x
a[x_] := -1 x + 6
b[x_] := -2.25 x + 6
h[x_] := -1 x + 4

pos = {Right, Top};
Show[{Plot[f[x], {x, -6, 15}, PlotStyle -> Blue], 
  Plot[g[x], {x, 0, 15}, PlotStyle -> Green], 
  Plot[a[x], {x, 0, 6}, PlotStyle -> Red], 
  Plot[b[x], {x, 0, 2.667}, PlotStyle -> Red], 
  Plot[h[x], {x, 0, 4.1}, PlotStyle -> Yellow]}, 
 ListPlot[{f[x], g[x], a[x], b[x], h[x]} -> {"\[ScriptCapitalU]", 
    "\[ScriptCapitalU]'", "After Price\[DifferenceDelta]", 
    "Before Price\[DifferenceDelta]", "Hypothetical Budget Line"}, 
  ListPlot[{{1.333, 3}, {2, 2}, {3, 3}} -> {"A", "B", "C"}] , 
  GridLines -> Automatic, PlotRange -> {{0, 10}, {0, 6.5}}, 
  AxesOrigin -> {0, 0}, 
  AxesLabel -> {HoldForm[Good x], HoldForm[Good y]}, 
  PlotLabel -> HoldForm[Price of good y is cheaper], 
  LabelStyle -> {GrayLevel[0], Bold, Italic}]]

Second attempt:

f[x_] := 4/x
g[x_] := 9/x
a[x_] := -1 x + 6
b[x_] := -2.25 x + 6
h[x_] := -1 x + 4

pos = {Right, Top};
Show[{Plot[f[x], {x, -6, 15}, PlotStyle -> Blue, 
   PlotLegends -> "\[ScriptCapitalU]"], 
  Plot[g[x], {x, 0, 15}, PlotStyle -> Green, 
   PlotLegends -> "\[ScriptCapitalU]'"], 
  Plot[a[x], {x, 0, 6}, PlotStyle -> Red, 
   PlotLegends -> "After Price\[DifferenceDelta]"], 
  Plot[b[x], {x, 0, 2.667}, PlotStyle -> Red, 
   PlotLegends -> "Before Price\[DifferenceDelta]"], 
  Plot[h[x], {x, 0, 4.1}, PlotStyle -> Yellow, 
   PlotLegends -> "Hypothetical Budget Line"]}, 
 ListPlot[{{1.333, 3}, {2, 2}, {3, 3}} -> {"A", "B", "C"}] , 
 GridLines -> Automatic, PlotRange -> {{0, 10}, {0, 6.5}}, 
 AxesOrigin -> {0, 0}, 
 AxesLabel -> {HoldForm[Good x], HoldForm[Good y]}, 
 PlotLabel -> HoldForm[Price of good y is cheaper], 
 LabelStyle -> {GrayLevel[0], Bold, Italic}]

Thank you so much for your help!

POSTED BY: Casimir Dahrouch
2 Replies
Posted 4 years ago

Hi Casimir,

Is this what you are trying to do? Yellow does not show well on a white background so I changed it to to Cyan

Show[
 {Plot[f[x], {x, -6, 15}, PlotStyle -> Blue, 
   PlotLegends -> LineLegend[{Blue}, {"\[ScriptCapitalU]"}]],
  Plot[g[x], {x, 0, 15}, PlotStyle -> Green, 
   PlotLegends -> LineLegend[{Green}, {"\[ScriptCapitalU]'"}]],
  Plot[a[x], {x, 0, 6}, PlotStyle -> Red, 
   PlotLegends -> LineLegend[{Red}, {"After Price\[DifferenceDelta]"}]],
  Plot[b[x], {x, 0, 2.667}, PlotStyle -> Red, 
   PlotLegends -> LineLegend[{Red}, {"Before Price\[DifferenceDelta]"}]],
  Plot[h[x], {x, 0, 4.1}, PlotStyle -> Cyan, 
   PlotLegends -> LineLegend[{Cyan}, {"Hypothetical Budget Line"}]],
  ListPlot[{{1.333, 3}, {2, 2}, {3, 3}} -> {"A", "B", "C"}]}, 
 PlotRange -> {{0, 10}, {0, 6.5}},
 GridLines -> Automatic,
 AxesOrigin -> {0, 0},
 FrameLabel -> {"Good x", "Good y"},
 Frame -> True,
 PlotLabel -> "Price of good y is cheaper", 
 LabelStyle -> {GrayLevel[0], Bold, Italic}
 ]

enter image description here

POSTED BY: Rohit Namjoshi

Here is an example of a plot with two curves and legends. I think this is what you are trying to do but I can't be entirely sure. It seems to me you are doing the right thing (i.e. you have PlotLegends within each plot within a Show statement) but I can't figure out where your code is going wrong.

Attachments:
POSTED BY: Joseph Smith
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract