J-H*_*J-H 7 python plot matplotlib seaborn
我使用factorplot()方法使用Python和Seaborn构建了以下绘图.是否可以使用线条样式作为图例来替换基于右侧线条颜色的图例? 
graycolors = sns.mpl_palette('Greys_r', 4)
g = sns.factorplot(x="k", y="value", hue="class", palette=graycolors, data=df, linestyles=["-", "--"])
Run Code Online (Sandbox Code Playgroud)
此外,我正在尝试使用我的factorplot方法中的color ="black"参数获取黑色两条线,但这会导致异常"factorplot()得到意外的关键字参数'color'".如何以相同的颜色绘制两条线并仅按线型分开?
谢谢
I have been looking for a solution trying to put the linestyle in the legend like matplotlib, but I have not yet found how to do this in seaborn. However, to make the data clear in the legend I have 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()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3275 次 |
| 最近记录: |