flex_subtitle = "built using jupyter-flex"
flex_external_link = "https://github.com/danielfrg/jupyter-flex/blob/master/examples/plots/plotly.ipynb"
flex_title = "Plotly plots"
flex_orientation = "rows"
import datetime
import numpy as np
import pandas as pd
import pandas_datareader.data as web
import plotly.express as px
import plotly.graph_objects as go
np.random.seed(42)
margin = go.layout.Margin(l=20, r=20, b=20, t=30)
from scipy.stats import norm
cls = np.repeat(["A", "B"], 10)
x = np.arange(0, 20) + norm.rvs(20, 3, size=20)
y = np.arange(0, 20) + norm.rvs(20, 3, size=20)
df = pd.DataFrame({"cls": cls, "x": x, "y": y})
fig = px.scatter(df, x="x", y="y")
fig.update_layout(margin=margin)
fig
fig = px.scatter(df, x="x", y="y", trendline="ols")
fig.update_layout(margin=margin)
fig
fig = px.scatter(df, x="x", y="y", trendline="lowess")
fig.update_layout(margin=margin)
fig
fig = px.scatter(df, x="x", y="y", color="cls", trendline="ols")
fig.update_layout(margin=margin)
fig
import scipy.special as sc
import plotly.figure_factory as ff
df = pd.DataFrame({
"nu075": np.random.gamma(1, 0.75, size=100),
"nu1": np.random.gamma(1, 1, size=100),
"nu2": np.random.gamma(1, 2, size=100),
})
fig = ff.create_distplot([df[c] for c in df.columns], df.columns, show_hist=False)
fig.update_layout(margin=margin)
fig
df = px.data.iris()
fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species", marginal_y="rug", marginal_x="histogram")
fig.update_layout(margin=margin)
fig
df = px.data.iris()
fig = px.density_contour(df, x="sepal_width", y="sepal_length", color="species", marginal_x="rug", marginal_y="histogram")
fig.update_layout(margin=margin)
fig.show()
df = px.data.tips()
fig = px.histogram(df, x="sex", y="tip", histfunc="avg", color="smoker", barmode="group",
facet_row="time", facet_col="day", category_orders={"day": ["Thur", "Fri", "Sat", "Sun"],
"time": ["Lunch", "Dinner"]})
fig.update_layout(margin=margin)
fig.show()
start = datetime.datetime(2015, 1, 1)
end = datetime.datetime(2019, 12, 1)
# df_1 = web.DataReader("AAPL", "yahoo", start, end).reset_index()
# df_2 = web.DataReader("MSFT", "yahoo", start, end).reset_index()
# df_3 = web.DataReader("AMZN", "yahoo", start, end).reset_index()
df_1 = pd.read_csv("AAPL.csv")
df_2 = pd.read_csv("MSFT.csv")
df_3 = pd.read_csv("AMZN.csv")
margin = go.layout.Margin(l=20, r=20, b=20, t=30)
fig = go.Figure([go.Scatter(x=df_1['Date'], y=df_1['Close'])])
fig.update_layout(margin=margin)
fig
fig = go.Figure([go.Scatter(x=df_2['Date'], y=df_2['Close'])])
fig.update_layout(margin=margin)
fig
fig = go.Figure([go.Scatter(x=df_3['Date'], y=df_3['Close'])])
fig.update_layout(margin=margin)
fig
cls = np.repeat(["A", "B"], 10)
x = np.arange(0, 20) + norm.rvs(20, 3, size=20)
y = np.arange(0, 20) + norm.rvs(20, 3, size=20)
df = pd.DataFrame({"cls": cls, "x": x, "y": y})
fig = px.scatter(df, x="x", y="y")
fig.update_layout(margin=margin)
fig
fig = px.scatter(df, x="x", y="y", trendline="ols")
fig.update_layout(margin=margin)
fig
fig = px.scatter(df, x="x", y="y", trendline="lowess")
fig.update_layout(margin=margin)
fig