Python Seaborn Matplotlib 设置线型为图例

标签 python matplotlib plot seaborn

我使用 factorplot() 方法用 seaborn 构建了以下绘图。

是否可以使用线条样式作为图例来替换右侧基于线条颜色的图例? 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=["-", "--"])

此外,我试图在我的 factorplot 方法中使用 color="black" 参数使两条线都变成黑色,但这会导致异常“factorplot() 得到了一个意外的关键字参数”颜色'”。如何用相同的颜色绘制两条线并仅通过线型将它们分开?

最佳答案

我一直在寻找一种解决方案,试图将线型放入图例中,例如 matplotlib ,但我还没有找到如何在 seaborn 中执行此操作。但是,为了使图例中的数据清晰,我使用了不同的 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)
data = {'x': np.append(x, x), 'y': np.append(y, y2),
        'class': np.append(np.repeat('sin', n), np.repeat('cos', n))}
df = pd.DataFrame(data)

# 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()

the graph

关于Python Seaborn Matplotlib 设置线型为图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36663613/

相关文章:

r - 在 R 中使用 fitdistrplus 正确显示 4 面板分布图的图例

r - ggplot 升级后颜色和图例丢失

python - 如何使用 python OpenCV 缩放图像中字符的粗细?

python - 准备好列 Numpy/Matplotlib

python - 如何查找用户是否输入了主机名或 IP 地址?

python - 在 QTableView 中嵌入控件?

python-3.x - 在 matplotlib 图形中插入 png 图像

python - 如何使用matplotlib根据条形高度绘制具有特定颜色的3D条形图

python - 绘制椭圆轨道

python - 将辅助数据绘制为第二个 y 轴上的刻度