Seaborn FacetGrid legend loses linetype detail when changing lineweight
If I change the linewidth of the plt.plot instance via FacetGrid().map(), I will lose the details of the linetype in the legend. In this figure, the linetype is correctly visible from the chart
However, if I increase the linewidth z no longer displays the linetype correctly
If you want to reproduce the chart, here’s the code
import numpy as np
import seaborn as sbn
import pandas as pd
import matplotlib.pyplot as plt
x = np.tile(np.linspace(0,10,11),2)
y = np.linspace(1,11,11)
z = np.linspace(2,12,11)
yz = np.append(y,z)
y_or_z = np.repeat(['y','z'],11)
df = pd. DataFrame(data={'x':x,'yz':yz,'y_or_z':y_or_z})
g=sbn. FacetGrid(data = df, size=4, aspect=1.2, hue = 'y_or_z', hue_kws=dict(ls=['-','-.']))
g.map(plt.plot,'x','yz', lw=4).add_legend()
g.savefig('ex1')
g=sbn. FacetGrid(data = df, size=4, aspect=1.2, hue = 'y_or_z', hue_kws=dict(ls=['-','-.']))
g.map(plt.plot,'x','yz').add_legend()
g.savefig('ex2')
Solution
You may notice that the orange line in the second figure is shorter than the blue line. So the legend does show the correct linetype, but the length of the legend handle is not long enough to carry the full linetype pattern.
Of course, you can change the handle length so that a full linetype loop fits it, for example
g.add_legend(handlelength=10)
The
handlelength
parameter is recorded in the matplotlib Legend
documentation:
handlelength
: float or None
The length of the legend handles. Measured in font-size units. Default is None which will take the value from the legend.handlelength rcParam.