Code Monkey home page Code Monkey logo

pandas-highcharts's Introduction

pandas-highcharts

https://travis-ci.org/gtnx/pandas-highcharts.svg?branch=master

What is it

pandas-highcharts is a Python package which allows you to easily build Highcharts plots with pandas.DataFrame objects.

Motivation

  • pandas is the best tool to handle data in Python
  • pandas is able to produce matplotlib plots. They work pretty well but have two major drawbacks
    • Not very web friendly
    • Pretty ugly
  • Highcharts produce nice, interactive plot in your browser and is very complete

Features

  • Same interface as DataFrame.plot
  • Following parameters are handled
    • data
    • x
    • y
    • kind
    • figsize
    • use_index
    • title
    • grid
    • legend
    • style
    • logx
    • logy
    • loglog
    • xticks
    • yticks
    • xlim
    • ylim
    • rot
    • fontsize
    • position
    • stacked
    • sort_columns
    • secondary_y
    • mark_right
  • Following parameters are not handled (yet) :
    • ax
    • ay
    • subplots
    • sharex
    • sharey
    • layout
    • colormap
    • colorbar
    • layout
    • table
    • yerr
    • xerr
    • kwds
  • You can specify those specific highcharts parameters:
    • tooltip
  • Static files (highcharts.js) are not embedded

Installation

Install the package using pip

pip install pandas-highcharts

Usage

Import it in your views

import pandas_highcharts
df = ... # create your dataframe here
chart = pandas_highcharts.serialize(df, render_to='my-chart', output_type='json')

In your templates

<div id="my-chart"></div>
<script type="text/javascript">
  new Highcharts.Chart({{chart|safe}});
</script>

Contributing

See CONTRIBUTING.rst for information on how to contribute to pandas-highcharts.

More examples

Some examples are available on nbviewer.

Please read the doc for DataFrame.plot.

For example, with the following dataset:

import pandas as pd
from pandas_highcharts.core import serialize
from pandas.compat import StringIO
dat = """ts;A;B;C
2015-01-01 00:00:00;27451873;29956800;113
2015-01-01 01:00:00;20259882;17906600;76
2015-01-01 02:00:00;11592256;12311600;48
2015-01-01 03:00:00;11795562;11750100;50
2015-01-01 04:00:00;9396718;10203900;43
2015-01-01 05:00:00;14902826;14341100;53"""
df = pd.read_csv(StringIO(dat), sep=';', index_col='ts', parse_dates='ts')

# Basic line plot
chart = serialize(df, render_to="my-chart", title="My Chart")
# Basic column plot
chart = serialize(df, render_to="my-chart", title="Test", kind="bar")
# Basic column plot
chart = serialize(df, render_to="my-chart", title="Test", kind="barh")
# Plot C on secondary axis
chart = serialize(df, render_to="my-chart", title="Test", secondary_y = ["C"])
# Plot on a 1000x700 div
chart = serialize(df, render_to="my-chart", title="Test", figsize = (1000, 700))

pandas-highcharts's People

Contributors

garaud avatar gtnx avatar iapain avatar spookylukey avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pandas-highcharts's Issues

TypeError: 0 is not JSON serializable

When I ran tests.py, I've this strange error. It occurred from the first line of the CoreTest.test_type test case.

