Alxander
Alxander

Reputation: 323

Seaborn FacetGrid legend loses linestyle details when changing linewidth

If I change the linewidth of a plt.plot instance through FacetGrid().map() I lose the details of the linestyle in the legend. In this figure the linestyles are properly visible from the graph Example 2

However, if I increase the linewidth z no longer properly shows the linestyle

Example 1

Here is the code if you want to reproduce the graphs,

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')

Upvotes: 3

Views: 937

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339340

You may notice that the orange line in the second plot is shorter than the blue line. So the legend does show the correct line style, but the legend handle is not long enough to host a complete linestyle pattern.

You may of course change the handle length such that a complete linestyle cycle fits into it, e.g.

g.add_legend(handlelength=10) 

enter image description here

The handlelength argument is documented 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.

Upvotes: 3

Related Questions