Ploting Stem plot in pandas - python

I have the following code:
import pandas as pd
from pandas_datareader import data as web
import datetime
df = web.DataReader('tip', 'yahoo', start= '1,1,2017', end = datetime.datetime.today())
df1 = df.resample('W-Mon').mean()
df1["Adj Close"].pct_change().plot()
This produces this plot:
To plot in stem in pandas:
df1["Adj Close"].pct_change().stem()
Is producing this error:
AttributeError: 'Series' object has no attribute 'stem'

You could use matplotlib:
import matplotlib.pyplot as plt
plt.stem(df1["Adj Close"].pct_change().index,
df1["Adj Close"].pct_change())
plt.show()
Then customise axis/title as you like.

Related

AttributeError: 'str' object has no attribute 'strftime' error faced

I am facing this problem while coding;
import pandas as pd
import matplotlib from matplotlib import dates as d
import datetime as dt
import matplotlib.pyplot as plt
import numpy as np
data = pd.read_excel(r'F:\Downloads\Book1.xlsx', index_col=0)
data.replace('NoData', np.nan, inplace= True)
data['Time'] = data.index.map(lambda x: x.strftime("%H:%M"))
Error:
AttributeError: 'str' object has no attribute 'strftime'
You can use the parse_dates argument in your read_excel() call, and this means pandas should convert the specified column to datetime objects:
import pandas as pd
import matplotlib from matplotlib import dates as d
import datetime as dt
import matplotlib.pyplot as plt
import numpy as np
data = pd.read_excel(r'F:\Downloads\Book1.xlsx', index_col=0, parse_dates=[0])
data.replace('NoData', np.nan, inplace= True)
data['Time'] = data.index.map(lambda x: x.strftime("%H:%M"))
The documentation for read_excel is here.
I think you can use the pd.to_datetime() function to get datetime as follows:
import pandas as pd
import matplotlib from matplotlib import dates as d
import datetime as dt
import matplotlib.pyplot as plt
import numpy as np
data = pd.read_excel(r'F:\Downloads\Book1.xlsx', index_col=0)
data.replace('NoData', np.nan, inplace= True)
data['Time'] = pd.to_datetime(data.index, format="%H:%M")
You can check the pandas documentation for further information here.

Trying to get a plot using Seaborn but Error saying 'str' object has no attribute 'get'

import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
train = pd.read_csv("train.csv")
# Extract data on column "Category" from train data
Cates = train["Category"].unique()
figure, ax = plt.subplots(10,4)
for i in range(len(Cates)):
a = train[train['Category']==Cates[i]]
sns.countplot(data=each, x="DayOfWeek", ax=ax[(i//4)][i%4])

pandas_datareader cant print date on x axis

In following plot date is not showing on x axis what do to show date time stamp
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import datetime
import pandas_datareader as web
start=datetime.datetime(2014,1,8)
end =datetime.datetime(2019,1,8)
Tesla=web.DataReader('TSLA',"iex",start,end)
Ford=web.DataReader('F',"iex",start,end)
Tesla["open"].plot(label="Tesla",title="opening Price",figsize=(16,8))
plt.legend()
plt.show()
Your dataframe index is created as dtype object or string, let's convert index to DatetimeIndex using pd.to_datetime:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import datetime
import pandas_datareader as web
start=datetime.datetime(2014,1,8)
end =datetime.datetime(2019,1,8)
Tesla=web.DataReader('TSLA',"iex",start,end)
Ford=web.DataReader('F',"iex",start,end)
#Change Telsa index to datetime dtype
Tesla.index = pd.to_datetime(Tesla.index)
#Let's do Fords too
Ford.index = pd.to_datetime(Ford.index)
Tesla["open"].plot(label="Tesla",title="opening Price",figsize=(16,8))
plt.legend()
plt.show()
Output:

How to properly handle 'despine' function to avoid error message

I have been using despine(plt.gca()) as a tool to plot my time series data as demonstrated below:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from datetime import datetime
date_rng = pd.date_range(start='1/2015', end='1/2019', freq='M')
#Let’s create an example data frame with the timestamp data and look at the first 5
df = pd.DataFrame(date_rng, columns=['date'])
df['data'] = np.random.randint(0,100,size=(len(date_rng)))
df.head()
df['datetime'] = pd.to_datetime(df['date'])
df = df.set_index('datetime')
df.drop(['date'], axis=1, inplace=True)
df.head()
# we visualize the data:
df.plot(lw=1.5)
despine(plt.gca())
plt.gcf().autofmt_xdate()
plt.ylabel('Series');
The above python code gives the below error message
NameError: name 'despine' is not defined
if I import seaborn as below:
mport seaborn as sns
df.plot(lw=1.5)
sns.despine(plt.gca())
plt.gcf().autofmt_xdate()
plt.ylabel('Series');
it will produce the error below:
'AxesSubplot' object is not iterable
though the plot is made, but I will prefer there is no error message at all. This error message keep coming each time I use the particular line of code.
Please help me figure out what is wrong with despine(plt.gca()). I am running this code on python 3
You haven't defined any function called despine, nor have you imported any modules with that function defined within it. Assuming you want to use seaborn.despine, you need to import the module and then access the despine function:
import seaborn as sns
# Your code here
sns.despine(ax=plt.gca())

python matplotlib plot datetime index

I am trying to create a simple line graph based on a datetime index. But I get an error message.
#standard packages
import numpy as np
import pandas as pd
#visualization
%matplotlib inline
import matplotlib.pylab as plt
#create weekly datetime index
edf = pd.read_csv('C:\Users\j~\raw.csv', parse_dates=[6])
edf2 = edf[['DATESENT','Sales','Traffic']].copy()
edf2['DATESENT']=pd.to_datetime(edf2['DATESENT'],format='%m/%d/%Y')
edf2 = edf2.set_index(pd.DatetimeIndex(edf2['DATESENT']))
edf2.resample('w').sum()
edf2
#output
SALES
DATESENT
2014-01-05 476
2014-01-12 67876
Then I try to plot (the simplest line plot possible to see sales by week)
#linegraph
edf3.plot(x='DATESENT',y='Sales')
But I get this error message
KeyError: 'DATESENT'
You're getting a KeyError because your 'DATESENT' is the index and NOT a column in edf3. You can do this instead:
#linegraph
edf3.plot(x=edf3.index,y='Sales')

Categories