Python 如何使用tsplot设置多个标记?

Python 如何使用tsplot设置多个标记?,python,matplotlib,pandas,seaborn,Python,Matplotlib,Pandas,Seaborn,我有一个熊猫数据框中的时间序列数据,我想为这些行设置单独的标记。到目前为止,通过使用marker='o'参数,我只为两行使用了相同的标记 我使用的是来自的示例,我复制了我的副本并粘贴了下面的代码 如何为每条线绘制单独的标记 import numpy as np np.random.seed(9221999) import pandas as pd from scipy import stats import matplotlib.pyplot as plt import seaborn as s

我有一个熊猫数据框中的时间序列数据,我想为这些行设置单独的标记。到目前为止,通过使用
marker='o'
参数,我只为两行使用了相同的标记

我使用的是来自的示例,我复制了我的副本并粘贴了下面的代码

如何为每条线绘制单独的标记

import numpy as np
np.random.seed(9221999)
import pandas as pd
from scipy import stats
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(palette="Set2")


def gamma_pdf(x, shape, coef, obs_err_sd=.1, tp_err_sd=.001):
    y = stats.gamma(shape).pdf(x) * coef
    y += np.random.normal(0, obs_err_sd, 1)
    y += np.random.normal(0, tp_err_sd, len(x))
    return y

gammas = []
n_units = 20
params = [(5, 1), (8, -.5)]
x = np.linspace(0, 15, 31)
for s in range(n_units):
    for p, (shape, coef) in enumerate(params):
        y = gamma_pdf(x, shape, coef)
        gammas.append(pd.DataFrame(dict(condition=[["pos", "neg"][p]] * len(x),
                                        subj=["subj%d" % s] * len(x),
                                        time=x * 2,
                                        BOLD=y), dtype=np.float))
gammas = pd.concat(gammas)

sns.tsplot(gammas, time="time", unit="subj",
           condition="condition", value="BOLD", marker="o")
plt.show()

您必须对每个级别的
条件
变量调用
tsplot
两次,或者您可以通过这种方式进行绘图,然后对绘图数据进行后期处理:

ax = sns.tsplot(gammas, time="time", unit="subj",
                condition="condition", value="BOLD", marker="o")
ax.lines[-1].set_marker("s")

我想你只需要再次调用ax.legend(),它就会更新。是的,谢谢!使用
title
参数到
legend
以获得与
seaborn
产生的图例完全相同的图例:
ax.legend(title='condition')