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
Scientific Visualization: Python + Matplotlib

An open access book on scientific visualization using python and matplotlib

Nicolas P. Rougier 8.6k Dec 31, 2022
Minimalistic tool to visualize how the routes to a given target domain change over time, feat. Python 3.10 & mermaid.js

Minimalistic tool to visualize how the routes to a given target domain change over time, feat. Python 3.10 & mermaid.js

Péter Ferenc Gyarmati 1 Jan 17, 2022
Practical-statistics-for-data-scientists - Code repository for O'Reilly book

Code repository Practical Statistics for Data Scientists: 50+ Essential Concepts Using R and Python by Peter Bruce, Andrew Bruce, and Peter Gedeck Pub

1.7k Jan 04, 2023
Simple implementation of Self Organizing Maps (SOMs) with rectangular and hexagonal grid topologies

py-self-organizing-map Simple implementation of Self Organizing Maps (SOMs) with rectangular and hexagonal grid topologies. A SOM is a simple unsuperv

Jonas Grebe 1 Feb 10, 2022
A python visualization of the A* path finding algorithm

A python visualization of the A* path finding algorithm. It allows you to pick your start, end location and make obstacles and then view the process of finding the shortest path. You can also choose

Kimeon 4 Aug 02, 2022
A minimalistic wrapper around PyOpenGL to save development time

glpy glpy is pyOpenGl wrapper which lets you work with pyOpenGl easily.It is not meant to be a replacement for pyOpenGl but runs on top of pyOpenGl to

Abhinav 9 Apr 02, 2022
Advanced hot reloading for Python

The missing element of Python - Advanced Hot Reloading Details Reloadium adds hot reloading also called "edit and continue" functionality to any Pytho

Reloadware 1.9k Jan 04, 2023
PolytopeSampler is a Matlab implementation of constrained Riemannian Hamiltonian Monte Carlo for sampling from high dimensional disributions on polytopes

PolytopeSampler PolytopeSampler is a Matlab implementation of constrained Riemannian Hamiltonian Monte Carlo for sampling from high dimensional disrib

9 Sep 26, 2022
This is a web application to visualize various famous technical indicators and stocks tickers from user

Visualizing Technical Indicators Using Python and Plotly. Currently facing issues hosting the application on heroku. As soon as I am able to I'll like

4 Aug 04, 2022
A Python-based non-fungible token (NFT) generator built using Samilla and Matplotlib

PyNFT A Pythonic NF (non-fungible token) generator built using Samilla and Matplotlib Use python pynft.py [amount] The intention behind this generato

Ayush Gundawar 6 Feb 07, 2022
The visual framework is designed on the idea of module and implemented by mixin method

Visual Framework The visual framework is designed on the idea of module and implemented by mixin method. Its biggest feature is the mixins module whic

LEFTeyes 9 Sep 19, 2022
100 Days of Code The Complete Python Pro Bootcamp for 2022

100-Day-With-Python 100 Days of Code - The Complete Python Pro Bootcamp for 2022. In this course, I spend with python language over 100 days, and I up

Rajdip Das 8 Jun 22, 2022
Backend app for visualizing CANedge log files in Grafana (directly from local disk or S3)

CANedge Grafana Backend - Visualize CAN/LIN Data in Dashboards This project enables easy dashboard visualization of log files from the CANedge CAN/LIN

13 Dec 15, 2022
flask extension for integration with the awesome pydantic package

Flask-Pydantic Flask extension for integration of the awesome pydantic package with Flask. Installation python3 -m pip install Flask-Pydantic Basics v

249 Jan 06, 2023
plotly scatterplots which show molecule images on hover!

molplotly Plotly scatterplots which show molecule images on hovering over the datapoints! Required packages: pandas rdkit jupyter_dash ➡️ See example.

150 Dec 28, 2022
A tool for automatically generating 3D printable STLs from freely available lidar scan data.

mini-map-maker A tool for automatically generating 3D printable STLs from freely available lidar scan data. Screenshots Tutorial To use this script, g

Mike Abbott 51 Nov 06, 2022
A python script to visualise explain plans as a graph using graphviz

README Needs to be improved Prerequisites Need to have graphiz installed on the machine. Refer to https://graphviz.readthedocs.io/en/stable/manual.htm

Edward Mallia 1 Sep 28, 2021
Simple Inkscape Scripting

Simple Inkscape Scripting Description In the Inkscape vector-drawing program, how would you go about drawing 100 diamonds, each with a random color an

Scott Pakin 140 Dec 27, 2022
Data visualization electromagnetic spectrum

Datenvisualisierung-Elektromagnetischen-Spektrum Anhand des Moduls matplotlib sollen die Daten des elektromagnetischen Spektrums dargestellt werden. D

Pulsar 1 Sep 01, 2022
Datapane is the easiest way to create data science reports from Python.

Datapane Teams | Documentation | API Docs | Changelog | Twitter | Blog Share interactive plots and data in 3 lines of Python. Datapane is a Python lib

Datapane 744 Jan 06, 2023