首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用Python绘制每小时结果图

使用Python绘制每小时结果图
EN

Stack Overflow用户
提问于 2017-03-23 14:08:06
回答 1查看 461关注 0票数 1

我使用group by得到了每小时价格的结果:

代码语言:javascript
运行
复制
group=df.groupby("hour")["price"].mean()

然后我使用下面的代码通过最大价格对结果进行归一化:

代码语言:javascript
运行
复制
nor=preprocessing.normalize(group, norm='max')

然后,我使用以下代码绘制结果:

代码语言:javascript
运行
复制
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()

但是它没有显示任何东西?

EN

回答 1

Stack Overflow用户

发布于 2017-03-23 14:22:31

我认为你需要将单行2D数组转换为1D数组,但首先需要由reshape(1, -1)进行重塑,因为Series group是转换为单个样本的:

代码语言:javascript
运行
复制
nor=preprocessing.normalize(group.values.reshape(1,-1), norm='max')[0,:]

另一种使用numpy.squeeze的解决方案

代码语言:javascript
运行
复制
nor=np.squeeze(preprocessing.normalize(group.values.reshape(1,-1), norm='max'))

另一种选择是here

示例:

代码语言:javascript
运行
复制
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()

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42968433

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档