sarahkh
sarahkh

Reputation: 11

Plot hourly result using Python

I have result of hourly price by using group by:

group=df.groupby("hour")["price"].mean()

and then I normalize the result by max price by using this code:

nor=preprocessing.normalize(group, norm='max')

I then plot the result using this code:

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()

but it does not show anything?

Upvotes: 1

Views: 619

Answers (1)

jezrael
jezrael

Reputation: 862851

I think you need convert one-line 2D array to 1D array, but first need reshape by reshape(1, -1) because Series group is converted to a single sample:

nor=preprocessing.normalize(group.values.reshape(1,-1), norm='max')[0,:]

Another solution with numpy.squeeze:

nor=np.squeeze(preprocessing.normalize(group.values.reshape(1,-1), norm='max'))

Another alternatives are here.

Sample:

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

Upvotes: 1

Related Questions