gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 11:02:51 25 4
gpt4 key购买 nike

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

我正在使用 http://stanford.edu/~mwaskom/software/seaborn/tutorial/timeseries_plots.html#specifying-input-data-with-long-form-dataframes 中的示例我复制并粘贴了下面的代码。

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

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

Code Output

最佳答案

您必须对 condition 变量的每个级别调用 tsplot 两次,或者您可以这样绘制,然后对绘图数据:

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

关于python - 如何使用 tsplot 设置多个标记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27986268/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com