In [1]:
%load_ext autoreload
%autoreload 2
import numpy as np
import pandas as pd
from IPython.display import display
import ezhc as hc
In [2]:
%load_ext version_information
%version_information pandas, ipython, jupyter, ezhc
Out[2]:
plot()
has the following arguments:
save=True
and optionally save_name
and optionally save_path
(default='saved') will save the graph as a stand alone HTML doc under save_path
after creating it if necessarynotebook
(default=True) will not inject require
and jquery
libs as they are already available in the classical notebook. Set to False to inject them.version
(default='latest') will specify the highcharts version to use. It is recommended to leave the default value (6.1.2 as of 4sep18).proxy
(default=None') is necessary if you want to check from highcharts release page what the latest version is, and update the list of all past versions. If no proxy is provided, the versions are hardcoded in the source code.options_as_dict()
will return highchart/highstocks options as a Python dictionary
chart_id
to specify which div for renderingoptions_as_json()
will return highchart/highstocks options as jsonplot()
In [3]:
df = hc.sample.df_timeseries(N=2, Nb_bd=15+0*3700) #<=473
df.info()
display(df.head())
display(df.tail())
In [4]:
g = hc.Highstock()
g.chart.width = 650
g.chart.height = 550
g.legend.enabled = True
g.legend.layout = 'horizontal'
g.legend.align = 'center'
g.legend.maxHeight = 100
g.tooltip.enabled = True
g.tooltip.valueDecimals = 2
g.exporting.enabled = True
g.chart.zoomType = 'xy'
g.title.text = 'Time series plotted with HighStock'
g.subtitle.text = 'Transparent access to the underlying js lib'
g.plotOptions.series.compare = 'percent'
g.yAxis.labels.formatter = hc.scripts.FORMATTER_PERCENT
g.tooltip.pointFormat = hc.scripts.TOOLTIP_POINT_FORMAT_PERCENT
g.tooltip.positioner = hc.scripts.TOOLTIP_POSITIONER_CENTER_TOP
g.xAxis.gridLineWidth = 1.0
g.xAxis.gridLineDashStyle = 'Dot'
g.yAxis.gridLineWidth = 1.0
g.yAxis.gridLineDashStyle = 'Dot'
g.credits.enabled = True
g.credits.text = 'Source: XXX Flow Strategy & Solutions.'
g.credits.href = 'http://www.example.com'
g.series = hc.build.series(df)
g.plot(save=False, version='latest', center=True)
## IF BEHIND A CORPORATE PROXY
## IF NOT PROXY IS PASSED TO .plot() THEN NO HIGHCHARTS VERSION UPDATE IS PERFORMED
## HARDODED VERSIONS ARE USED INSTEAD
# p = hc.Proxy('mylogin', 'mypwd', 'myproxyhost', 'myproxyport')
# g.plot(save=False, version='latest', proxy=p)
Out[4]:
In [5]:
options_as_dict = g.options_as_dict()
options_as_dict
Out[5]:
In [6]:
options_as_json = g.options_as_json()
options_as_json
Out[6]:
In [7]:
df = hc.sample.df_timeseries(N=3, Nb_bd=2000)
df['Cash'] = 1.0+0.02/260
df['Cash'] = df['Cash'].cumprod()
display(df.head())
display(df.tail())
In [8]:
g = hc.Highstock()
g.chart.height = 550
g.legend.enabled = True
g.legend.layout = 'horizontal'
g.legend.align = 'center'
g.legend.maxHeight = 100
g.tooltip.enabled = True
g.tooltip.valueDecimals = 2
g.exporting.enabled = True
g.chart.zoomType = 'xy'
g.title.text = 'Time series plotted with HighStock'
g.subtitle.text = 'Transparent access to the underlying js lib'
g.plotOptions.series.compare = 'percent'
g.yAxis.labels.formatter = hc.scripts.FORMATTER_PERCENT
g.tooltip.pointFormat = hc.scripts.TOOLTIP_POINT_FORMAT_PERCENT
g.tooltip.positioner = hc.scripts.TOOLTIP_POSITIONER_CENTER_TOP
g.xAxis.gridLineWidth = 1.0
g.xAxis.gridLineDashStyle = 'Dot'
g.yAxis.gridLineWidth = 1.0
g.yAxis.gridLineDashStyle = 'Dot'
g.credits.enabled = True
g.credits.text = 'Source: XXX Flow Strategy & Solutions.'
g.credits.href = 'http://www.example.com'
g.series = hc.build.series(df, visible={'Track3': False})
g.plot(save=True, save_name='NoTable')
Out[8]:
plot_with_table1()
and plot_with_table2()
are exceptions with respect to the idea of this module: It is NOT just transparent access to Highchart/Highstock. I added a table (based on datatable.net) to display more data about the period selected. This measurements cannot be calculated beforehand, so it has to be postprocessing. save=True
function plot_with_table1/2()
will create a standalone HTML file containing the output in subdirectory 'saved'. Optionally save_name
can be set - an automatic time tag is added to keep things orderly, unless dated=False
.
In [9]:
g.plot_with_table_1(dated=False, save=True, save_name='Table1')
Out[9]:
In [10]:
g.plotOptions.series.compare = 'value'
g.yAxis.labels.formatter = hc.scripts.FORMATTER_BASIC
g.tooltip.pointFormat = hc.scripts.TOOLTIP_POINT_FORMAT_BASIC
g.tooltip.formatter = hc.scripts.FORMATTER_QUANTILE
disclaimer = """
THE VALUE OF YOUR INVESTMENT MAY FLUCTUATE.
THE FIGURES RELATING TO SIMULATED PAST PERFORMANCES REFER TO PAST
PERIODS AND ARE NOT A RELIABLE INDICATOR OF FUTURE RESULTS.
THIS ALSO APPLIES TO HISTORICAL MARKET DATA.
"""
template_footer = hc.scripts.TEMPLATE_DISCLAIMER
create_footer = hc.scripts.from_template
logo_path = hc.scripts.PATH_TO_LOGO_SG
# logo_path = 'http://img.talkandroid.com/uploads/2015/11/Chrome-Logo.png'
# logo_path = hc.scripts.image_src('http://img.talkandroid.com/uploads/2015/11/Chrome-Logo.png')
footer = create_footer(template_footer, comment=disclaimer, img_logo=logo_path)
g.plot_with_table_2(dated=False, save=True, save_name='Table2', footer=footer)
Out[10]:
In [11]:
df = hc.sample.df_one_idx_several_col()
df
Out[11]:
In [12]:
g = hc.Highcharts()
g.chart.type = 'column'
g.chart.width = 500
g.chart.height = 300
# g.plotOptions.column.animation = False
g.title.text = 'Basic Bar Chart'
g.yAxis.title.text = 'Fruit Consumption'
g.xAxis.categories = list(df.index)
g.series = hc.build.series(df)
g.plot(center=True, save=True, save_name='test', dated=False)
Out[12]:
In [13]:
g.plotOptions.column.stacking = 'normal'
g.title.text = 'Stack Bar Chart'
g.yAxis.title.text = 'Total Fruit Consumption'
g.plot()
Out[13]:
In [14]:
g.plotOptions.column.stacking = 'percent'
g.yAxis.title.text = 'Fruit Consumption Distribution'
g.plot()
Out[14]:
In [15]:
g = hc.Highcharts()
g.chart.type = 'bar'
g.chart.width = 500
g.chart.height = 400
g.title.text = 'Basic Bar Chart'
g.xAxis.title.text = 'Fruit Consumption'
g.xAxis.categories = list(df.index)
g.series = hc.build.series(df)
g.plot()
Out[15]:
In [16]:
g.plotOptions.bar.stacking = 'normal'
g.title.text = 'Stacked Bar Chart'
g.xAxis.title.text = 'Total Fruit Consumption'
g.plot()
Out[16]:
In [17]:
g.plotOptions.bar.stacking = 'percent'
g.title.text = 'Stacked Bar Chart'
g.xAxis.title.text = 'Fruit Consumption Distribution'
g.plot()
Out[17]:
In [18]:
df = hc.sample.df_one_idx_one_col()
df
Out[18]:
In [19]:
g = hc.Highcharts()
g.chart.type = 'pie'
g.chart.width = 400
g.chart.height = 400
gpo = g.plotOptions.pie
gpo.showInLegend = True
gpo.dataLabels.enabled = False
g.title.text = 'Browser Market Share'
g.series = hc.build.series(df)
g.plot()
Out[19]:
In [20]:
g.chart.width = 400
g.chart.height = 300
gpo.showInLegend = False
gpo.dataLabels.enabled = True
gpo.startAngle = -90
gpo.endAngle = 90
gpo.innerSize = '40%'
gpo.center = ['50%', '95%']
g.plot()
Out[20]:
In [21]:
df = hc.sample.df_two_idx_one_col()
df.head()
Out[21]:
In [22]:
g = hc.Highcharts()
g.chart.type = 'pie'
g.chart.width = 500
g.chart.height = 500
g.exporting = False
gpo = g.plotOptions.pie
gpo.showInLegend = False
gpo.dataLabels.enabled = True
gpo.center = ['50%', '50%']
gpo.size = '65%'
g.drilldown.drillUpButton.position = {'x': 0, 'y': 0}
g.title.text = 'Browser Market Share'
g.series, g.drilldown.series = hc.build.series_drilldown(df)
g.plot()
Out[22]:
In [23]:
g = hc.Highcharts()
g.chart.type = 'bar'
g.chart.width = 500
g.chart.height = 500
g.exporting = False
gpo = g.plotOptions.pie
gpo.showInLegend = False
gpo.dataLabels.enabled = True
gpo.center = ['50%', '50%']
gpo.size = '65%'
g.drilldown.drillUpButton.position = {'x': 0, 'y': 0}
g.title.text = 'Browser Market Share'
g.series, g.drilldown.series = hc.build.series_drilldown(df)
g.plot()
Out[23]:
In [24]:
df = hc.sample.df_several_idx_one_col_2()
df.head()
Out[24]:
In [25]:
g = hc.Highcharts()
g.chart.type = 'pie'
g.chart.width = 500
g.chart.height = 500
g.exporting = False
gpo = g.plotOptions.pie
gpo.showInLegend = False
gpo.dataLabels.enabled = True
gpo.center = ['50%', '50%']
gpo.size = '65%'
g.drilldown.drillUpButton.position = {'x': 0, 'y': 0}
g.title.text = 'World Population'
g.series, g.drilldown.series = hc.build.series_drilldown(df, top_name='World')
g.plot()
Out[25]:
In [26]:
df = hc.sample.df_one_idx_two_col()
df.head()
Out[26]:
In [27]:
g = hc.Highcharts()
g.chart.type = 'columnrange'
g.chart.inverted = True
g.chart.width = 700
g.chart.height = 400
gpo = g.plotOptions.columnrange
gpo.dataLabels.enabled = True
gpo.dataLabels.formatter = 'function() { return this.y + "°C"; }'
g.tooltip.valueSuffix = '°C'
g.xAxis.categories, g.series = hc.build.series_range(df)
g.series[0]['name'] = 'Temperature'
g.yAxis.title.text = 'Temperature (°C)'
g.xAxis.title.text = 'Month'
g.title.text = 'Temperature Variations by Month'
g.subtitle.text = 'Vik, Norway'
g.legend.enabled = False
g.plot(save=True, save_name='index', dated=False, notebook=False)
Out[27]:
In [28]:
df = hc.sample.df_scatter()
df.head()
Out[28]:
In [29]:
g = hc.Highcharts()
g.chart.type = 'scatter'
g.chart.width = 700
g.chart.height = 500
g.chart.zoomType = 'xy'
g.exporting = False
g.plotOptions.scatter.marker.radius = 5
g.tooltip.headerFormat = '<b>Sex: {series.name}</b><br>'
g.tooltip.pointFormat = '{point.x} cm, {point.y} kg'
g.legend.layout = 'vertical'
g.legend.align = 'left'
g.legend.verticalAlign = 'top'
g.legend.x = 100
g.legend.y = 70
g.legend.floating = True
g.legend.borderWidth = 1
g.xAxis.title.text = 'Height (cm)'
g.yAxis.title.text = 'Weight (kg)'
g.title.text = 'Height Versus Weight of 507 Individuals by Gender'
g.subtitle.text = 'Source: Heinz 2003'
g.series = hc.build.series_scatter(df, color_column='Sex',
color={'Female': 'rgba(223, 83, 83, .5)',
'Male': 'rgba(119, 152, 191, .5)'})
g.plot()
Out[29]:
In [30]:
df = hc.sample.df_scatter()
df['Tag'] = np.random.choice(range(int(1e5)), size=len(df), replace=False)
df.head()
Out[30]:
In [31]:
g = hc.Highcharts()
g.chart.type = 'scatter'
g.chart.width = 700
g.chart.height = 500
g.chart.zoomType = 'xy'
g.exporting = False
g.plotOptions.scatter.marker.radius = 5
g.tooltip.headerFormat = '<b>Sex: {series.name}</b><br><b>Tag: {point.key}</b><br>'
g.tooltip.pointFormat = '{point.x} cm, {point.y} kg'
g.legend.layout = 'vertical'
g.legend.align = 'left'
g.legend.verticalAlign = 'top'
g.legend.x = 100
g.legend.y = 70
g.legend.floating = True
g.legend.borderWidth = 1
g.xAxis.title.text = 'Height (cm)'
g.yAxis.title.text = 'Weight (kg)'
g.title.text = 'Height Versus Weight of 507 Individuals by Gender'
g.subtitle.text = 'Source: Heinz 2003'
g.series = hc.build.series_scatter(df, color_column='Sex', title_column='Tag',
color={'Female': 'rgba(223, 83, 83, .5)',
'Male': 'rgba(119, 152, 191, .5)'})
g.plot()
Out[31]:
In [32]:
df = hc.sample.df_bubble()
df.head()
Out[32]:
In [33]:
g = hc.Highcharts()
g.chart.type = 'bubble'
g.chart.width = 700
g.chart.height = 500
g.chart.zoomType = 'xy'
g.plotOptions.bubble.minSize = 20
g.plotOptions.bubble.maxSize = 60
g.legend.enabled = True
g.title.text = 'Bubbles'
g.series = hc.build.series_bubble(df, color={'A': 'rgba(223, 83, 83, .5)', 'B': 'rgba(119, 152, 191, .5)'})
g.plot()
Out[33]:
In [34]:
df = hc.sample.df_several_idx_one_col()
df.head()
Out[34]:
In [35]:
colors = ['#7cb5ec', '#434348', '#90ed7d', '#f7a35c', '#8085e9',
'#f15c80', '#e4d354', '#2b908f', '#f45b5b', '#91e8e1']
points = hc.build.series_tree(df, set_color=True, colors=colors, set_value=True, precision=2)
points[:5]
Out[35]:
In [36]:
g = hc.Highcharts()
g.chart.type = 'treemap'
g.chart.width = 900
g.chart.height = 600
g.title.text = 'Global Mortality Rate 2012, per 100 000 population'
g.subtitle.text = 'Click points to drill down.\nSource: \
<a href="http://apps.who.int/gho/data/node.main.12?lang=en">WHO</a>.'
g.exporting = False
g.series = [{
'type': "treemap",
'layoutAlgorithm': 'squarified',
'allowDrillToNode': True,
'dataLabels': {
'enabled': False
},
'levelIsConstant': False,
'levels': [{
'level': 1,
'dataLabels': {
'enabled': True
},
'borderWidth': 3
}],
'data': points,
}]
g.plot()
Out[36]: