Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inventory map plot: Missing legend and artifacts from legend setup #3067

Closed
1 task done
megies opened this issue May 16, 2022 · 2 comments
Closed
1 task done

Inventory map plot: Missing legend and artifacts from legend setup #3067

megies opened this issue May 16, 2022 · 2 comments
Labels
bug confirmed bug .imaging Issues with our plotting functionalities release blocker critical issues blocking the corresponding future release
Milestone

Comments

@megies
Copy link
Member

megies commented May 16, 2022

Avoid duplicates

  • I searched existing issues

Bug Summary

With 9659cf3, 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

@@ -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, 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

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

@megies megies added bug confirmed bug .imaging Issues with our plotting functionalities labels May 16, 2022
@megies megies added this to the 1.3.1 milestone May 16, 2022
@megies
Copy link
Member Author

megies commented May 16, 2022

These helper scatter plots should probably be removed like this:

1015             for code, color in sorted(color_per_network.items()):
1016                 ax.scatter([0], [0], size, color, label=code, marker=marker)
             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()

@megies megies added the release blocker critical issues blocking the corresponding future release label May 16, 2022
@trichter
Copy link
Member

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)

@christadler christadler mentioned this issue Jun 7, 2022
11 tasks
@megies megies closed this as completed Aug 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug confirmed bug .imaging Issues with our plotting functionalities release blocker critical issues blocking the corresponding future release
Projects
None yet
Development

No branches or pull requests

2 participants