Repository: obspy/obspy

Issue:
Inventory map plot: Missing legend and artifacts from legend setup
### Avoid duplicates

- [X] I searched existing issues

### Bug Summary

With 9659cf3e4499fe15fa71bd0bd3a9600aca7f180e, while fixing the docs build apparently, two small bugs were introduced.

 a) Legend is removed from the plot, right after adding it
 b) Helper artists (bogus scatter plots), used to set up the legend are not removed anymore, ending up in markers lingering at 0°N 0°E

```diff
@@ -1010,20 +1013,20 @@ class Inventory(ComparingObject):
 
         if legend is not None and color_per_network:
             ax = fig.axes[0]
-            count = len(ax.collections)
             for code, color in sorted(color_per_network.items()):
                 ax.scatter([0], [0], size, color, label=code, marker=marker)
             # workaround for older matplotlib versions
             try:
-                ax.legend(loc=legend, fancybox=True, scatterpoints=1,
-                          fontsize="medium", markerscale=0.8,
-                          handletextpad=0.1)
+                leg = ax.legend(loc=legend, fancybox=True, scatterpoints=1,
+                                fontsize="medium", markerscale=0.8,
+                                handletextpad=0.1)
+                leg.remove()
             except TypeError:
                 leg_ = ax.legend(loc=legend, fancybox=True, scatterpoints=1,
                                  markerscale=0.8, handletextpad=0.1)
-                leg_.prop.set_size("medium")
+                leg_.remove()
             # remove collections again solely created for legend handles
-            ax.collections = ax.collections[:count]
+            # ax.collections = ax.collections[:count]
 
         if outfile:
             fig.savefig(outfile)
```
 a) is trivial, just get rid of these remove calls again
 b) currently calls like `.pop()` and `.remove()` work on `ax.collections`, but there's a [note that with matplotlib 3.7 this will get changed to being immutable](https://github.com/matplotlib/matplotlib/blob/c6c7ec1978c22ae2c704555a873d0ec6e1e2eaa8/lib/matplotlib/axes/_base.py#L1327-L1336), so this needs to be done differently and correctly right now


---

In addition, this part also shows a warning:

```
*c* argument looks like a single numeric RGB or RGBA sequence, which should be avoided as value-mapping will have precedence in case its length matches with *x* & *y*.  Please use the *color* keyword-argument or provide a 2D array with a single row if you intend to specify the same RGB or RGBA value for all points.
```

This simply needs to be changed to `color=color`:

```
ax.scatter([0], [0], size, color, label=code, marker=marker)
```

### Code to Reproduce

```python
from obspy import read_inventory

inv = read_inventory()
inv.plot(color_per_network=True, projection='global')
```


### Error Traceback

_No response_

### ObsPy Version?

master

### Operating System?

_No response_

### Python Version?

_No response_

### Installation Method?

developer installation / from source / git checkout


Discussion:
These helper scatter plots should probably be removed like this:

```python
1015             for code, color in sorted(color_per_network.items()):
1016                 ax.scatter([0], [0], size, color, label=code, marker=marker)
```

```python
             helpers = []
             for code, color in sorted(color_per_network.items()):
                 helpers.append(ax.scatter([0], [0], size, color, label=code, marker=marker))
...
             for helper in helpers:
                 helper.remove()
```
Could we use something like this (adapted from mpl gallery)?

```
from matplotlib.lines import Line2D

legend_elements = [
    Line2D([0], [0], marker=marker, color=color, label=code, markersize=size)
    for code, color in sorted(color_per_network.items())]
ax.legend(handles=legend_elements)
```

Resolving patch:
diff --git a/obspy/core/inventory/inventory.py b/obspy/core/inventory/inventory.py
--- a/obspy/core/inventory/inventory.py
+++ b/obspy/core/inventory/inventory.py
@@ -964,6 +964,7 @@ def plot(self, projection='global', resolution='l',
         """
         from obspy.imaging.maps import plot_map
         import matplotlib.pyplot as plt
+        from matplotlib.lines import Line2D
 
         # The empty ones must be kept as otherwise inventory files without
         # channels will end up with nothing.
@@ -1012,20 +1013,13 @@ def plot(self, projection='global', resolution='l',
 
         if legend is not None and color_per_network:
             ax = fig.axes[0]
-            for code, color in sorted(color_per_network.items()):
-                ax.scatter([0], [0], size, color, label=code, marker=marker)
-            # workaround for older matplotlib versions
-            try:
-                leg = ax.legend(loc=legend, fancybox=True, scatterpoints=1,
-                                fontsize="medium", markerscale=0.8,
-                                handletextpad=0.1)
-                leg.remove()
-            except TypeError:
-                leg_ = ax.legend(loc=legend, fancybox=True, scatterpoints=1,
-                                 markerscale=0.8, handletextpad=0.1)
-                leg_.remove()
-            # remove collections again solely created for legend handles
-            # ax.collections = ax.collections[:count]
+            legend_elements = [
+                Line2D([0], [0], marker=marker, color=color, label=code,
+                       markersize=size ** 0.5, ls='')
+                for code, color in sorted(color_per_network.items())]
+            ax.legend(handles=legend_elements, loc=legend, fancybox=True,
+                      scatterpoints=1, fontsize="medium", markerscale=0.8,
+                      handletextpad=0.1)
 
         if outfile:
             fig.savefig(outfile)


Test patch: