Repository: PlasmaPy/PlasmaPy

Issue:
Add legends to Langmuir plot examples
The plots [here](http://docs.plasmapy.org/en/stable/auto_examples/plot_langmuir_analysis.html#sphx-glr-auto-examples-plot-langmuir-analysis-py) could use some nice legends.


Discussion:
I tried this in a local cloned version by executing examples/plot_langmuir_analysis.py, post making edits in the plotting. Even with/without those edits I am not able to see the plots. However it does print the required details when swept_probe_analysis() is called.
Also, I tried looking up for auto_examples under the docs, but it is set to be ignored via the .gitignore file. Further, despite having astropy v3.0.2 initially, I didn't get any error like #467. Can anybody help out why I can't see any plots? @ritiek?
> Further, despite having astropy v3.0.2 initially, I didn't get any error like #467.

By any chance you're using astropy v3.0.3? It seems like it got fixed it the newer version but I was able to still reproduce it with astropy v3.0.2.

> I tried this in a local cloned version by executing examples/plot_langmuir_analysis.py, post making edits in the plotting. Even with/without those edits I am not able to see the plots.

I don't know what's the agreed way to show them when executing them from the terminal but adding these lines at the end (after the code to plot the graph) seems to work for me.
```python
>>> import matplotlib.pyplot as plt
>>> plt.show()
```

If you're using jupyter notebook, you should be able to see them by adding
```python
%matplotlib inline
```
at the top of your code.
I checked it again. It was v3.0.2 only. Previously, the function call never went to actually plot it, hence o error. I did get the said error after that. 
Thanks for pointing out the plt.show(), I'll create a PR for this issue soon.
> I checked it again. It was v3.0.2 only.

That's weird but I think it is actually a good thing that it doesn't error out. :D

> Thanks for pointing out the plt.show(), I'll create a PR for this issue soon.

You're welcome. That said, we'll still need to confirm what's the preferred way to show plots. Maybe @StanczakDominik or @jasperbeckers who wrote the code?

Resolving patch:
diff --git a/plasmapy/diagnostics/langmuir.py b/plasmapy/diagnostics/langmuir.py
--- a/plasmapy/diagnostics/langmuir.py
+++ b/plasmapy/diagnostics/langmuir.py
@@ -350,13 +350,15 @@ def swept_probe_analysis(probe_characteristic, probe_area, gas_argument,
             fig, (ax1, ax2) = plt.subplots(2, 1)
             ax1.plot(probe_characteristic.bias,
                      probe_characteristic.current,
-                     marker='.', color='k', linestyle='')
+                     marker='.', color='k', linestyle='',
+                     label="Probe current")
             ax1.set_title("Probe characteristic")
             ax2.set_ylim(probe_characteristic.get_padded_limit(0.1))
 
             ax2.plot(probe_characteristic.bias,
                      np.abs(probe_characteristic.current),
-                     marker='.', color='k', linestyle='')
+                     marker='.', color='k', linestyle='',
+                     label="Probe current")
             ax2.set_title("Logarithmic")
             ax2.set_ylim(probe_characteristic.get_padded_limit(0.1, log=True))
 
@@ -364,8 +366,9 @@ def swept_probe_analysis(probe_characteristic, probe_area, gas_argument,
             ax1.axhline(y=I_es.value, color='grey', linestyle='--')
             ax1.axvline(x=V_F.value, color='k', linestyle='--')
             ax1.axhline(y=I_is.value, color='r', linestyle='--')
-            ax1.plot(ion_current.bias, ion_current.current, c='y')
-            ax1.plot(electron_current.bias, electron_current.current, c='c')
+            ax1.plot(ion_current.bias, ion_current.current, c='y', label="Ion current")
+            ax1.plot(electron_current.bias, electron_current.current,
+                     c='c', label="Electron current")
             tot_current = ion_current + electron_current
             ax1.plot(tot_current.bias, tot_current.current, c='g')
 
@@ -373,11 +376,14 @@ def swept_probe_analysis(probe_characteristic, probe_area, gas_argument,
             ax2.axhline(y=I_es.value, color='grey', linestyle='--')
             ax2.axvline(x=V_F.value, color='k', linestyle='--')
             ax2.axhline(y=np.abs(I_is.value), color='r', linestyle='--')
-            ax2.plot(ion_current.bias, np.abs(ion_current.current), c='y')
+            ax2.plot(ion_current.bias, np.abs(ion_current.current),
+                     label="Ion current", c='y')
             ax2.plot(electron_current.bias, np.abs(electron_current.current),
-                     c='c')
+                     label="Electron current", c='c')
             ax2.plot(tot_current.bias, np.abs(tot_current.current), c='g')
             ax2.set_yscale("log", nonposy='clip')
+            ax1.legend(loc='best')
+            ax2.legend(loc='best')
 
             fig.tight_layout()
 
@@ -869,7 +875,8 @@ def get_electron_temperature(exponential_section, bimaxwellian=False,
             plt.scatter(exponential_section.bias.to(u.V),
                         np.log(exponential_section.current.to(u.A).value),
                         color='k',
-                        marker='.')
+                        marker='.',
+                        label="Exponential section")
 
             if bimaxwellian:
                 plt.scatter(x0, y0, marker='o', c='g')
@@ -878,15 +885,19 @@ def get_electron_temperature(exponential_section, bimaxwellian=False,
                              exponential_section.bias.to(u.V).value,
                              fit[0], fit[1], fit[2] + fit[3]),
                          c='g',
-                         linestyle='--')
+                         linestyle='--',
+                         label="Bimaxwellian exponential section fit",
+                         )
 
             plt.plot(exponential_section.bias.to(u.V),
                      fit_func(exponential_section.bias.to(u.V).value,
                      *fit),
+                     label="Exponential fit",
                      c='g')
 
             plt.ylabel("Logarithmic current")
             plt.title("Exponential fit")
