python - 使用 Python 绘制每小时结果

标签 python pandas plot machine-learning

我通过使用 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()

graph

关于python - 使用 Python 绘制每小时结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42968433/

相关文章:

python - 将货币转换为 float (括号表示负数)

python - matplotlib:每个 x 轴刻度的分组误差线

python - 具有渐变颜色的 Bokeh 散点图

python - matplotlib 中的阴影图例线

python - SQLAlchemy 无法自动加载 mssql 临时表

python - 使用实时相机预览更新 matplotlib 中的帧

python - Python 中的 Switch/Case 实现有什么值(value)吗?

python - 如何将导致错误的数据插入到单独的文件中,同时处理列的其余部分?

python - 将一列拆分为两列,留下空白

python - 通过计算 python 中 groupby 之后的列中 0 出现的次数来获取子集