import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# simulate some artificial data
x = np.random.randn(1000,)
y = 5 * x ** 2 + np.random.randn(1000,)
data = pd.DataFrame(0.0, columns=['X', 'Y'], index=np.arange(1000))
data.X = x
data.Y = y
# now do your stuff
# ================================
# use the pandas 'cut' function
data['X_bins'] = pd.cut(data.X, 3)
data.set_index('X_bins', append=True, inplace=True)
data.drop('X', axis=1, inplace=True)
data.unstack(level=1).boxplot()
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# simulate some artificial data
x = np.random.randn(1000,)
y = 5 * x ** 2 + np.random.randn(1000,)
data = pd.DataFrame(0.0, columns=['X', 'Y'], index=np.arange(1000))
data.X = x
data.Y = y
# now do your stuff
# ================================
# use the pandas 'cut' function
data['X_bins'] = pd.cut(data.X, 3)
# for each bin, calculate the mean of Y
result = data.groupby('X_bins')['Y'].mean()
# do the plot
result.plot()
对于后续问题,我们可以使用boxplot做一些更强大的功能。在
下面是一个例子。在
相关问题 更多 >
编程相关推荐