我通过使用 group by 获得了每小时价格的结果:
group=df.groupby("hour")["price"].mean()
然后我使用以下代码按最高价格标准化结果:
nor=preprocessing.normalize(group, norm='max')
然后我使用以下代码绘制结果:
plt.figure()
plt.plot( nor,
label='price 1',
color='red')
plt.xlim(0,24)
plt.ylim([0.0, 1])
plt.xlabel('time')
plt.ylabel('price')
plt.title('g2')
plt.legend(loc='upper right', fancybox=True)
plt.show()
但它没有显示任何内容?
最佳答案
我认为您需要将单行二维数组转换为一维数组,但首先需要通过 reshape(1, -1)
进行 reshape ,因为 Series
group
转换为单个样本:
nor=preprocessing.normalize(group.values.reshape(1,-1), norm='max')[0,:]
另一个解决方案 numpy.squeeze
:
nor=np.squeeze(preprocessing.normalize(group.values.reshape(1,-1), norm='max'))
另一个替代方案是 here .
示例:
np.random.seed(100)
df = pd.DataFrame({'hour':range(24),
'price':np.random.random(size=24)})
#print (df)
group=df.groupby("hour")["price"].mean()
nor=preprocessing.normalize(group.values.reshape(1,-1), norm='max')[0,:]
print (nor)
[ 0.55527461 0.28444985 0.43379039 0.86322869 0.00482193 0.12422457
0.68540035 0.84389197 0.13969269 0.58765517 0.91079122 0.21377175
0.18937637 0.11074418 0.22449638 1. 0.82941286 0.17569674
0.83405366 0.28006038 0.44113396 0.96056302 0.83550941 0.34345369]
plt.figure()
plt.plot( nor,
label='price 1',
color='red')
plt.xlim(0,24)
plt.ylim([0.0, 1])
plt.xlabel('time')
plt.ylabel('price')
plt.title('g2')
plt.legend(loc='upper right', fancybox=True)
plt.show()
关于python - 使用 Python 绘制每小时结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42968433/