Interactive Chart (ichart
)#
The ichart
function is designed to create interactive charts using Plotly. It can be used to visualize various types of data, including financial data, scientific data, or any other data that can be represented in a chart format. The function provides a variety of customization options for the chart’s appearance and behavior.
To use the ichart
function, you need to provide a pandas DataFrame containing the data to be plotted. The function works with time series data and other types of data that can be represented as a chart.
The function takes several optional parameters to customize the chart’s appearance, such as the chart title, axis titles, chart style, and more.
ichart() function#
Description#
The ichart
function creates an interactive chart using Plotly with various customization options.
Parameters#
data (pandas.DataFrame): DataFrame containing data to be plotted.
title (str, optional): Title of the chart. Default is an empty string.
colors (list, optional): List of colors to be used in the chart.
yTitle (str, optional): Title of the y-axis. Default is an empty string.
xTitle (str, optional): Title of the x-axis. Default is an empty string.
style (str, optional): Chart style. Default is ‘normal’. Other options include ‘area’ and ‘drawdowns_histogram’.
width (int, optional): Width of the chart in pixels. Default is 990.
height (int, optional): Height of the chart in pixels. Default is 500.
hovermode (str, optional): Hover mode for the chart. Default is ‘x’.
yticksuffix (str, optional): Suffix for y-axis ticks. Default is an empty string.
ytickprefix (str, optional): Prefix for y-axis ticks. Default is an empty string.
ytickformat (str, optional): Format for y-axis ticks. Default is an empty string.
source_text (str, optional): Source text to be displayed below the chart. Default is an empty string.
y_position_source (str, optional): Y position of the source text. Default is ‘-0.125’.
xticksuffix (str, optional): Suffix for x-axis ticks. Default is an empty string.
xtickprefix (str, optional): Prefix for x-axis ticks. Default is an empty string.
xtickformat (str, optional): Format for x-axis ticks. Default is an empty string.
dd_range (list, optional): Range for drawdowns in the histogram. Default is [-50, 0].
y_axis_range (list, optional): Range for the y-axis. Default is None.
log_y (bool, optional): Use logarithmic scale for y-axis. Default is False.
image (str, optional): Image to be displayed as a watermark. Default is an empty string.
Returns#
plotly.graph_objs._figure.Figure: A Plotly figure object representing the interactive chart.
Example#
import pandas as pd
import yfinance as yf
import ds4finance as dsf
# Download historical data for SPY
spy = yf.download("SPY", start="2015-01-01", end="2020-12-31", progress=False)[['Adj Close']]
dsf.ichart(spy, title='SPY', size='auto')
dsf.ichart(spy, title='SPY',
y_position_source='Yahoo finance', yticksuffix='$',
yTitle='Value of SPY (ETF)', size='auto')
dsf.ichart(spy, title='SPY',
y_position_source='Yahoo finance', yticksuffix='$',
yTitle='Value of SPY (ETF)', log_y=True, size='auto')
dsf.ichart(spy, title='SPY',
yticksuffix='$', yTitle='Value of SPY (ETF)',
log_y=True, size='auto',
style='area', source_text='Yahoo Finance')
colors_list=['royalblue', 'darkorange',
'dimgrey', 'rgb(86, 53, 171)', 'rgb(44, 160, 44)',
'rgb(214, 39, 40)', '#ffd166', '#62959c', '#b5179e',
'rgb(148, 103, 189)', 'rgb(140, 86, 75)',
'rgb(227, 119, 194)', 'rgb(127, 127, 127)',
'rgb(188, 189, 34)', 'rgb(23, 190, 207)'] * 10
def ichart(data, title='', colors=colors_list, yTitle='', xTitle='', style='normal',
width=990, height=500, hovermode='x', yticksuffix='', ytickprefix='',
ytickformat="", source_text='', y_position_source=-0.125, xticksuffix='',
xtickprefix='', xtickformat="", dd_range=[-50, 0], y_axis_range=None,
log_y=False, image=''):
"""
Create an interactive chart using Plotly.
Parameters
----------
data : pandas.DataFrame
DataFrame containing data to be plotted.
title : str, optional
Title of the chart. Default is an empty string.
colors : list, optional
List of colors
style:
normal, area, drawdowns_histogram
colors:
color_list or lightcolors
hovermode:
'x', 'x unified', 'closest'
y_position_source:
-0.125 or bellow
dd_range:
[-50, 0]
ytickformat:
".1%"
image:
'fp'
"""
if image == 'fp':
image = 'https://raw.githubusercontent.com/LuisSousaSilva/Articles_and_studies/master/FP-cor-positivo.png'
fig = go.Figure()
fig.update_layout(
...
)
if log_y:
fig.update_yaxes(type="log")
if style == 'normal':
z = -1
for i in data:
z = z + 1
fig.add_trace(go.Scatter(
...
))
if style == 'area':
z = -1
for i in data:
z = z + 1
fig.add_trace(go.Scatter(
...
))
if style == 'drawdowns_histogram':
fig.add_trace(go.Histogram(
...
))
return fig