<Python, pandas, Highcharts> Highcharts 散布図
Highcharts
での散布図scatter
。
pandas-highcharts
モジュールが、何故かscatter
に対応してない。
よって、自作。
def hic_scatter(x, y, df, hue=None): ''' highchart_scatter, ''' cd_title = '{} vs {}'.format(x, y) cd_xAxis = '{}'.format(x) cd_yAxis = '{}'.format(y) if args['--hue'] == None: cd_data = json.loads(df.to_json(orient='values')) cd_series = [{'name' : cd_xAxis, 'color' : 'rgb(100,100,100)', 'data' : cd_data}] else: cd_series = list() for k, dfg in df.groupby(args['--hue']): cd_data = json.loads(dfg.to_json(orient='values')) cd_series.append({'name' : '{}'.format(k), 'color' : 'rgb(100,100,100)', 'data' : cd_data}) chartdict = { 'chart': {'type': 'scatter', 'zoomType' : 'xy'}, 'title': {'text': cd_title}, 'xAxis': {'title' : { 'enabled' : 'true', 'text' : cd_xAxis }, 'startOnTick' : 'true', 'endOnTick' : 'true', 'showLastLabel' : 'true' }, 'yAxis': {'title' : { 'text' : cd_yAxis }}, 'plotOptions' : { 'scatter' : { 'marker' : { 'radius' : 5, 'states' : { 'hover' : { 'enabled' : 'true', 'lineColor' : 'rgb(100,100,100)' } } }, 'states' : { 'hover' : { 'marker' : { 'enabled' : 'false' } } }, 'tooltip' : { 'formatter' : "function(){ return \'<b>\' + this.series.name + \'<\b><br>\' + this.x + \'<br>\' + this.y;}" } } }, 'series': cd_series } template = ''' <html> <script src='https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js'></script> <script src="http://code.highcharts.com/highcharts.js"></script> <script src="http://code.highcharts.com/modules/exporting.js"></script> <script> $(function (){{ $("#{chart}").highcharts({data}); }}); </script> <body> <div id="{chart}" style="width:100%; height:400px;"></div> </body> </html> ''' return template.format(chart='container', data=json.dumps(chartdict))