2

I am new to this, trying to plot 2 subfigures horizontal with sample data using Matplotlib and Seaborn library in Jupyter Notebook, the 2 sub-charts come out alright, but the 2 legends overlap each other on the far right hand. I couldn't figure out a way to move legend of the 1st sub-figure to the left. Any suggestions are greatly appreciated!

screenshot

import matplotlib as mpl
tips = sns.load_dataset("tips")

f = mpl.figure.Figure(figsize=(8, 2), dpi=100)
sf1, sf2 = f.subfigures(1, 2)

(
    so.Plot(tips, "total_bill", "tip", color="day")
    .facet(col="day")
    .add(so.Dot(color="#aabc"), col=None, color=None)
    .add(so.Dot())
).on(sf1).plot()

(
    so.Plot(tips, "total_bill", "tip")
    .add(so.Dot(color="#aabc"))
    .add(so.Dot(), data=tips.query("size == 1"), color="time")
).on(sf2).plot()
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
user21559775
  • 101
  • 4

2 Answers2

2

This is super clunky, probably because handling legends in the seaborn.objects interface is a work in progress but this seems to get close to what you want, I think?

In a first cell use an example from the posted solution there.

import seaborn as sns
import matplotlib.pyplot as plt
import seaborn.objects as so
tips = sns.load_dataset("tips")
f, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)

(
     so.Plot(tips, x='total_bill', y='tip', color='day')
     .add(so.Dot(pointsize=2))
     .add(so.Line(color='k'), so.PolyFit(order=1), color=None)
     .on(ax1)
     .plot()
)
legend = f.legends.pop(0)
ax1.legend(legend.legendHandles, [t.get_text() for t in legend.texts]);

enter image description here

(I know that code and plot above has nothing to do with what you want; however, it imports something or sets up something that I couldn't get the code I thought should be all you need (below) to do on it's own. Needing that unrelated code and plot is why I say it's super clunky. But I couldn't get it to work without it, and it worked fairly well when I had it there and so I'm sharing. Maybe it will factor into figuring out a cleaner solution.)

In the next cell, put this variation of your code:

import seaborn as sns
import seaborn.objects as so
import matplotlib as mpl
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")

f = mpl.figure.Figure(figsize=(8, 2), dpi=100)
sf1, sf2 = f.subfigures(1, 2)

(
    so.Plot(tips, "total_bill", "tip", color="day")
    .facet(col="day")
    .add(so.Dot(color="#aabc"), col=None, color=None)
    .add(so.Dot())
    .on(sf1)
    .plot()
)

(
    so.Plot(tips, "total_bill", "tip")
    .add(so.Dot(color="#aabc"))
    .add(so.Dot(), data=tips.query("size == 1"), color="time")
    .on(sf2)
    .plot()
)
legend = f.legends.pop(1)
f.legends.pop(0) # don't need the 'day' legend showing, so use this
sf2.legend(legend.legendHandles, [t.get_text() for t in legend.texts])
f.figure

Run that and it should look a little better without the two legends overlapping on the far right side.

I didn't see why you need the 'day' legend showing because that is clearly handled in the plots on the left panel , and so I used the pop method to remove it.

enter image description here

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Wayne
  • 6,607
  • 8
  • 36
  • 93
0

Slight change in how the legends are assigned to each subfigure

import seaborn as sns
import seaborn.objects as so
import matplotlib as mpl
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")

f = mpl.figure.Figure(figsize=(8, 2), dpi=100)
sf1, sf2 = f.subfigures(1, 2)

(
    so.Plot(tips, "total_bill", "tip", color="day")
    .facet(col="day")
    .add(so.Dot(color="#aabc"), col=None, color=None)
    .add(so.Dot())
    .on(sf1)
    .plot()
)

(
    so.Plot(tips, "total_bill", "tip")
    .add(so.Dot(color="#aabc"))
    .add(so.Dot(), data=tips.query("size == 1"), color="time")
    .on(sf2)
    .plot()
)

# remove the legends from the primary figure
l2 = f.legends.pop(1)
l1 = f.legends.pop(0)

# add the legend properties to the appropriate subfigure
sf1.legend(l1.legend_handles, [t.get_text() for t in l1.texts])
sf2.legend(l2.legend_handles, [t.get_text() for t in l2.texts])

f.figure

enter image description here

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
user21559775
  • 101
  • 4