def add_periodic_vertical_lines(fig: go.Figure, start: Union[str, Timestamp], end: Union[str, Timestamp], freq: str, y1: float, y0: float = 0) -> go.Figure: """ add vertical lines to plotly figures that repeat at a certain frequency ie every sunday Args: fig: plotly figure start: first date of period end: last date of the period freq: pandas time series frequency directive ie W-THU y1: max value of line y0: minimum value of line Returns: fig: the plotly figures with lines added as a trace """ dates_dow = pd.date_range(start, end, freq=freq) for day in dates_dow: fig.add_shape( type='line', x0=day, y0=y0, x1=day, y1=y1, line=dict(width=1, dash='dot'), ) return fig
def add_chart_title(fig: go.Figure, title: str, elevate_line: int = 1, title_margin: int = 40, font_size: int = 12, **kwargs) -> None: y_line = 0.98 + (elevate_line * 0.04) y_annotation = y_line fig.add_annotation( text=title, font={'size': font_size}, align='left', yanchor='bottom', xref='paper', x=0.5, yref='paper', y=y_annotation, showarrow=False, **kwargs ) fig.add_shape( type='line', xref='paper', x0=0, x1=1, yref='paper', y0=y_line, y1=y_line, line=dict( width=1 ) ) if title_margin: fig.update_layout( dict( margin={'t': title_margin + (elevate_line * 30)}, ) )
def plot(self, fig: go.Figure) -> go.Figure: width = 2 color1 = 'rgba(46, 134, 193, 0.5)' position = (1, 1) rsi = self.result fig.add_trace(go.Scatter(x=self.data['date'], y=rsi, mode='lines', name='rsi', line=dict(color=color1, width=width)), row=position[0], col=position[1]) fig.add_shape(type="line", x0=self.data['date'].min(), y0=30, x1=self.data['date'].max(), y1=30, line=dict(color=color1, width=width, dash="dot"), row=position[0], col=position[1]) fig.add_shape(type="line", x0=self.data['date'].min(), y0=70, x1=self.data['date'].max(), y1=70, line=dict(color=color1, width=width, dash="dot"), row=position[0], col=position[1]) return fig
def add_vertline(fig: go.Figure, y1: float, x: int = 1) -> go.Figure: """add vertical line to at x=1 to plotly figure with height of y1""" fig.add_shape( type='line', x0=x, y0=0, x1=x, y1=y1, line=dict(width=5, dash='dot', color='red'), ) return fig
def plot(self, fig: go.Figure) -> go.Figure: width = 2 color1 = 'rgba(46, 134, 193, 0.5)' color2 = 'rgba(40, 180, 99, 0.5)' color3 = 'rgba(136, 78, 160, 0.5)' position = (1, 1) stoch_fast, stoch_slow, stoch_hist = self.result fig.add_trace(go.Scatter(x=self.data['date'], y=stoch_fast, mode='lines', name='stochastic_fast', line=dict(color=color1, width=width)), row=position[0], col=position[1]) fig.add_trace(go.Scatter(x=self.data['date'], y=stoch_slow, mode='lines', name='stochastic_slow', line=dict(color=color2, width=width)), row=position[0], col=position[1]) fig.add_trace(go.Bar( x=self.data['date'], y=stoch_hist, marker_color=color3, name='stoch_hist', ), row=position[0], col=position[1]) fig.add_shape(type="line", x0=self.data['date'].min(), y0=20, x1=self.data['date'].max(), y1=20, line=dict(color=color3, width=width, dash="dot"), row=position[0], col=position[1]) fig.add_shape(type="line", x0=self.data['date'].min(), y0=80, x1=self.data['date'].max(), y1=80, line=dict(color=color3, width=width, dash="dot"), row=position[0], col=position[1]) return fig
def add_zero_line(fig: go.Figure) -> None: """Add a dotted line across y=0. :param fig: Plotly figure. :type fig: go.Figure """ fig.add_shape( type="line", xref="paper", yref="y", x0=0, y0=0, x1=1, y1=0, line=dict(color="grey", width=1, dash="dash"), )
def scatter_median_lines(fig: go.Figure, df: pd.DataFrame, x: str, y: str) -> go.Figure: """add vertical line to at x=1 to plotly figure with height of y1""" fig.add_shape( type='line', x0=df[x].min(), y0=df[y].median(), x1=df[x].max(), y1=df[y].median(), line=dict(width=3, dash='dot', color='red'), ) fig.add_shape( type='line', x0=df[x].median(), y0=df[y].min(), x1=df[x].median(), y1=df[y].max(), line=dict(width=3, dash='dot', color='red'), ) return fig
def add_correlation_line(figure: go.Figure, xy_min: float, xy_max: float) -> go.Figure: return figure.add_shape( type="line", layer="below", xref="x", yref="y", x0=xy_min, y0=xy_min, x1=xy_max, y1=xy_max, line=dict(color="black", width=2, dash="dash"), )
def plot_dimensions_graph( fig: go.Figure, y: List[float], y_labels: Optional[List[str]] = None, y_err: Optional[List[float]] = None, practical_maximum: Optional[float] = None, label: Optional[str] = None): color = QUALITATIVE_COLORS[0] rgb_ints = [str(int(x.strip(" "))) for x in color[4:][:-1].split(",")] new_color = f"rgba({','.join(rgb_ints + ['0.2'])})" # Error range x = list(range(1, len(y) + 1)) if y_err: y_upper = [m + e for m, e in zip(y, y_err)] y_lower = [m - e for m, e in zip(y, y_err)] fig.add_trace(go.Scatter( x=x + x[::-1], y=y_upper + y_lower[::-1], fill='toself', fillcolor=new_color, line_color='rgba(255,255,255,0)', showlegend=False, name=label, hoverinfo='skip', legendgroup=label, )) custom_data: Optional[List[Any]] = None hover_template = None if y_labels: if y_err: custom_data = list(zip(y_labels, y_err)) hover_template = '<b>%{y:.3f} +- %{customdata[1]:.3f}</b><br>%{customdata[0]}' else: custom_data = y_labels hover_template = '<b>%{y:.3f}</b><br>%{customdata}' # Mean line fig.add_trace(go.Scatter( x=x, y=y, customdata=custom_data, hovertemplate=hover_template, line_color=color, showlegend=True, name=label, legendgroup=label, )) fig.update_traces(mode='lines') # Max MI if practical_maximum: fig.add_shape( # Line Horizontal type="line", x0=min(x), y0=practical_maximum, x1=max(x), y1=practical_maximum, line=dict( color="LightSeaGreen", dash="dash", ), ) fig.update_layout(margin=dict(l=20, r=20, t=20, b=20)) # noqa fig.update_xaxes(title_text="Dimensions Selected", tickvals=x) # Make serif fig.update_layout(font=dict( family="serif", )) return fig