Can't Show Multiple Stocks Plot with JupyterLab or from Terminal - python

I have this code but it won't show the plot, how to fix this?
I am following this from youtube tutorial: https://www.google.com/url?sa=t&source=video&cd=&cad=rja&uact=8&ved=2ahUKEwjl14q4tZL9AhXs1XMBHbbCA_YQtwJ6BAgJEAI&url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DvKWf5skmGAI&usg=AOvVaw0SFP9z9l4Eb37YCeJgnpeK
Is Python really this bad? The code out there mostly problematic when someone try to run in another computer with perhaps different Python version or package version.
import numpy as np
import pandas as pd
from pandas_datareader import data as wb
import matplotlib.pyplot as plt
stocks = [
{
'ticker': 'UU.L',
'name': 'United Utilities'
},
{
'ticker': 'VOD.L',
'name': 'Vodafone Group'
},
{
'ticker': 'BP.L',
'name': 'BP Group'
}
]
def create_plots(stocks):
data = pd.DataFrame()
for stock in stocks:
data[stock['ticker']] = wb.DataReader(stock['ticker'], data_source='yahoo', start='2007-1-1')['Adj Close']
returns = data.apply(lambda x: (x / x[0] * 100))
plt.figure(figsize=(10,6))
for stock in stocks:
plt.plot(returns[stock['ticker']], label=stock['name'])
plt.legend()
plt.ylabel('Cumulative Returns %')
plt.xlabel('Time')
plt.show

Related

Change the format of values in a Plotly pie chart (in Python)

I'd like to make a pie chart in Python with plotly that displays amounts of money. The following code shows a minimal working example:
import pandas as pd
import plotly.express as px
data = {"Name": ["Stocks", "ETF's", "Cash"], "Value": [1000, 2000, 3000]}
df = pd.DataFrame(data)
figure = px.pie(df, values='Value', names='Name',
title='Fund Distribution')
figure.update_traces(textposition='inside', textinfo='percent+label+value')
figure.show()
Which yields the following figure:
But, it'd like to format the values. I.e. $3,000 instead of just 3000.
I found this question regarding the same problem in R but nothing in Python.
Thanks a lot.
You can use text attribute to format values and then add it to textinfo:
import pandas as pd
import plotly.express as px
data = {"Name": ["Stocks", "ETF's", "Cash"], "Value": [1000, 2000, 3000]}
df = pd.DataFrame(data)
figure = px.pie(df, values='Value', names='Name',
title='Fund Distribution',)
figure.update_traces(textposition='inside',
text=df['Value'].map("${:,}".format),
textinfo='percent+label+text')
figure.show()

pandas / matplotlib : How do I show all years on the x-axis of my plot?

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import matplotlib.markers as markers
from datetime import datetime as dt
df = pd.DataFrame(
{
'date': ['2011-01-01', '2011-02-01', '2012-01-01', '2012-02-01', ],
'amount': [100, 200, 250, 150,],
}
)
df.date = pd.to_datetime(df.date)
df_TotalDay = df[['date','amount']].copy(deep=True)
df_TotalDay = df_TotalDay.groupby('date').amount.sum()
df_TotalDay
fig, ax = plt.subplots(figsize=(15,5))
ax.plot(df_TotalDay.index, df_TotalDay.values)
plt.xticks([x for x in df_TotalDay.index])
plt.show()
In the plot above, I just want to display the unique year. Here, it would just be '2011' and '2012' instead of the actual dates.
I've tried
plt.xticks([x for x in df_TotalDay.index.year.unique()])
but this didn't work.
I know this looks a bit silly with the DataFrame above but my actual DataFrame is quite large and when I plot my data, it looks like this:
In the plot above, matplotlib is not listing every year on the x-axis. I would like to include the missing years.
You are very close. You should generate all the dates you want to show and then add to xticks:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame(
{
'date': ['2011-01-01', '2013-02-01', '2016-01-01', '2018-02-01', ],
'amount': [100, 200, 250, 150,],
}
)
df.date = pd.to_datetime(df.date)
df_TotalDay = df[['date','amount']].copy(deep=True)
df_TotalDay = df_TotalDay.groupby('date').amount.sum()
fig, ax = plt.subplots(figsize=(15,5))
ax.plot(df_TotalDay.index, df_TotalDay.values)
plt.xticks(pd.date_range(df_TotalDay.index.min(), df_TotalDay.index.max(), freq='YS'))
plt.show()
To show only year, you could use
import matplotlib.dates as mdates
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y'))
An alternative to Z Li answer is to use matplotlibs dates.YearLocator
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib
from datetime import datetime as dt
df = pd.DataFrame(
{
'date': ['2011-01-01', '2011-02-01', '2012-01-01', '2012-02-01', ],
'amount': [100, 200, 250, 150,],
}
)
df.date = pd.to_datetime(df.date)
df_TotalDay = df[['date','amount']].copy(deep=True)
df_TotalDay = df_TotalDay.groupby('date').amount.sum()
fig, ax = plt.subplots(figsize=(15,5))
ax.plot(df_TotalDay.index, df_TotalDay.values)
ax.set_xlabel('Year')
ax.minorticks_off()
ax.xaxis.set_major_locator(matplotlib.dates.YearLocator())
ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter("%Y"))

How to fix this error in Python related to web.DataReader

I am having an error related to web.datareader. For some reason I can't figure out what is causing it. Any ideas?
!pip install mplfinance
!pip install seaborn
!pip install matplotlib
import numpy as np
import pandas as pd
import mplfinance as mpf
import datetime as dt
import matplotlib.pyplot as plt
import seaborn as sns
currency = "USD"
metric = "Close"
start = dt.datetime(2018,1,1)
end = dt.datetime.now()
crypto = ['BTC', 'ETH', 'LTC', 'XRP', 'DASH', 'SC']
colnames = []
first = True
for ticker in crypto:
data = web.DataReader(f"{ticker}-{currency}", "yahoo", start, end)
if first:
combined = data[[metric]].copy()
colnames.append(ticker)
combined.columns = colnames
first = False
else:
combined = combined.join(data[metric])
colnames.append(ticker)
combined.columns = colnames
plt.yscale('log') # first show linear
for ticker in crypto:
plt.plot(combined[ticker], label=ticker)
plt.legend(loc="upper right")
plt.show()
# # Correlation Heat Map
print(combined)
combined = combined.pct_change().corr(method='pearson')
sns.heatmap(combined, annot=True, cmap="coolwarm")
plt.show()
You are missing two lines:
!pip install pandas-datareader
import pandas_datareader as web

Stock data download python no library

I am coding a project for school (A-Level) and need to be able to download stock data and chart it. I am able to chart the data using matplotlib. However I am only allowed to use a certain number of libraries.
I need to get the data without importing a library,but was unable to do it. I've tried downloading from https://query1.finance.yahoo.com/v7/finance/download/ticker, but the crumb value keeps changing so I keep getting errors from wrong cookie.
How can i fix this? Or is there an easier site for the data?
My code:
import requests
r = requests.get("query1.finance.yahoo.com/v7/finance/download/…)
file = open(r"MSFT.csv", 'w')
file.write(r.text) file.close()
Download the data from https://datahub.io or else you can subscribe for real time data feed from third party vendors of different exchanges.
You stated: 'I am only allowed to use a certain number of libraries.' What does that mean? You should be able to use any libraries you need to use, right. Run the script below. It will download stock data from Yahoo and plot the time series.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import scipy.optimize as sco
import datetime as dt
import math
from datetime import datetime, timedelta
from pandas_datareader import data as wb
from sklearn.cluster import KMeans
np.random.seed(777)
start = '2019-4-30'
end = '2019-10-31'
# N = 90
# start = datetime.now() - timedelta(days=N)
# end = dt.datetime.today()
tickers = ['MMM',
'ABT',
'ABBV',
'ABMD',
'AAPL',
'XEL',
'XRX',
'XLNX',
'XYL',
'YUM',
'ZBH',
'ZION',
'ZTS']
thelen = len(tickers)
price_data = []
for ticker in tickers:
prices = wb.DataReader(ticker, start = start, end = end, data_source='yahoo')[['Adj Close']]
price_data.append(prices.assign(ticker=ticker)[['ticker', 'Adj Close']])
df = pd.concat(price_data)
df.dtypes
df.head()
df.shape
pd.set_option('display.max_columns', 500)
df = df.reset_index()
df = df.set_index('Date')
table = df.pivot(columns='ticker')
# By specifying col[1] in below list comprehension
# You can select the stock names under multi-level column
table.columns = [col[1] for col in table.columns]
table.head()
plt.figure(figsize=(14, 7))
for c in table.columns.values:
plt.plot(table.index, table[c], lw=3, alpha=0.8,label=c)
plt.legend(loc='upper left', fontsize=12)
plt.ylabel('price in $')

Plotly Library do not showed anything

I tried to plot a scatter plot using plotly libraries.
import chart_studio.plotly as py
import plotly.offline as pyoff
import plotly.graph_objs as go
#plot monthly sales
plot_data = [
go.Scatter(
x=df['date'],
y=df['qty'],
)
]
plot_layout = go.Layout(
title='Montly Sold'
)
fig = go.Figure(data=plot_data, layout=plot_layout)
pyoff.iplot(fig)
fig.show()
The output is just a blank
How to overcome this problem?
I don't have the chart_studio installed but it seems it wasn't used anyway in your code. So after commenting chart_studio import and adding some data to your dataframe I can successfully run your code in my IDE (Eclipse). However it was opening two windows with the same plot so I had to remove one of the two last lines so just one window opens.
Then I tried your code in local Jupyter Notebook and in hosted Google CoLab and it works fine with the following code:
import plotly.graph_objs as go
import pandas as pd
import numpy as np
rng = pd.date_range('2015-02-24', periods=5, freq='T')
df = pd.DataFrame({ 'date': rng, 'qty': np.random.randn(len(rng)) })
#plot monthly sales
plot_data = [
go.Scatter(
x=df['date'],
y=df['qty'],
)
]
plot_layout = go.Layout(
title='Montly Sold'
)
fig = go.Figure(data=plot_data, layout=plot_layout)
fig.show()
Or you could leave the import plotly.offline as pyoff and use pyoff.iplot(fig) instead of fig.show() which also works fine.
Note: Running your code in Jupyter Notebook for the first time after (re-)starting you computer can take quite some time to generate and show a plot.

Categories