Issue
I'm trying to build some software to visualize hourly electricity prices. I have this so far:
import warnings
warnings.filterwarnings('ignore')
import numpy as np
import pytz
import matplotlib.pyplot as plt
import pandas as pd
import dateutil
import csv
with open('Hourly_Prices2.csv', newline='') as f:
reader = csv.reader(f)
data = list(reader)
df = pd.DataFrame(data)
df.columns=['Hour of Day', 'Spot Price']
df.drop(0, inplace=True)
df.set_index('Hour of Day', inplace=True)
print (df)
plt.xlabel("Hour of Day")
plt.ylabel("Spot Price (MWH)")
plt.title("Daily Price of Electricity")
plt.plot(df)
All of the data frame stuff seems to be working fine, by which I mean that the csv file is opening fine, and I'm able to output two columns, one showing the hour and the other showing the spot price for that hour. My problem is that when I try to plot it using plt.plot(df)
, I'm given a message reading
TypeError: unhashable type: 'numpy.ndarray'
What can I do to fix this? I'm a pretty new programmer, so I have a feeling I'm missing something obvious. Thanks so much for any help!
Solution
You can not only plot directly from the DataFrame, but you can also read your csv directly into the DataFrame as demonstrated below:
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv('Hourly_Prices2.csv')
df.columns = ['Hour of Day', 'Spot Price']
df.set_index('Hour of Day', inplace=True)
print(df)
df.plot()
plt.xlabel("Hour of Day")
plt.ylabel("Spot Price (MWH)")
plt.title("Daily Price of Electricity")
plt.show()
Answered By - pakpe
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.