Troubleshooting 'PlotLegend not showing' in Plot Function: Solutions & Examples

  • Mathematica
  • Thread starter Ishika_96_sparkles
  • Start date
  • Tags
    Quantum
In summary, p1, p2 and p3 have different plots with overlapping ranges, PlotLegend is missing in p3, and Jedishrfu found a way to add the PlotLegend automatically.
  • #1
Ishika_96_sparkles
57
22
TL;DR Summary
I plotted a few curves for the wavefunction of Quantum Harmonic oscillator problem with certain energy values. The plotlegends do not seem to work in the final output.
Here, are the parts of the plot function
plot I:
p1 = Plot[Normal[g*Exp[-x^2/2] /. solute[[1]] /. en -> 3], {x, -8, 8},
    PlotStyle -> {Dashed, Gray}, PlotLegends -> Automatic];
(* Here, [B]g[/B]=1 - x^2 - x^4/6 - x^6/30 - x^8/168 - x^10/1080 , energy [B]en[/B] =3 and solute is the series solution of [B]g[/B] with unknown coefficients*)
plot II:
p2 = Plot[
   Normal[g*Exp[-x^2/2] /. solute[[1]] /. en -> 10], {x, -8, 8},
   PlotStyle -> {Dashed, Red}, PlotLegends -> Automatic];

(* Here, g=1 - (9 x^2)/2 + (15 x^4)/8 - x^6/16 - (3 x^8)/896 - x^10/3840 *)

plot III:
p3 = Plot[
   Normal[g*Exp[-x^2/2] /. solute[[1]] /. en -> 17], {x, -8, 8},
   PlotStyle -> {Thick, Black}, PlotLegends -> Automatic];

(* Here, g= 1 - 8 x^2 + 8 x^4 - (32 x^6)/15 + (16 x^8)/105 *)

and then the SHOW command as follows

Show command:
Show[{p1, p2, p3}, PlotRange -> All,
 LabelStyle -> Directive[Blue, Italic, 12]]

What did I do wrong, here?

The plots with no legends for the curves.
 
Physics news on Phys.org
  • #2
Try plotting one at a time and see if the plot legends appear then add one plot and try again.

If they appear on the first plot but not the 2nd or 3rd then you know where your error might be.

You could also try plotting them in reverse order.

The basic idea is to see if its the way you specified plot legends in each plot call vs a mistake in a plot that somehow hides the plot legend.

Also have you searched via google to see if others have had the same issue?

Here's what I found about plotlegends:

https://reference.wolfram.com/language/PlotLegends/ref/PlotLegend.html

In one example they do two plots in one call so maybe that's what you have to do.
 
  • #3
Thanks @jedishrfu
I shall try your suggestion and see if it works and then post the working solution here.
 
  • #4
So, this is the way i have learned to do it for the SHOW command
(I checked the plots individually and the PlotLegend worked fine. However, I found the following way to be more clean than filling the plot command with most of the attributes.)

Use of Legended:
Legended[Show[p1, p2, p3, PlotRange -> All],
 Placed[LineLegend[{Gray, Red,
    Green}, {"\!\(\*SubscriptBox[\(\[Psi]\), \(2\)]\)(x)",
    "\!\(\*SubscriptBox[\(\[Psi]\), \(6\)]\)(x)",
    "\!\(\*SubscriptBox[\(\[Psi]\), \(8\)]\)(x)"}], {.9, .8}]]

The numbers in the curly brackets are the position coordinates {x,y} of the placement of the legend for the curve. The output is given below. Thanks a lot for the help!

QM_prac-1b.jpg
 
  • Like
Likes jedishrfu
  • #5
Thanks for your analysis, solution and insight. It’s always great to see that in a thread And will help future readers who stumble across it in their search for answers.
 
  • Like
Likes Ishika_96_sparkles

FAQ: Troubleshooting 'PlotLegend not showing' in Plot Function: Solutions & Examples

Why is my PlotLegend not showing up in my plot?

There could be several reasons for this issue. One common reason is that the PlotLegend is not included in the list of items to be plotted. Another reason could be that the PlotLegend is not formatted correctly, such as missing quotation marks or incorrect syntax. It is also possible that the PlotLegend is being hidden behind other elements in the plot.

How do I fix my PlotLegend not showing up in my plot?

To fix this issue, first check that the PlotLegend is included in the list of items to be plotted. If it is not, add it to the list. If the PlotLegend is included but still not showing up, check the formatting to ensure it is correct. You can also try adjusting the placement of the PlotLegend in the plot, such as moving it to a different corner or increasing its size.

Can I customize the appearance of my PlotLegend?

Yes, you can customize the appearance of your PlotLegend by using options in the PlotLegend function. For example, you can change the legend's position, font size, color, and background color. You can also add a title to your PlotLegend.

My PlotLegend is showing up, but it is not displaying the correct labels. How can I fix this?

This issue is most likely caused by incorrect labeling or formatting in the PlotLegend function. Double check that the labels in the PlotLegend function match the labels of the items being plotted. You can also try manually specifying the labels in the PlotLegend function to ensure they are correct.

I am using a third-party package for my plot, and my PlotLegend is still not showing up. What should I do?

If you are using a third-party package, it is possible that the package has its own syntax for adding a PlotLegend. Check the documentation for the package to see if there are any specific instructions for adding a PlotLegend. If you are still having issues, you can try reaching out to the developer of the package for assistance.

Similar threads

Replies
1
Views
812
Replies
2
Views
2K
Replies
6
Views
2K
Replies
5
Views
4K
Replies
4
Views
2K
Replies
2
Views
1K
Replies
3
Views
4K
Replies
1
Views
6K
Back
Top