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
Tools for exploratory data analysis in Python

Dora Exploratory data analysis toolkit for Python. Contents Summary Setup Usage Reading Data & Configuration Cleaning Feature Selection & Extraction V

Nathan Epstein 599 Dec 25, 2022
GUI for visualization and interactive editing of SMPL-family body models ie. SMPL, SMPL-X, MANO, FLAME.

Body Model Visualizer Introduction This is a simple Open3D-based GUI for SMPL-family body models. This GUI lets you play with the shape, expression, a

Muhammed Kocabas 207 Jan 01, 2023
script to generate HeN ipfs app exports of GLSL shaders

HeNerator A simple script to generate HeN ipfs app exports from any frag shader created with: GlslViewer GlslEditor The Book of Shaders glslCanvas VS

Patricio Gonzalez Vivo 22 Dec 21, 2022
Sky attention heatmap of submissions to astrometry.net

astroheat Installation Requires Python 3.6+, Tested with Python 3.9.5 Install library dependencies pip install -r requirements.txt The program require

4 Jun 20, 2022
A napari plugin for visualising and interacting with electron cryotomograms.

napari-tomoslice A napari plugin for visualising and interacting with electron cryotomograms. Installation You can install napari-tomoslice via pip: p

3 Jan 03, 2023
A python wrapper for creating and viewing effects for Matt Parker's christmas tree.

Christmas Tree Visualizer A python wrapper for creating and viewing effects for Matt Parker's christmas tree. Displays py or csv effect files and allo

4 Nov 22, 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 04, 2023
Param: Make your Python code clearer and more reliable by declaring Parameters

Param Param is a library providing Parameters: Python attributes extended to have features such as type and range checking, dynamically generated valu

HoloViz 304 Jan 07, 2023
An interactive dashboard built with python that enables you to visualise how rent prices differ across Sweden.

sweden-rent-dashboard An interactive dashboard built with python that enables you to visualise how rent prices differ across Sweden. The dashboard/web

Rory Crean 5 Dec 19, 2021
Visualize data of Vietnam's regions with interactive maps.

Plotting Vietnam Development Map This is my personal project that I use plotly to analyse and visualize data of Vietnam's regions with interactive map

1 Jun 26, 2022
The Metabolomics Integrator (MINT) is a post-processing tool for liquid chromatography-mass spectrometry (LCMS) based metabolomics.

MINT (Metabolomics Integrator) The Metabolomics Integrator (MINT) is a post-processing tool for liquid chromatography-mass spectrometry (LCMS) based m

Sören Wacker 0 May 04, 2022
eoplatform is a Python package that aims to simplify Remote Sensing Earth Observation by providing actionable information on a wide swath of RS platforms and provide a simple API for downloading and visualizing RS imagery

An Earth Observation Platform Earth Observation made easy. Report Bug | Request Feature About eoplatform is a Python package that aims to simplify Rem

Matthew Tralka 4 Aug 11, 2022
Render Jupyter notebook in the terminal

jut - JUpyter notebook Terminal viewer. The command line tool view the IPython/Jupyter notebook in the terminal. Install pip install jut Usage $jut --

Kracekumar 169 Dec 27, 2022
A high performance implementation of HDBSCAN clustering. http://hdbscan.readthedocs.io/en/latest/

HDBSCAN Now a part of scikit-learn-contrib HDBSCAN - Hierarchical Density-Based Spatial Clustering of Applications with Noise. Performs DBSCAN over va

Leland McInnes 91 Dec 29, 2022
This repository contains a streaming Dataflow pipeline written in Python with Apache Beam, reading data from PubSub.

Sample streaming Dataflow pipeline written in Python This repository contains a streaming Dataflow pipeline written in Python with Apache Beam, readin

Israel Herraiz 9 Mar 18, 2022
Rick and Morty Data Visualization with python

Rick and Morty Data Visualization For this project I looked at data for the TV show Rick and Morty Number of Episodes at a Certain Location Here is th

7 Aug 29, 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 01, 2023
Generate SVG (dark/light) images visualizing (private/public) GitHub repo statistics for profile/website.

Generate daily updated visualizations of GitHub user and repository statistics from the GitHub API using GitHub Actions for any combination of private and public repositories, whether owned or contri

Adam Ross 2 Dec 16, 2022
A filler visualizer built using python

filler-visualizer 42 filler のログをビジュアライズしてスポーツさながら楽しむことができます! Usage (標準入力でvisualizer.pyに渡せばALL OK) 1. 既にあるログをビジュアライズする $ ./filler_vm -t 3 -p1 john_fill

Takumi Hara 1 Nov 04, 2021
🐍PyNode Next allows you to easily create beautiful graph visualisations and animations

PyNode Next A complete rewrite of PyNode for the modern era. Up to five times faster than the original PyNode. PyNode Next allows you to easily create

ehne 3 Feb 12, 2022