I have MSSQL stored procedure that returns the needed values. I have been able to connect and reference this dataset through PyCharm. However, I am struggling to create a line graph from this information.
Here is the dataset of the SQL code:
Here is my code:
import matplotlib.pyplot as plt
import pyodbc
import pandas as pd
server = 'SERVER'
database = 'DATABASE'
username = 'USERNAME'
password = 'PASSWORD'
sql_connect = pyodbc.connect(
'DRIVER={SQL Server};SERVER=' + server + ';DATABASE=' + database + ';UID=' + username + ';PWD=' + password
)
data = pd.read_sql('EXECUTE [dbo].[stored_procedure]', sql_connect)
df = pd.DataFrame({
'3000': data["3000"],
'4000': data["4000"],
'5000': data["5000"],
'6000': data["6000"],
'7000': data["7000"],
'8000': data["8000"],
'8200': data["8200"],
}, index=data.Hour)
chart = df.plot.line()
When I go back to review the dataset that it returns I get the following:
I thought maybe it was a datatype error and converted each column to an integer like this:
df= pd.DataFrame({
'3000': int(data["3000"]),
'4000': int(data["4000"]),
'5000': int(data["5000"]),
'6000': int(data["6000"]),
'7000': int(data["7000"]),
'8000': int(data["8000"]),
'8200': int(data["8200"]),
}, index=data.Hour)
chart = df.plot.line()
It worked at first however, I ran it again later and it started to error out. I reviewed some other questions and tried to apply the solutions to this issue but to no success. Any idea's on how I can correct this issue?
UPDATE
I figured out that the "Hour" index is causing the "nan" data issues and have adjusted my code to the following now:
df = pd.DataFrame({
'3000': data["3000"].astype('int'),
'4000': data["4000"].astype('int'),
'5000': data["5000"].astype('int'),
'6000': data["6000"].astype('int'),
'7000': data["7000"].astype('int'),
'8000': data["8000"].astype('int'),
'8200': data["8200"].astype('int'),
})
lines = df .plot.line(
color={
"3000": "#00FFFF",
"4000": "#228B22",
"5000": "#191970",
"6000": "#CC5500",
"7000": "#0D98BA",
"8000": "#FFFF00",
"8200": '#FF69B4'
}
)
plt.show()
sql_connect.close()
That code is returning the following graph:
The data within the "Hour" column is considered an "object" data type and should be the x-axis on this chart. However, when adding that to the index, it errors out. Any suggestions?
Thanks again for all the help!