a plottling library for python, based on D3

Overview

Hello

August 2013

Hello! Maybe you're looking for a nice Python interface to build interactive, javascript based plots that look as nice as all those d3 plots you've been seeing lately? Well, this repository is not a bad place to start looking. The code herein was an experiment to see if this approach was a good idea and, if it was, what the experience of plotting into the browser from Python would feel like.

All the code should work, more or less, and you are welcome to fork it, muck about with it, and generally get a taste for what this sort of plotting feels like.

You probably don't want to stop reading here, though. Instead, you should go check out vincent which is a much nicer take on this idea, created using vega, and is in general a much more gentlemanly way to go about this sort of thing. It's also being properly updated and developed, unlike the code below.

d3py

This is d3py: a plotting library for python based on d3. The aim of d3py is to provide a simple way to plot data from the command line or simple scripts into a browser window.

d3py accomplishes this by building on two excellent packages. The first is d3.js (Mike Bostock), which is a javascript library for creating data driven documents, which allows us to place arbitrary svg into a browser window. The second is the pandas Python module (Wes Mckinney), which blesses Python with (amongst other things) the DataFrame data structure.

The idioms used to plot data are very simple, and borrow from R's ggplot2 (Hadley Wickham) and Python's matplotlib (John Hunter et al).

Install d3py and dependencies:

  1. easy_install https://github.com/mikedewar/d3py/tarball/master
  2. pip install pandas
  3. pip install numpy
  4. pip install networkx

Example:

  1. create a PandasFigure object around a DataFrame (or a NetworkXFigure object around a Graph)
  2. add geoms to the figure object to plot specific combinations of columns of the data frame.
  3. show the figure, which serves up the figure in a browser window
  4. muck about with the style of the plot using the browser's developer tools
  5. share FTW!

Each geom takes as parameters an appropriate number of column names of the data frame as arguments. For example the Line geom, which has two dimensions, takes an x-value and a y-value. A Point geom, which makes up a scatter plot, has three dimensions and so takes three parameters: x, y and colour (in the future it could take size, too!).

Each geom is styled using css which you can pass in arbitrarily. So, for example, the Point geom comes with a bunch of default styles, but you can also specify fill=red as a keyword argument which will add a custom css line for that set of points which will turn them red. This also means you can style the plot live in the browser using Firebug in Firefox or Chrome's developer tools.

d3py aims to create really simple javascript source code wherever possible, so you can go in and edit the plots to embed them into your own sites if needs be. The .show() method writes an html file containing the basic markup, a css file with the styles for each geom, a json file with the data from the Figure's DataFrame and a js file with the d3 code in it. The strings that generate the js and css files can always be pulled from the Figure object so you can see how d3py builds up your graph.

An example session could like:

import d3py
import pandas
import numpy as np
	
# some test data
T = 100
# this is a data frame with three columns (we only use 2)
df = pandas.DataFrame({
    "time" : range(T),
    "pressure": np.random.rand(T),
    "temp" : np.random.rand(T)
})
## build up a figure, ggplot2 style
# instantiate the figure object
fig = d3py.PandasFigure(df, name="basic_example", width=300, height=300) 
# add some red points
fig += d3py.geoms.Point(x="pressure", y="temp", fill="red")
# writes 3 files, starts up a server, then draws some beautiful points in Chrome
fig.show() 

Check out the examples in the folder for more functionality! Assuming everything is working OK, the examples should generate (something akin to) the following plots:

point

point example

line

line example

bar

bar example

area

area example

Comments
  • How to plot multiple lines

    How to plot multiple lines

    Hello, thanks for this great module! I would like to know the proper technique to plot multiple line on a PandasFigure? What id the proper Data Frame and how to call it? for example: i would like to plot two lines on the same figure defined by x,y. How can i tel d3py to use a dataframe like that: x y 0 [1, 2] [10, 11] 1 [3, 4] [13, 14] thanks

    opened by vallettea 8
  • #57: re-factor to make it easy to deploy and support other web technologies.

    #57: re-factor to make it easy to deploy and support other web technologies.

    ...ies.

    • the displayable module knows how to display figures
    • the deployable module knows how to deploy figures
    • favor os.sep of hardcoding / in path
    • replaced string replace calls with jinja2 template module
    opened by kern3020 6
  • Problem with new examples

    Problem with new examples

    python d3py_bar.py Cleanup after exception: <type 'exceptions.AttributeError'>: 'module' object has no attribute 'xAxis' Cleaning temp files Traceback (most recent call last): File "d3py_bar.py", line 12, in p += d3py.xAxis(x = "apple_type") AttributeError: 'module' object has no attribute 'xAxis' Cleaning temp files Exception AttributeError: "'Bar' object has no attribute 'cleanup'" in <bound method Bar.del of <d3py.geoms.Bar object at 0x272f650>> ignored

    opened by ghost 6
  • error when plotting int or long types

    error when plotting int or long types

    I tried plotting something with x-axis data of type long and it gave me the following error on line 164 of

    TypeError: 0 is not JSON serializable
    

    The line that threw the error is: https://github.com/mikedewar/D3py/blob/master/d3py/d3py.py#L164

    opened by alaiacano 6
  • Server shuts down directly after fig.show()

    Server shuts down directly after fig.show()

    python test.py you can find your chart at http://localhost:8000/basic_example/basic_example.html Shutting down httpd Cleaning temp files

    The browser window opens but the server is already shut down.

    Source code:

    import d3py
    import pandas
    import numpy as np
    # some test data
    T = 100
    # this is a data frame with three columns (we only use 2)
    df = pandas.DataFrame({
        "time" : range(T),
        "pressure": np.random.rand(T),
        "temp" : np.random.rand(T)
    })
    ## build up a figure, ggplot2 style
    # instantiate the figure object
    fig = d3py.Figure(df, name="basic_example", width=300, height=300) 
    # add some red points
    fig += d3py.geoms.Point(x="pressure", y="temp", fill="red")
    # writes 3 files, starts up a server, then draws some beautiful points in Chrome
    fig.show()
    
    opened by ghost 5
  • Addition of Vega syntax generation

    Addition of Vega syntax generation

    The major update is the addition of Vega syntax via incorporation of the Vincent project: https://github.com/wrobstory/vincent

    None of the original API/syntax for building/showing figures has changed- you can still build figures from the ground up using d3py.geoms. Now you can also build them with vega syntax.

    I also did some code commenting and PEP8 cleaning, started to build some more comprehensive tests (need a lot more work), and moved some of the methods in figure.py around so that the class logic flows better for the first-time reader.

    opened by wrobstory 3
  • Can't run d3py_graph.py example: 'NetworkXFigure' object has no attribute 'httpd'

    Can't run d3py_graph.py example: 'NetworkXFigure' object has no attribute 'httpd'

    I cloned d3py and tried to run the d3py_graph.py example, but ran into a problem.

    $ git clone git://github.com/mikedewar/d3py.git
    [...]
    $ cd d3py/
    $ python setup.py install
    [...]
    $ cd examples/
    $ python d3py_graph.py 
    Traceback (most recent call last):
      File "d3py_graph.py", line 15, in <module>
        with d3py.NetworkXFigure(G, width=500, height=500) as p:
      File "[...]/local/lib/python2.7/site-packages/d3py/networkx_figure.py", line 39, in __init__
        port=port, **kwargs
    TypeError: __init__() takes exactly 10 arguments (9 given)
    Error in clean-up: 'NetworkXFigure' object has no attribute 'httpd'
    

    I just discovered d3py a few minutes ago, so forgive me if I've missed something. I got the demo to run like this:

    $ git diff
    diff --git a/examples/d3py_graph.py b/examples/d3py_graph.py
    index 99c73ba..b1d9e6a 100644
    --- a/examples/d3py_graph.py
    +++ b/examples/d3py_graph.py
    @@ -12,6 +12,6 @@ G.add_edge(3,4)
     G.add_edge(4,2)
    
     # use 'with' if you are writing a script and want to serve this up forever
    -with d3py.NetworkXFigure(G, width=500, height=500) as p:
    +with d3py.NetworkXFigure(G, width=500, height=500, host='localhost') as p:
         p += d3py.ForceLayout()
         p.show()
    

    Unlike PandasFigure(Figure), NetworkXFigure(Figure) does not have a default host argument.

    opened by ceball 3
  • print html snippet from d3py

    print html snippet from d3py

    scenario: In python web applications, one would want to insert d3 visualization with d3py by "printing" html snippet to an existing html. For example, googleVis package in R provides such functionality in its print function, which can be used with R markdown to produce html page easily.

    opened by alexdeng 3
  • readme sample doesn't render with geoms.Bar

    readme sample doesn't render with geoms.Bar

    The sample code in the readme works as it is, but if I change line 17 to:

    fig += d3py.geoms.Bar(x="time", y="temp",fill="red")
    

    It fails to render in firefox or chrome. However, all of the requests (js, json, html) return 200 except for the favicon.ico.

    opened by davidthewatson 3
  • Stream files to webserver instead of saving to disk

    Stream files to webserver instead of saving to disk

    As the title says... this should help with ipython compatibility and would vastly simplify cleanup. This could be done with simple modifications to the figure object and a new HTTPServer object (HTTPFileStreamServer?).

    opened by mynameisfiber 2
  • Fixed host arguments in NetworkXFigure

    Fixed host arguments in NetworkXFigure

    Added host argument to NetworkXFigure prototype, and to the internal sup...erclass call.

    This fixes a bug in which the NetworkXFigure could not be drawn, due to an incorrect number of passed arguments to the superclass.

    opened by widdowquinn 1
  • docs: fix simple typo, sandard -> standard

    docs: fix simple typo, sandard -> standard

    There is a small typo in d3py/figure.py.

    Should read standard rather than sandard.

    Semi-automated pull request generated by https://github.com/timgates42/meticulous/blob/master/docs/NOTE.md

    opened by timgates42 0
  • Cannot see the output html: Shutting down httpd

    Cannot see the output html: Shutting down httpd

    I tried to run the example code:

    import d3py
    import pandas
    import numpy as np
    
    # some test data
    T = 100
    # this is a data frame with three columns (we only use 2)
    df = pandas.DataFrame({
        "time": range(T),
        "pressure": np.random.rand(T),
        "temp": np.random.rand(T)
    })
    ## build up a figure, ggplot2 style
    # instantiate the figure object
    fig = d3py.PandasFigure(df, name="basic_example", width=300, height=300)
    # add some red points
    fig += d3py.geoms.Point(x="pressure", y="temp", fill="red")
    # writes 3 files, starts up a server, then draws some beautiful points in Chrome
    fig.show() 
    

    but failed:

    C:\Python27\python.exe J:/github_repos/DeepSep/aaa.py
    You can find your chart at http://localhost:8000/basic_example.html
    Shutting down httpd
    
    Process finished with exit code 0
    

    Have any ideas? Thanks.

    opened by hsluoyz 0
  •  Cannot read property 'weight' of undefined

    Cannot read property 'weight' of undefined

    image image `import d3py import networkx as nx

    import logging logging.basicConfig(level=logging.DEBUG)

    G=nx.Graph() G.add_edge(1,2) G.add_edge(1,3) G.add_edge(3,2) G.add_edge(3,4) G.add_edge(4,2)

    use 'with' if you are writing a script and want to serve this up forever

    with d3py.NetworkXFigure(G, width=500, height=500) as p: p += d3py.ForceLayout() p.show() `

    opened by 101hanbin 0
  • Few issues

    Few issues

    You need to setup ipython to the requirements along with networkx

    You also need to modify the example specifically d3py_vega_scatter.py to import numpy as np

    opened by andersonpaac 0
Releases(0.11.2)
Owner
Mike Dewar
Vice President of Data Science at MasterCard
Mike Dewar
Custom Plotly Dash components based on Mantine React Components library

Dash Mantine Components Dash Mantine Components is a Dash component library based on Mantine React Components Library. It makes it easier to create go

Snehil Vijay 239 Jan 8, 2023
Visual Python is a GUI-based Python code generator, developed on the Jupyter Notebook environment as an extension.

Visual Python is a GUI-based Python code generator, developed on the Jupyter Notebook environment as an extension.

Visual Python 564 Jan 3, 2023
Declarative statistical visualization library for Python

Altair http://altair-viz.github.io Altair is a declarative statistical visualization library for Python. With Altair, you can spend more time understa

Altair 8k Jan 5, 2023
Cartopy - a cartographic python library with matplotlib support

Cartopy is a Python package designed to make drawing maps for data analysis and visualisation easy. Table of contents Overview Get in touch License an

null 1.2k Jan 1, 2023
A Python Library for Self Organizing Map (SOM)

SOMPY A Python Library for Self Organizing Map (SOM) As much as possible, the structure of SOM is similar to somtoolbox in Matlab. It has the followin

Vahid Moosavi 497 Dec 29, 2022
Multi-class confusion matrix library in Python

