Getting "key error" while plotting using Pandas - python-3.x

I am trying to do a simple plot of a data from a text file. Below is the file:
Date,Open,High,Low,Close
03-10-16,774.25,776.065002,769.578768,772.559998
04-10-16,776.03,778.710022,772.890015,776.429993
05-10-16,779.30,782.070007,775.650024,776.469971
06-10-16,779.00,780.479989,775.539978,776.859985
07-10-16,779.65,779.659973,770.757867,775.080017
Below is the python code i m trying to execute:
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv('financial.txt', index_col=0)
df.plot(x=df.index, y=df.columns)
plt.show()
Error:
KeyError: "Index(['03-10-16', '04-10-16', '05-10-16', '06-10-16', '07-10-16'], dtype='object', name='Date') not in index"
I am not sure why i am getting that error? Although i have achieved what i wanted by using csv but not sure why i am getting that error.
Checked for the same error online as well but didn't get much.I have checked this.
Key Error:3 while using For to plot using matplotlib.pyplot.scatter
Any light on the error is much appreciated.
Thanks.

This modification will work. You misunderstood how to use df.plot(). Please refer this page visualization. This code below is just a basic visualization, you can change to df.plot.box() or df.plot.area() to get more advanced visualization.
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv('financial.txt', index_col=0)
df.plot()
plt.show()

Related

I am running a code on Regression on student grades and hours although i got the table right as output but the scattered plot is not showing

import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
# Load data
df = pd.read_csv('Grade_Set_1.csv')
print (df)
# Simple scatter plot
df.plot(kind='scatter', x='Hours_Studied', y='Test_Grade', title='Grade vs Hours Studied')
# check the correlation between variables
print(df.corr())
I got this key error on :
KeyError: 'Hours_Studied'

Unable to view Seaborn Plots

I am trying to create a plot using seaborn with data from an excel sheet on python on Jupiter.
The code I am using is:
[In]: import numpy as np
[In]:import pandas as pd
[In]:import seaborn as sns
[In]:import matplotlid.pyplot as plt
[In]:%matplotlib
[In]: df =pd.read_excel(r'C:\Users\jcree\OneDrive\Documents\Accounting\Applied_Accounting_Principles\Exercises\Data_Analytics\Workbook1.xlsx.xlsx')
[In]: sns.distplot(df['Item Price'])
This is where the plot should show up but nothing is happening.
I tried using this solution Seaborn plots not showing up but I am unableto get a chart to show.
Any help would be appreciated

"DataFrame" is not callable

It seems to be a recurrent problem on the site but i was not able to understand any of the similar problems/topics. I'm trying to get a scatter matrix from pandas (pandas.plotting.scatter_matrix), but I get the error DataFrame is not callable.
Sorry to bother you, the error is maybe obvious but I'm not able to deal with it.
I'm not very familiar with pandas.
#Data_set is data from load_iris from sklearn.datasets, it is a bunch and it
#has 5 keys : 'features_names','target_names','target','DESCR', 'data'
iris_df = pd.DataFrame(Data_set['data'], columns=Data_set['feature_names'])
iris_df['species'] = Data_set['target']
pd.plotting.scatter_matrix(iris_df, alpha=0.2, figsize=(10, 10))
plt.show()
I just want to print the scatter matrix of my data and I get the error DataFrame is not callable and I'm not able to understand why.
I can get the scatter_matrix without any problems using the following code:
from sklearn import datasets
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
pal = sns.color_palette("cubehelix", 8)
sns.set_palette(pal)
Data_set = datasets.load_iris()
iris_df = pd.DataFrame(Data_set['data'], columns=Data_set['feature_names'])
iris_df['species'] = Data_set['target']
pd.plotting.scatter_matrix(iris_df, alpha=0.2, figsize=(10, 10))
plt.show()
There's a possibility you haven't read in the data set correctly. Check the contents of your Data_set.

How to plot a function with the pandas library using Python 3

Please, I need some help, so I am still in the learning phase of the Python programming for Data Science. I tried plotting a function but I am getting this. Please, what can I do? I am using Python 3. I appreciate your insights, explanations.
import pandas as pd
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib import rcParams
import seaborn as sb
mtcars = pd.read_csv('mtcars.csv')
mtcars.columns = ['Car names','mpg','cyl','disp','hp','drat','wt','qsec','vs','am','gear','carb']
mpg1 = mtcars['mpg']
print(mpg1.plot())
df = mtcars[['mpg','cyl','wt']]
print(df.plot())
When I run to see what the plot is like, this is what I get:
AxesSubplot(0.125,0.11;0.775x0.77)
Please can you explain? How can I see the plot?
Thank you so much
you shouldn't "print" the plot, instead you should just call mpg1.plot() (eventually follpwed by plt.show() if needed)

x axis labels (date) slips in Python matplotlib

I'm beginner in Python and I have the following problems. I would like to plot a dataset, where the x-axis shows date data. The Dataset look likes the follows:
datum, start, end
2017.09.01 38086 37719,8984
2017.09.04 37707.3906 37465.2617
2017.09.05 37471.5117 37736.1016
2017.09.06 37723.5898 37878.8594
2017.09.07 37878.8594 37783.5117
2017.09.08 37764.7383 37596.75
2017.09.11 37615.5117 37895.8516
2017.09.12 37889.6016 38076.8789
2017.09.13 38089.1406 38119.0898
2017.09.14 38119.2617 38243.1992
2017.09.15 38243.7188 38325.9297
2017.09.18 38325.3086 38387.2188
2017.09.19 38387.2188 38176.0781
2017.09.20 38173.2109 38108.0391
2017.09.21 38107.2617 38109.2109
2017.09.22 38110.4609 38178.6289
2017.09.25 38121.9102 38107.8711
2017.09.26 38127.25 37319.2383
2017.09.27 37360.8398 37244.3008
2017.09.28 37282.1094 37191.6484
2017.09.29 37192.1484 37290.6484
In the first column are the labels of the x-axis (this is the date).
When I write the following code the x axis data slips:
import pandas as pd
import matplotlib.pyplot as plt
bux = pd.read_csv('C:\\Home\\BUX.txt',
sep='\t',
decimal='.',
header=0)
fig1 = bux.plot(marker='o')
fig1.set_xticklabels(bux.datum, rotation='vertical', fontsize=8)
The resulted figure look likes as follows:
The second data row in the dataset is '2017.09.04 37707.3906 37465.2617', BUT '2017.09.04' is yield at the third data row with start value=37471.5117
What shell I do to get correct x axis labels?
Thank you!
Agnes
First, there is a comma in the second line instead of a .. This should be adjusted. Then, you convert the "datum," column to actual dates and simply plot the dataframe with matplotlib.
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('data/BUX.txt', sep='\s+')
df["datum,"] = pd.to_datetime(df["datum,"], format="%Y.%m.%d")
plt.plot(df["datum,"], df["start,"], marker="o")
plt.plot(df["datum,"], df["end"], marker="o")
plt.gcf().autofmt_xdate()
plt.show()
Thank you! It works perfectly. The key moment was to convert the data to date format. Thank you again!
Agnes
Actually you can easily use the df.plot() to fix it:
import pandas as pd
import matplotlib.pyplot as plt
import io
t="""
date start end
2017.09.01 38086 37719.8984
2017.09.04 37707.3906 37465.2617
2017.09.05 37471.5117 37736.1016
2017.09.06 37723.5898 37878.8594
2017.09.07 37878.8594 37783.5117
2017.09.08 37764.7383 37596.75
2017.09.11 37615.5117 37895.8516
2017.09.12 37889.6016 38076.8789
2017.09.13 38089.1406 38119.0898
2017.09.14 38119.2617 38243.1992
2017.09.15 38243.7188 38325.9297
2017.09.18 38325.3086 38387.2188
2017.09.19 38387.2188 38176.0781
2017.09.20 38173.2109 38108.0391
2017.09.21 38107.2617 38109.2109
2017.09.22 38110.4609 38178.6289
2017.09.25 38121.9102 38107.8711
2017.09.26 38127.25 37319.2383
2017.09.27 37360.8398 37244.3008
2017.09.28 37282.1094 37191.6484
2017.09.29 37192.1484 37290.6484
"""
import numpy as np
data=pd.read_fwf(io.StringIO(t),header=1,parse_dates=['date'])
data.plot(x='date',marker='o')
plt.show()

Resources