在不同的地块中绘制多个 Pandas 自相关图
Plotting multiple Pandas autocorrelation plots in different plots
我的问题与this one有些相关。我有一个 Pandas DataFrame,我想分别为 value
中的每个项目绘制自相关函数 category
。下面是我尝试过的,它在同一个图上绘制了所有自相关函数。如何分别绘制它们并控制绘图大小?
# Import libraries
import pandas as pd
from pandas.plotting import autocorrelation_plot
# Create DataFrame
df = pd.DataFrame({
'category': ['sav','sav','sav','sav','sav','check','check','check','check','check','cd','cd','cd','cd','cd'],
'value': [1.2,1.3,1.5,1.7,1.8, 10,13,17,20,25, 7,8,8.5,9,9.3]
})
# Loop through for each item in category and plot autocorrelation function
for cat in df['category'].unique():
s = df[df['category']==cat]['value']
s = s.diff().iloc[1:] #First order difference to de-trend
ax = autocorrelation_plot(s)
一种简单的方法是在每次迭代后强制渲染 plt.show()
:
# Loop through for each item in category and plot autocorrelation function
for cat in df['category'].unique():
# create new figure, play with size
plt.figure(figsize=(10,6))
s = df[df['category']==cat]['value']
s = s.diff().iloc[1:] #First order difference to de-trend
ax = autocorrelation_plot(s)
plt.show() # here
语法也可以简化为groupby
:
for cat, data in df.groupby('category')['value']:
plt.figure(figsize=(10,6))
autocorrelation_plot(data.diff().iloc[1:])
plt.title(cat)
plt.show()
我的问题与this one有些相关。我有一个 Pandas DataFrame,我想分别为 value
中的每个项目绘制自相关函数 category
。下面是我尝试过的,它在同一个图上绘制了所有自相关函数。如何分别绘制它们并控制绘图大小?
# Import libraries
import pandas as pd
from pandas.plotting import autocorrelation_plot
# Create DataFrame
df = pd.DataFrame({
'category': ['sav','sav','sav','sav','sav','check','check','check','check','check','cd','cd','cd','cd','cd'],
'value': [1.2,1.3,1.5,1.7,1.8, 10,13,17,20,25, 7,8,8.5,9,9.3]
})
# Loop through for each item in category and plot autocorrelation function
for cat in df['category'].unique():
s = df[df['category']==cat]['value']
s = s.diff().iloc[1:] #First order difference to de-trend
ax = autocorrelation_plot(s)
一种简单的方法是在每次迭代后强制渲染 plt.show()
:
# Loop through for each item in category and plot autocorrelation function
for cat in df['category'].unique():
# create new figure, play with size
plt.figure(figsize=(10,6))
s = df[df['category']==cat]['value']
s = s.diff().iloc[1:] #First order difference to de-trend
ax = autocorrelation_plot(s)
plt.show() # here
语法也可以简化为groupby
:
for cat, data in df.groupby('category')['value']:
plt.figure(figsize=(10,6))
autocorrelation_plot(data.diff().iloc[1:])
plt.title(cat)
plt.show()