【问题标题】:How to control which line gets what marker in a seaborn multi-line plot?如何控制在 seaborn 多线图中哪条线得到什么标记?
【发布时间】:2020-07-02 15:25:31
【问题描述】:

我无法控制哪个数据类别在 seaborn 的多线图中获取什么标记。为了证明我在这里尝试做的是一个人为的例子 -

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

sns.set(style="ticks")
sns.set_context("paper", rc={"font.size":8,"axes.titlesize":20,"axes.labelsize":15})
fig, ax = plt.subplots()

df = pd.DataFrame({'A': [1,2,3,4,1,2,3,4,1,2,3,4], 'B': ['a', 'a', 'a', 'a', 'b', 'b', 'b', 'b', 'c', 'c', 'c', 'c'], 'C': [1,1,3,4,1,1,3,3,1,1,2,3]})

sns.lineplot(x='A', y='C', hue='B', hue_order=['c', 'b', 'a'], markers=[{'c': 'X', 'b': 's', 'a': 'o'}], palette={'a': 'Orange', 'b': 'Green', 'c': 'Red'}, dashes=False, data=df, markers=True, ax=ax)

plt.title('A Plot')
ax.set(xlabel='Time', ylabel='#')
ax.legend(title='Legend', loc='upper left', labels=['A', 'B', 'C'])
ax.set(xticks=df['A'].values)
plt.gca().set_position([0, 0, 1, 1])
plt.show()

但图像渲染时没有任何标记 -

当我执行style='B' 时,我收到以下错误 -

ValueError: These `style` levels are missing markers: {'b', 'c'}

我尝试了style_order=['a', 'b', 'c'],但我得到了与上面相同的情节。

我如何真正获得具有正确情节线的标记?

【问题讨论】:

    标签: python python-3.x matplotlib plot seaborn


    【解决方案1】:

    您必须同时提供hue="B"style="B",然后向markers={...} 提供字典(实际上您在代码中传递了一个包含字典的列表)。

    sns.lineplot(x='A', y='C', hue='B', style='B', data=df, hue_order=['c', 'b', 'a'], markers={'c': 'X', 'b': 's', 'a': 'o'}, dashes=False, palette={'a': 'Orange', 'b': 'Green', 'c': 'Red'})
    

    【讨论】:

    • 您是否知道如何减少图像中标记周围的空白/空白填充。因为当我将情节保存为大图像时,它非常重要。请让我知道或指导参考。
    • 啊,不用担心,我明白了 - kwargs = {'markeredgewidth': 0.25}
    猜你喜欢
    • 2020-11-03
    • 2020-07-01
    • 1970-01-01
    • 2021-01-15
    • 1970-01-01
    • 2021-04-26
    • 1970-01-01
    • 2016-12-03
    • 1970-01-01
    相关资源
    最近更新 更多