+            plt.legend(loc='best')
             plt.tight_layout()
 
     k = [T_e]
@@ -955,9 +966,12 @@ def extrapolate_electron_current(probe_characteristic, fit,
             plt.figure()
             plt.scatter(probe_characteristic.bias,
                         probe_characteristic.current.to(u.mA), marker='.',
+                        label="Probe characteristic",
                         c='k')
             plt.plot(electron_characteristic.bias,
-                     electron_characteristic.current.to(u.mA))
+                     electron_characteristic.current.to(u.mA),
+                     label="Estimated electron characteristic")
+            plt.legend()
 
     return electron_characteristic
 
@@ -1221,5 +1235,6 @@ def get_EEDF(probe_characteristic, visualize=False):
             plt.title("Electron Energy Distribution Function")
             plt.xlabel("Energy (eV)")
             plt.ylabel("Probability")
+            plt.grid()
 
     return energy, probability
diff --git a/plasmapy/examples/plot_langmuir_analysis.py b/plasmapy/examples/plot_langmuir_analysis.py
--- a/plasmapy/examples/plot_langmuir_analysis.py
+++ b/plasmapy/examples/plot_langmuir_analysis.py
@@ -9,9 +9,11 @@
 """
 
 from plasmapy.diagnostics.langmuir import Characteristic, swept_probe_analysis
+import matplotlib.pyplot as plt
 import astropy.units as u
 import numpy as np
 import os
+from pprint import pprint
 
 ######################################################
 # The first characteristic we analyze is a simple single-probe measurement in
@@ -25,8 +27,8 @@
 bias, current = np.load(path)
 
 # Create the Characteristic object, taking into account the correct units
-characteristic = Characteristic(np.array(bias) * u.V,
-                                np.array(current)*1e3 * u.mA)
+characteristic = Characteristic(u.Quantity(bias,  u.V),
+                                u.Quantity(current, u.A))
 
 # Calculate the cylindrical probe surface area
 probe_length = 1.145 * u.mm
@@ -38,7 +40,7 @@
 # Now we can actually perform the analysis. Since the plasma is in Helium an
 # ion mass number of 4 is entered. The results are visualized and the obtained
 # EEDF is also shown.
-print(swept_probe_analysis(characteristic,
+pprint(swept_probe_analysis(characteristic,
                            probe_area, 'He-4+',
                            visualize=True,
                            plot_EEDF=True))
@@ -55,8 +57,8 @@
 # Load the data from a file and create the Characteristic object
 path = os.path.join("langmuir_samples", "Pace2015.npy")
 bias, current = np.load(path)
-characteristic = Characteristic(np.array(bias) * u.V,
-                                np.array(current) * 1e3 * u.mA)
+characteristic = Characteristic(u.Quantity(bias, u.V),
+                                u.Quantity(current, u.A))
 
 ######################################################
 # Initially the electrons are assumed to be Maxwellian. To check this the fit
@@ -71,7 +73,7 @@
 # It can be seen that this plasma is slightly bi-Maxwellian, as there are two
 # distinct slopes in the exponential section. The analysis is now performed
 # with bimaxwellian set to True, which yields improved results.
-print(swept_probe_analysis(characteristic,
+pprint(swept_probe_analysis(characteristic,
                            0.738 * u.cm**2,
                            'Ar-40 1+',
                            bimaxwellian=True,
@@ -93,8 +95,8 @@
 # Import probe data and calculate probe surface area.
 path = os.path.join("langmuir_samples", "Beckers2017b.npy")
 bias, current = np.load(path)
-characteristic = Characteristic(np.array(bias) * u.V,
-                                np.array(current) * 1e3 * u.mA)
+characteristic = Characteristic(u.Quantity(bias, u.V),
+                                u.Quantity(current, u.A))
 probe_length = 1.145 * u.mm
 probe_diameter = 1.57 * u.mm
 probe_area = (probe_length * np.pi * probe_diameter +
@@ -103,7 +105,7 @@
 ######################################################
 # `plot_electron_fit` is set to True to check the bi-Maxwellian properties.
 # The fit converges nicely to the two slopes of the electron growth region.
-print(swept_probe_analysis(characteristic,
+pprint(swept_probe_analysis(characteristic,
                            probe_area,
                            'He-4+',
                            bimaxwellian=True,


Test patch: