I am creating a figure (with the seaborn.objects
interface) that requires a legend with both colors and markers. I am trying to move the legend that is generated, but I am having trouble moving the marker section. See the example below:
The example is intended to be run in a notebook environment. Add plt.show()
to display figures when running as a Python file.
import matplotlib.pyplot as plt
import seaborn as sns
import seaborn.objects as so
mpg = sns.load_dataset("mpg")
f, axs = plt.subplots(1, 2, figsize=(8, 4))
_ = (
so.Plot(mpg, "weight", "acceleration", color="cylinders")
.add(so.Dot(), marker="origin")
.scale(
color=["#49b", "#a6a", "#5b8", "#128", "#332"],
marker={"japan": ".", "europe": "+", "usa": "*"},
)
.on(axs[0])
.plot()
)
I am trying to move the legend from far-right of plot to right-side of subplot. The answer to How can I customize the legend with Seaborn 0.12 objects? is only a partial solution in this case. Only part of the legend is moved successfully. I wonder why legend_handles
doesn't catch the 2nd part of legend...
l1 = f.legends.pop(0)
axs[0].legend(l1.legend_handles, [t.get_text() for t in l1.texts])
How can all parts of the legend be moved?