It seems that the ouput dict contains some integers which are integers of type np.int64. This numpy int type is not JSON serializable. Call serialize on your DataFrame from the tests.py file with the output_type="json"`parameter and do:

import json
# 'df' from tests.py
json_data = serialize(df, render_to="chart", output_type="json")
# json.dumps(json_data) # raises the TypeError
type(json_data["series"][0]["data"][0][0])
# => numpy.int64

Unfortunately, when I read the code, the serialize_series function specifically, I cannot see where you can have such type.

Sample dataframe

Hello,

maybe you should put in doc a sample DataFrame

with

dat = """ts;A;B;C
2015-01-01 00:00:00;27451873;29956800;113
2015-01-01 01:00:00;20259882;17906600;76
2015-01-01 02:00:00;11592256;12311600;48
2015-01-01 03:00:00;11795562;11750100;50
2015-01-01 04:00:00;9396718;10203900;43
2015-01-01 05:00:00;14902826;14341100;53"""
from pandas.compat import StringIO
df = pd.read_csv(StringIO(dat), sep=';', index_col='ts', parse_dates='ts')

or with a random walk

N = 600
index = pd.date_range('2015-01-01', periods=N, freq='1D')
df = pd.DataFrame(np.random.randn(N, 3), index=index, columns=list('ABC'))
df = df.cumsum() + 800

it will be easier to give pandas-highcharts a try.

Here is a link to show it
http://nbviewer.ipython.org/github/scls19fr/pandas_timeseries_notebooks_samples/blob/master/pandas-highcharts.ipynb

Kind regards

Adding PlotLines with JavaScript after Render

I am having issues adding PlotLines onto the charts after rendering into my template.

This code adds a plotline:

chart.xAxis[0].addPlotLine({
                   value: 1000,
                   color: 'red',
                   width: 2,
                   id: 'plot-line-1',
                   zIndex: 3,
 });

The plot lines are being added to the html, but with some funky characteristics:

<g class="highcharts-plot-lines-0">
    <path fill="none" stroke="red" stroke-width="200" visibility="hidden"></path>
</g>

Namely:

  1. Visibility is set to "hidden"
  2. There is no 'd' attribute in the path

Is this likely to be a Highcharts failure?

Making a Highchart object

I was thinking about refactoring your code a little bit by making a Highchart object as in class Highchart(object): and would make the serialize function use this object and behave the exact same way it does at the moment.

I want to do that to be able to generate the object (without using the serialize function), alter the data structure at will, and then render it my way.

Would you review / accept such a PR?

Upgrading to Python 3 and adding support for pie chart

Hi ,

I downloaded this package and installed it for Python 3.4. it did not work.

I changed core.py module to

  • Work with Python 3
  • Made APIs more generic cab called by pandas or non-panda by passing X_LIST and Y_LIST to be plotted as two separate list.
  • Added support for 'pie' plot.

Do you want me to submit code to you so that yo can make new version.
If not is it okay to create branch 'yieldfrom-pandas-highchart' and add changes there ?

Regards,
Shivaram

Jupyterlab doesn't render figures

Used in Jupyterlab, pandas_highcharts figures do not render. Repro in Jupyterlab:

[1]  import pandas as pd;
from pandas_highcharts.display import display_charts

df = pd.read_csv(...)
display_charts(df, title='Title')

Output is simply an empty cell.

I expect this is related to Jupyterlab's restriction of Javascript execution and requirement of a Jupyterlab extension: jupyterlab/jupyterlab#3748

failing to import display_charts from python (v2.7.10), working fine with %load from IPython

  1. Thank you very much for your work. Combination of pandas with highcharts makes the graphic creation on the webpage a very entertaining exercise.

Nevertheless under OSX I have notices a problem when importing the display_charts.
Everything appears to work as expected when using the IPython (v4.1.2). When trying to use the examples from pure python (v2.7.10) I see the following error:

# > python  app2.py

Traceback (most recent call last):
  File "app2.py", line 11, in <module>
    from pandas_highcharts.display import display_charts
  File "build/bdist.macosx-10.11-x86_64/egg/pandas_highcharts/display.py", line 32, in <module>
AttributeError: 'NoneType' object has no attribute 'config'

The app2.py code presents itself as:

from flask import Flask, render_template
import pandas as pd
from pandas_highcharts.core import serialize
import ConfigParser
import os

from pandas_highcharts.display import display_charts
[..]

Am I missing something ?

Thank you in advance for your comments / support.

Cheers,
ManieqZ

How do I download a notebook as HTML?

Hey there, I'm trying to download the example notebooks with the plots as HTML and the charts won't show - there are a bunch of ERR_FILE_NOT_FOUND errors in the JS console.

Am I missing an intermediary step to be able to do this?

Cheers,
Sam

JSONEncoder: time.mktime raises an error on Windows

Python 2.7.7 32bits on Windows 7 with the Anaconda distribution (I don't like this OS for dev but, it's for work you know)

Raised OverflowError: mktime argument out of range

If the input value cannot be represented as a valid time, either OverflowError or ValueError will be
raised (which depends on whether the invalid value is caught by Python or the underlying C libraries

display_charts() for two different style and secondary_y

Hi,
Thank you for providing this useful tool.
I am using it for plotting bar with line and have some problem with it. But seems it will show in two separate graph rather than one graph with two yaxis. My code is like this
display_charts(df['A'], kind="bar", title="Germany inflation rate")
display_charts(df['B'], kind="line", title="Germany inflation rate",secondary_y = [True])

So could you help solve this problem?

Thanks!

Not able to install

        open("requirements.txt", "r").readlines()
    IOError: [Errno 2] No such file or directory: 'requirements.txt'
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):

  File "<string>", line 16, in <module>

  File "....../build/pandas-highcharts/setup.py", line 14, in <module>

    open("requirements.txt", "r").readlines()

IOError: [Errno 2] No such file or directory: 'requirements.txt'

Minor fix to sample notebook for Python 2 and 3 compatibility

Hello,

I noticed some minor fixes to sample notebook for Python 3 (Pandas Highcharts Example - example.ipynb)

from StringIO import StringIO

should be

from pandas.compat import StringIO

and

from urllib2 import urlopen

should be

from pandas.io.common import urlopen

in display.py

from core import serialize

should be changed to

from .core import serialize

xrange should also be changed to range with

from pandas.compat import range

Kind regards

Export/Print Chart Option not showing up

I replicated the example notebook step by step, and while the chart renders perfectly - the top-right icon to export the chart in various file options (which is a default for highcharts) doesn't appear. All the notebook examples show that option to be present. Have there been some changes recently?

image

Update IPython notebook example

  • the serialize function raised a JSONEncoder error for your first chart example (data from Quandl)
    (I use pandas 0.15.2 but I don't know if it's related)
  • If you convert the string date representation from Quandl data with pd.read_csv([...], parse_dates=[0]) (suppose "Date" is the first column), your serializer works very well and turns dates into a JSON format that Highchart can understand (you don't have to convert string into a int I guess)
  • The first display(HTML("""[highchart <script>]""")) cell have a%` but nothing to replace in the string.
  • I wonder why you call locals() instead of a dict with a simple key,value with your chart.
  • Good job!!

import in __init__ missing

I think you need at least from core import serialize in init

Else I cannot use pandas_highcharts.serialize

Additional Chart types

Hi,

This is great work. Are you planning to add any additional chart types and some of the other HC features any time soon? I'm particularly interested in rendering some big data charts that will need to look a bit like this.

http://jsfiddle.net/ttqemvzs/

Is there a template that can be edited fairly easily to add custom charts ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.