I saw this 3d plot. it was animated and added a new value every day. i have not found an example to recreate it with plotly in python.
the plot should start with the value from the first row (100). The start value should remain (no rolling values). The plot should be animated in such a way that each row value is added one after the other and the x-axis expands. the following data frame contains the values (df_stocks) and Dates to plot. assigning the colors would be a great addition. the more positive the deeper the green, the more negative the darker red.
import yfinance as yf
import pandas as pd
stocks = ["AAPL", "MSFT"]
df_stocks = pd.DataFrame()
for stock in stocks:
df = yf.download(stock, start="2022-01-01", end="2022-07-01", group_by='ticker')
df['perct'] = df['Close'].pct_change()
df_stocks[stock] = df['perct']
df_stocks.iloc[0] = 0
df_stocks += 1
df_stocks = df_stocks.cumprod()*100
df_stocks -= 100
You can use a list of go.Frame objects as shown in this example. Since you want the line plot to continually extend outward, each frame needs to include data that's one row longer than the previous frame, so we can use a list comprehension like:
frames = [go.Frame(data=
## ...extract info from df_stocks.iloc[:i]
for i in range(len(df_stocks))]
To add colors to your lines depending on their value, you can use binning and labels (as in this answer) to create new columns called AAPL_color and MSFT_color that contain the string of the css color (like 'darkorange' or 'green'). Then you can pass the information from these columns using the argument line=dict(color=...) in each go.Scatter3d object.
import yfinance as yf
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
stocks = ["AAPL", "MSFT"]
df_stocks = pd.DataFrame()
for stock in stocks:
df = yf.download(stock, start="2022-01-01", end="2022-07-01", group_by='ticker')
df['perct'] = df['Close'].pct_change()
df_stocks[stock] = df['perct']
df_stocks.iloc[0] = 0
df_stocks += 1
df_stocks = df_stocks.cumprod()*100
df_stocks -= 100
df_min = df_stocks[['AAPL','MSFT']].min().min() - 1
df_max = df_stocks[['AAPL','MSFT']].max().max() + 1
labels = ['firebrick','darkorange','peachpuff','palegoldenrod','palegreen','green']
bins = np.linspace(df_min,df_max,len(labels)+1)
df_stocks['AAPL_color'] = pd.cut(df_stocks['AAPL'], bins=bins, labels=labels).astype(str)
df_stocks['MSFT_color'] = pd.cut(df_stocks['MSFT'], bins=bins, labels=labels).astype(str)
frames = [go.Frame(
data=[
go.Scatter3d(
y=df_stocks.iloc[:i].index,
z=df_stocks.iloc[:i].AAPL.values,
x=['AAPL']*i,
name='AAPL',
mode='lines',
line=dict(
color=df_stocks.iloc[:i].AAPL_color.values, width=3,
)
),
go.Scatter3d(
y=df_stocks.iloc[:i].index,
z=df_stocks.iloc[:i].MSFT.values,
x=['MSFT']*i,
name='MSFT',
mode='lines',
line=dict(
color=df_stocks.iloc[:i].MSFT_color.values, width=3,
)
)]
)
for i in range(len(df_stocks))]
fig = go.Figure(
data=list(frames[1]['data']),
frames=frames,
layout=go.Layout(
# xaxis=dict(range=[0, 5], autorange=False),
# yaxis=dict(range=[0, 5], autorange=False),
# zaxis=dict(range=[0, 5], autorange=False),
template='plotly_dark',
legend = dict(bgcolor = 'grey'),
updatemenus=[dict(
type="buttons",
font=dict(color='black'),
buttons=[dict(label="Play",
method="animate",
args=[None])])]
),
)
fig.show()
Related
I want to use plotly to show 2 sinuse waves
I want to use slider to show the progress from time=0 to current slider step.
I tried to write the following code:
import numpy as np
import pandas as pd
if __name__ == "__main__":
time = np.arange(0, 10, 0.1)
val1 = np.sin(time)
val2 = np.sin(time) * np.sin(time)
df = pd.DataFrame(val1, columns=['val-1'])
df['val-2'] = val2
fig = px.scatter(df, animation_frame=df.index)
fig.update_layout(xaxis_range=[-100, 100])
fig.update_layout(yaxis_range=[-1.1, 1.1])
fig.show()
but I can see the current value of the 2 sinuse waves (and not the whole waves from step=0 to current step)
How can I change my code and see the whole sinuse waves from step=0 to current step ?
I don't think it is possible to animate a line chart in Express, so I would have to use a graph object. There is an example in the reference, which I will adapt to your assignment. As for the graph structure, create the initial graph data and the respective frames in the animation, add them to the layout by creating steps and sliders.
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
if __name__ == "__main__":
time = np.arange(0, 10, 0.1)
val1 = np.sin(time)
val2 = np.sin(time) * np.sin(time)
df = pd.DataFrame(val1, columns=['val-1'])
df['val-2'] = val2
data = [go.Scatter(mode='lines', x=df.index, y=df['val-1'], name='val-1'),
go.Scatter(mode='lines', x=df.index, y=df['val-2'], name='val-2')]
steps = []
for i in df.index:
step = dict(method="animate", args=[[i], {"title": f'step:{i}'}], label=f'{i}')
steps.append(step)
sliders = [dict(active=0, currentvalue={"prefix": "Step: "}, steps=steps)]
layout = go.Layout(dict(xaxis=dict(range=[-100,100]),
yaxis=dict(range=[-1.1,1.1]),
updatemenus=[dict(type='buttons',
buttons=[dict(label='Start', method='animate', args=[None]),
dict(label='Pause',
method='animate',
args=[[None], dict(frame=dict(
duration=0,
redraw=False),
mode="immediate",
formcurrent=True,
transition=dict(duration=0, easing="linear")
)])],
direction="left",
pad=dict(r=10, t=40),
showactive=False,
x=0.00,
xanchor="right",
y=0,
yanchor="top")],
sliders=sliders
))
frames = []
for i in df.index:
frame = go.Frame(data=[go.Scatter(x=df.index[0:i], y=df.loc[0:i,'val-1']),
go.Scatter(x=df.index[0:i], y=df.loc[0:i,'val-2'])],
layout=go.Layout(title_text=f'Step:{i}'),
name=i)
frames.append(frame)
fig = go.Figure(data=data, layout=layout, frames=frames)
fig.show()
I'm creating a dashboard in which I would like to compare the difference of price between two regions directly. If the price of region 1 is higher, y is POSITIVE, if the price of region 2 is higher, y is NEGATIVE.
The problem is that I would like the line and its fill to change color accordingly to its value, so it has a better representation.
I'm using fill='tozeroy'. I would like y-negative = red and y-positive = blue, for the lines and the fill.
def func(est1, est2):
est1, est2 = 'RIO GRANDE DO SUL', 'SANTA CATARINA' # filter to simulate the callback
df1 = df[df.ESTADO.isin([est1])]
df2 = df[df.ESTADO.isin([est2])]
df_final = pd.DataFrame()
df_estado1 = df1.groupby(pd.PeriodIndex(df1['DATA'], freq="M"))['VALOR REVENDA (R$/L)'].mean().reset_index()
df_estado2 = df2.groupby(pd.PeriodIndex(df2['DATA'], freq="M"))['VALOR REVENDA (R$/L)'].mean().reset_index()
df_estado1['DATA'] = pd.PeriodIndex(df_estado1['DATA'], freq="M")
df_estado2['DATA'] = pd.PeriodIndex(df_estado2['DATA'], freq="M")
df_final['DATA'] = df_estado1['DATA'].astype('datetime64[ns]')
df_final['VALOR REVENDA (R$/L)'] = df_estado1['VALOR REVENDA (R$/L)']-df_estado2['VALOR REVENDA (R$/L)']
fig = go.Figure()
fig.add_trace(go.Scatter(name='Comparação', y=df_final['VALOR REVENDA (R$/L)'], x=df_final['DATA'],
fill='tozeroy', mode='lines'))
return fig
Just for help porpouses, that's the "df_final" format which is returned:
df_final DataFrame
Here's the graph that is being returned from the function: graph returned
Also, how can I style my fill? Maybe add some gradient etc
I found this Plotly reference library, where I scraped the information I'm answering you with: https://plotly.com/python/creating-and-updating-figures/#plotly-express
import plotly.express as px
df = px.data.iris()
fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species",
title="Using The add_trace() method With A Plotly Express Figure")
fig.add_trace(
go.Scatter(
x=[2, 4],
y=[4, 8],
mode="lines",
line=go.scatter.Line(color="gray"),
showlegend=False)
)
fig.show()
Basically, if you put "df_final" in the place of df, and change the axis's data, you'll be good to go.
I have a simple dataframe containing dates and a few headers. I need to remove specific dates from the plot.
fig1 = px.line(df, x=Date, y="Header1")
fig1.show()
I want to remove values from the chart itself (not from dataframe), like (removing 15/01/2022 & 22/02/2022).
date vs value plot
I would most likely rather do this with the dataset used to build your figure, instead of in the figure itself. But this suggestion should do exactly what you're asking for. How you find the outliers will be entirely up to you. Given some thresholds toolow, tohigh, the snippet below will turn Plot 1 into Plot 2
fig.for_each_trace(lambda t: highOutliers.extend([t.x[i] for i, val in enumerate(t.y) if val > toohigh]))
fig.for_each_trace(lambda t: lowOutliers.extend([t.x[i] for i, val in enumerate(t.y) if val < loolow]))
fig.update_xaxes(
rangebreaks=[dict(values=highOutliers+lowOutliers)]
)
fig.update_traces(connectgaps=True)
Plot 1:
Plot 2:
Complete code:
from numpy import random
import datetime
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import numpy as np
# Some sample data
y = np.random.normal(50, 5, 15)
datelist = pd.to_datetime(pd.date_range(datetime.datetime(2020, 1, 1).strftime('%Y-%m-%d'),periods=len(y)).tolist())
df = pd.DataFrame({'date':datelist, 'y':y})
# Introduce some outliers
df.loc[5,'y'] = 120
df.loc[10,'y'] = 2
# build figure
fig = px.line(df, x = 'date', y = 'y')
# containers and thresholds for outliers
highOutliers = []
lowOutliers = []
toohigh = 100
toolow = 20
# find outliers
fig.for_each_trace(lambda t: highOutliers.extend([t.x[i] for i, val in enumerate(t.y) if val > toohigh]))
fig.for_each_trace(lambda t: lowOutliers.extend([t.x[i] for i, val in enumerate(t.y) if val < toolow]))
# define outliers as rangebreaks
fig.update_xaxes(
rangebreaks=[dict(values=highOutliers+lowOutliers)]
)
# connect gaps in the line
fig.update_traces(connectgaps=True)
fig.show()
I want to plot some time series data in plotly where the historic portion of the data has a daily resolution and the data for the current day has minute resolution. Is there a way to somehow "split" the x axis so that for the historic data it only shows the date and for the current data it shows time as well?
Currently it looks like this which is not really that readable
I think the only viable approach would be to put together two subplots. But using the correct setup should make the subplots reach pretty much 100% of what you're describing. You'll only need to adjust a few details like:
fig = make_subplots(rows=1, cols=2,
horizontal_spacing = 0,
shared_yaxes=True,
shared_xaxes=True)
Complete code:
# import pandas as pd
import pandas as pd
import numpy as np
from datetime import datetime, timedelta
from plotly.subplots import make_subplots
import plotly.graph_objects as go
# custom function to set the first
# minute dataset to contiunue from
# the last day in the day dataset
def next_day(date):
s = date
date = datetime.strptime(s, "%Y-%m-%d")
next_date = date + timedelta(days=1)
return(datetime.strftime(next_date, "%Y-%m-%d"))
# data
np.random.seed(10)
n_days = 5
n_minutes = (2*24)
dfd = pd.DataFrame({'time':[t for t in pd.date_range('2020', freq='D', periods=n_days).format()],
'y':np.random.uniform(low=-1, high=1, size=n_days).tolist()})
dfm = pd.DataFrame({'time':[t for t in pd.date_range(next_day(dfd['time'].iloc[-1]), freq='min', periods=n_minutes).format()],
'y':np.random.uniform(low=-1, high=1, size=n_minutes).tolist()})
dfm['y'] = dfm['y'].cumsum()
# subplot setup
fig = make_subplots(rows=1, cols=2,
horizontal_spacing = 0,
shared_yaxes=True,
shared_xaxes=True)
# trace for days
fig.add_trace(
go.Scatter(x=dfd['time'], y=dfd['y'], name = 'days'),
row=1, col=1
)
# trace for minutes
fig.add_trace(
go.Scatter(x=dfm['time'], y=dfm['y'], name = 'minutes'),
row=1, col=2
)
# some x-axis aesthetics
fig.update_layout(xaxis1 = dict(tickangle=0))
fig.update_layout(xaxis2 = dict(tickangle=90))
fig.add_shape( dict(type="line",
x0=dfd['time'].iloc[-1],
y0=dfd['y'].iloc[-1],
x1=dfm['time'].iloc[0],
y1=dfm['y'].iloc[0],
xanchor = 'middle',
xref = 'x1',
yref = 'y1',
line=dict(dash = 'dash',
color="rgba(0,0,255,0.9)",
width=1
)))
fig.update_xaxes(showgrid=False)
fig.update_layout(template = 'plotly_dark')
fig.show()
I use plotly package to show dynamic finance chart at python. However I didn't manage to put my all key points lines on one chart with for loop. Here is my code:
fig.update_layout(
for i in range(0,len(data)):
shapes=[
go.layout.Shape(
type="rect",
x0=data['Date'][i],
y0=data['Max_alt'][i],
x1='2019-12-31',
y1=data['Max_ust'][i],
fillcolor="LightSkyBlue",
opacity=0.5,
layer="below",
line_width=0)])
fig.show()
I have a data like below one. It is time series based EURUSD parity financial dataset. I calculated two constraits for both Local Min and Max. I wanted to draw rectangule shape to based on for each Min_alt / Min_ust and Max_alt / Max_range. I can draw for just one date like below image however I didn't manage to show all ranges in same plotly graph.
Here is the sample data set.
Here is the solution for added lines:
import datetime
colors = ["LightSkyBlue", "RoyalBlue", "forestgreen", "lightseagreen"]
ply_shapes = {}
for i in range(0, len(data1)):
ply_shapes['shape_' + str(i)]=go.layout.Shape(type="rect",
x0=data1['Date'][i].strftime('%Y-%m-%d'),
y0=data1['Max_alt'][i],
x1='2019-12-31',
y1=data1['Max_ust'][i],
fillcolor="LightSkyBlue",
opacity=0.5,
layer="below"
)
lst_shapes=list(ply_shapes.values())
fig1.update_layout(shapes=lst_shapes)
fig1.show()
However I have still problems to add traces to those lines. I mean text attribute.
Here is my code:
add_trace = {}
for i in range(0, len(data1)):
add_trace['scatter_' + str(i)] = go.Scatter(
x=['2019-12-31'],
y=[data1['Max_ust'][i]],
text=[str(data['Max_Label'][i])],
mode="text")
lst_trace = list(add_trace.values())
fig2=go.Figure(lst_trace)
fig2.show()
The answer:
For full control of each and every shape you insert, you could follow this logic:
fig = go.Figure()
#[...] data, traces and such
ply_shapes = {}
for i in range(1, len(df)):
ply_shapes['shape_' + str(i)]=go.layout.Shape()
lst_shapes=list(ply_shapes.values())
fig.update_layout(shapes=lst_shapes)
fig.show()
The details:
I'm not 100% sure what you're aimin to do here, but the following suggestion will answer your question quite literally regarding:
How to add more than one shape with loop in plotly?
Then you'll have to figure out the details regarding:
manage to put my all key points lines on one chart
Plot:
The plot itself is most likely not what you're looking for, but since you for some reason are adding a plot by the length of your data for i in range(0,len(data), I've made this:
Code:
This snippet will show how to handle all desired traces and shapes with for loops:
# Imports
import pandas as pd
#import matplotlib.pyplot as plt
import numpy as np
import plotly.graph_objects as go
#from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
# data, random sample to illustrate stocks
np.random.seed(12345)
rows = 20
x = pd.Series(np.random.randn(rows),index=pd.date_range('1/1/2020', periods=rows)).cumsum()
y = pd.Series(x-np.random.randn(rows)*5,index=pd.date_range('1/1/2020', periods=rows))
df = pd.concat([y,x], axis = 1)
df.columns = ['StockA', 'StockB']
# lines
df['keyPoints1']=np.random.randint(-5,5,len(df))
df['keyPoints2']=df['keyPoints1']*-1
# plotly traces
fig = go.Figure()
stocks = ['StockA', 'StockB']
df[stocks].tail()
traces = {}
for i in range(0, len(stocks)):
traces['trace_' + str(i)]=go.Scatter(x=df.index,
y=df[stocks[i]].values,
name=stocks[i])
data=list(traces.values())
fig=go.Figure(data)
# shapes update
colors = ["LightSkyBlue", "RoyalBlue", "forestgreen", "lightseagreen"]
ply_shapes = {}
for i in range(1, len(df)):
ply_shapes['shape_' + str(i)]=go.layout.Shape(type="line",
x0=df.index[i-1],
y0=df['keyPoints1'].iloc[i-1],
x1=df.index[i],
y1=df['keyPoints2'].iloc[i-1],
line=dict(
color=np.random.choice(colors,1)[0],
width=30),
opacity=0.5,
layer="below"
)
lst_shapes=list(ply_shapes.values())
fig.update_layout(shapes=lst_shapes)
fig.show()
Also you can use fig.add_{shape}:
fig = go.Figure()
fig.add_trace(
go.Scatter( ...)
for i in range( 1, len( vrect)):
fig.add_vrect(
x0=vrect.start.iloc[ i-1],
x1=vrect.finish.iloc[ i-1],
fillcolor=vrect.color.iloc[ i-1]],
opacity=0.25,
line_width=0)
fig.show()