Table of contents Overview Installation Usage Document Try PyCM in Your Browser Issues & Bug Reports Todo Outputs Dependencies Contribution References

Sepand Haghighi 1.3k Dec 31, 2022
NorthPitch is a python soccer plotting library that sits on top of Matplotlib

NorthPitch is a python soccer plotting library that sits on top of Matplotlib.

Devin Pleuler 30 Feb 22, 2022
The interactive graphing library for Python (includes Plotly Express) :sparkles:

plotly.py Latest Release User forum PyPI Downloads License Data Science Workspaces Our recommended IDE for Plotly’s Python graphing library is Dash En

Plotly 12.7k Jan 5, 2023
🎨 Python Echarts Plotting Library

pyecharts Python ❤️ ECharts = pyecharts English README ?? 简介 Apache ECharts (incubating) 是一个由百度开源的数据可视化,凭借着良好的交互性,精巧的图表设计,得到了众多开发者的认可。而 Python 是一门富有表达

pyecharts 13.1k Jan 3, 2023
Declarative statistical visualization library for Python

Altair http://altair-viz.github.io Altair is a declarative statistical visualization library for Python. With Altair, you can spend more time understa

Altair 6.4k Feb 13, 2021
Python library that makes it easy for data scientists to create charts.

Chartify Chartify is a Python library that makes it easy for data scientists to create charts. Why use Chartify? Consistent input data format: Spend l

Spotify 3.2k Jan 4, 2023
:small_red_triangle: Ternary plotting library for python with matplotlib

python-ternary This is a plotting library for use with matplotlib to make ternary plots plots in the two dimensional simplex projected onto a two dime

Marc 611 Dec 29, 2022
The interactive graphing library for Python (includes Plotly Express) :sparkles:

plotly.py Latest Release User forum PyPI Downloads License Data Science Workspaces Our recommended IDE for Plotly’s Python graphing library is Dash En

Plotly 8.9k Feb 18, 2021
🎨 Python Echarts Plotting Library

pyecharts Python ❤️ ECharts = pyecharts English README ?? 简介 Apache ECharts (incubating) 是一个由百度开源的数据可视化,凭借着良好的交互性,精巧的图表设计,得到了众多开发者的认可。而 Python 是一门富有表达

pyecharts 10.6k Feb 18, 2021
Declarative statistical visualization library for Python

Altair http://altair-viz.github.io Altair is a declarative statistical visualization library for Python. With Altair, you can spend more time understa

Altair 6.4k Feb 18, 2021
Python library that makes it easy for data scientists to create charts.

Chartify Chartify is a Python library that makes it easy for data scientists to create charts. Why use Chartify? Consistent input data format: Spend l

Spotify 2.8k Feb 18, 2021
:small_red_triangle: Ternary plotting library for python with matplotlib

python-ternary This is a plotting library for use with matplotlib to make ternary plots plots in the two dimensional simplex projected onto a two dime

Marc 391 Feb 17, 2021
A Python library created to assist programmers with complex mathematical functions

libmaths was created not only as a learning experience for me, but as a way to make mathematical models in seconds for Python users using mat

Simple 73 Oct 2, 2022
Python library that makes it easy for data scientists to create charts.

Chartify Chartify is a Python library that makes it easy for data scientists to create charts. Why use Chartify? Consistent input data format: Spend l

Spotify 3.2k Jan 1, 2023