如何为 Dataframe 中的实际值分配名称
How do I assign a name to the actual values in a Dataframe
我正在尝试使用 seaborn 创建一个线图,但我正在努力定义“y”。 ;)
我正在按照此处列出的流程进行操作:https://seaborn.pydata.org/generated/seaborn.lineplot.html
我失败的地方在于用均值和阴影 95% CI 创建绘图,因为我无法定义“y”。
该示例从同一 Dataframe 的先前形状中获取了它的 y(“乘客”),其中这是列 header(然后数据已使用月份和年份重新格式化为 columns/index).
我的数据已经在具有所需结构的 Dataframe 中(列是日期,行是 N 次模拟的输出)。我想绘制随时间变化的模拟输出的平均值和 CI。
所以我觉得这应该很容易,但是我找不到任何关于如何标记值的信息! (我想我可以将数据重塑为一列并给它一个标签,但这看起来效率很低!)
df 中的所有值都应具有相同的标签(“批准”),类似于“乘客”在 link 中的工作方式。
谢谢!!
您可能需要将数据框转换为 "long form"。这有助于大多数 Seaborn 功能发挥其全部潜力。
下面是一些示例代码,其中的数据最初按日期组织为一列。
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
# create some test data as suggested by the original post
df = pd.DataFrame({}, index=[f'sim_{i}' for i in range(1, 11)])
dates = pd.date_range('20211201', periods=20, freq='D')
for d in dates:
df[d] = np.random.normal(.1, 1, len(df)).cumsum()
df.index.name = 'simulation' # give the index an explicit name, this will be the column name after df.reset_index()
# convert the dataframe to long form
df_long = df.reset_index().melt(id_vars='simulation', var_name='date', value_name='value')
df_long['date'] = pd.to_datetime(df_long['date']) # make the column a real datetime column
fig, (ax1, ax2) = plt.subplots(nrows=2, figsize=(12, 4), sharex=True)
sns.lineplot(data=df_long, x='date', y='value', ci=95, ax=ax1)
sns.lineplot(data=df_long, x='date', y='value', hue='simulation', ax=ax2)
ax2.legend_.remove()
plt.tight_layout()
plt.show()
PS:这是原始数据框 df
的样子:
2021-12-01 2021-12-02 ... 2021-12-19 2021-12-20
sim_1 -0.173437 0.488611 ... 0.304839 -0.324995
sim_2 -0.283472 2.692735 ... -0.526787 -0.451747
...
和“长格式”:
simulation date value
0 sim_1 2021-12-01 -0.173437
1 sim_2 2021-12-01 -0.283472
2 sim_3 2021-12-01 -0.657405
...
我正在尝试使用 seaborn 创建一个线图,但我正在努力定义“y”。 ;)
我正在按照此处列出的流程进行操作:https://seaborn.pydata.org/generated/seaborn.lineplot.html
我失败的地方在于用均值和阴影 95% CI 创建绘图,因为我无法定义“y”。
该示例从同一 Dataframe 的先前形状中获取了它的 y(“乘客”),其中这是列 header(然后数据已使用月份和年份重新格式化为 columns/index).
我的数据已经在具有所需结构的 Dataframe 中(列是日期,行是 N 次模拟的输出)。我想绘制随时间变化的模拟输出的平均值和 CI。
所以我觉得这应该很容易,但是我找不到任何关于如何标记值的信息! (我想我可以将数据重塑为一列并给它一个标签,但这看起来效率很低!)
df 中的所有值都应具有相同的标签(“批准”),类似于“乘客”在 link 中的工作方式。
谢谢!!
您可能需要将数据框转换为 "long form"。这有助于大多数 Seaborn 功能发挥其全部潜力。
下面是一些示例代码,其中的数据最初按日期组织为一列。
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
# create some test data as suggested by the original post
df = pd.DataFrame({}, index=[f'sim_{i}' for i in range(1, 11)])
dates = pd.date_range('20211201', periods=20, freq='D')
for d in dates:
df[d] = np.random.normal(.1, 1, len(df)).cumsum()
df.index.name = 'simulation' # give the index an explicit name, this will be the column name after df.reset_index()
# convert the dataframe to long form
df_long = df.reset_index().melt(id_vars='simulation', var_name='date', value_name='value')
df_long['date'] = pd.to_datetime(df_long['date']) # make the column a real datetime column
fig, (ax1, ax2) = plt.subplots(nrows=2, figsize=(12, 4), sharex=True)
sns.lineplot(data=df_long, x='date', y='value', ci=95, ax=ax1)
sns.lineplot(data=df_long, x='date', y='value', hue='simulation', ax=ax2)
ax2.legend_.remove()
plt.tight_layout()
plt.show()
PS:这是原始数据框 df
的样子:
2021-12-01 2021-12-02 ... 2021-12-19 2021-12-20
sim_1 -0.173437 0.488611 ... 0.304839 -0.324995
sim_2 -0.283472 2.692735 ... -0.526787 -0.451747
...
和“长格式”:
simulation date value
0 sim_1 2021-12-01 -0.173437
1 sim_2 2021-12-01 -0.283472
2 sim_3 2021-12-01 -0.657405
...