Python Seaborn Matplotlib sets line style as legend

I have the following build construct with Python and Seaborn using the factorplot () method. Can a line style be used as a legend to replace a legend based on the color of the line on the right? enter image description here

graycolors = sns.mpl_palette('Greys_r', 4) g = sns.factorplot(x="k", y="value", hue="class", palette=graycolors, data=df, linestyles=["-", "--"]) 

Also, I am trying to get both lines in black using the color = "black" parameter in my factorplot method, but this throws an exception. "factorplot () received an unexpected keyword argument 'color'. How can I draw both lines of the same color and separate them only on linestyle?

thanks

+5
source share
1 answer

I was looking for a solution trying to put linestyle in a legend like matplotlib , but I have not yet found how to do this in the sea. However, to make the data clear in the legend, I used different markers :

 import seaborn as sns import numpy as np import pandas as pd # creating some data n = 11 x = np.linspace(0,2,n) y = np.sin(2*np.pi*x) y2 = np.cos(2*np.pi*x) df=pd.DataFrame({'x':np.append(x, x),'y':np.append(y, y2),'class':np.append(np.repeat('sin',n),np.repeat('cos',n))}) # plot the data with the markers # note that I put the legend=False to move it up (otherwise it was blocking the graph) g=sns.factorplot(x="x",y="y",hue="class",palette=graycolors, data=df, linestyles=["-", "--"],markers=['o','v'], legend=False) # placing the legend up g.axes[0][0].legend(loc=1) #showing graph plt.show() 

graph

+3
source

Source: https://habr.com/ru/post/1247259/


All Articles