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
Generate graphs with NetworkX, natively visualize with D3.js and pywebview

webview_d3 This is some PoC code to render graphs created with NetworkX natively using D3.js and pywebview. The main benifit of this approac

byt3bl33d3r 68 Aug 18, 2022
Data aggregated from the reports found at the MCPS COVID Dashboard into a set of visualizations.

Montgomery County Public Schools COVID-19 Visualizer Contents About this project Data Support this project About this project Data All data we use can

James 3 Jan 19, 2022
A blender import/export system for Defold

defold-blender-export A Blender export system for the Defold game engine. Setup Notes There are no exhaustive documents for this tool yet. Its just no

David Lannan 27 Dec 30, 2022
An interactive UMAP visualization of the MNIST data set.

Code for an interactive UMAP visualization of the MNIST data set. Demo at https://grantcuster.github.io/umap-explorer/. You can read more about the de

grant 70 Dec 27, 2022
termplotlib is a Python library for all your terminal plotting needs.

termplotlib termplotlib is a Python library for all your terminal plotting needs. It aims to work like matplotlib. Line plots For line plots, termplot

Nico Schlömer 553 Dec 30, 2022
Automatization of BoxPlot graph usin Python MatPlotLib and Excel

BoxPlotGraphAutomation Automatization of BoxPlot graph usin Python / Excel. This file is an automation of BoxPlot-Graph using python graph library mat

EricAugustin 1 Feb 07, 2022
An application that allows you to design and test your own stock trading algorithms in an attempt to beat the market.

StockBot is a Python application for designing and testing your own daily stock trading algorithms. Installation Use the

Ryan Cullen 280 Dec 19, 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
Profile and test to gain insights into the performance of your beautiful Python code

Profile and test to gain insights into the performance of your beautiful Python code View Demo - Report Bug - Request Feature QuickPotato in a nutshel

Joey Hendricks 138 Dec 06, 2022
MPL Plotter is a Matplotlib based Python plotting library built with the goal of delivering publication-quality plots concisely.

MPL Plotter is a Matplotlib based Python plotting library built with the goal of delivering publication-quality plots concisely.

Antonio López Rivera 162 Nov 11, 2022
PyPassword is a simple follow up to PyPassphrase

PyPassword PyPassword is a simple follow up to PyPassphrase. After finishing that project it occured to me that while some may wish to use that option

Scotty 2 Jan 22, 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
LinkedIn connections analyzer

LinkedIn Connections Analyzer 🔗 https://linkedin-analzyer.herokuapp.com Hey hey 👋 , welcome to my LinkedIn connections analyzer. I recently found ou

Okkar Min 5 Sep 13, 2022
A simple project on Data Visualization for CSCI-40 course.

Simple-Data-Visualization A simple project on Data Visualization for CSCI-40 course - the instructions can be found here SAT results in New York in 20

Hugo Matousek 8 Oct 27, 2021
IPython/Jupyter notebook module for Vega and Vega-Lite

IPython Vega IPython/Jupyter notebook module for Vega 5, and Vega-Lite 4. Notebooks with embedded visualizations can be viewed on GitHub and nbviewer.

Vega 335 Nov 29, 2022
A curated list of awesome Dash (plotly) resources

Awesome Dash A curated list of awesome Dash (plotly) resources Dash is a productive Python framework for building web applications. Written on top of

Luke Singham 1.7k Dec 26, 2022
PanGraphViewer -- show panenome graph in an easy way

PanGraphViewer -- show panenome graph in an easy way Table of Contents Versions and dependences Desktop-based panGraphViewer Library installation for

16 Dec 17, 2022
Gesture controlled media player

Media Player Gesture Control Gesture controller for media player with MediaPipe, VLC and OpenCV. Contents About Setup About A tool for using gestures

Atharva Joshi 2 Dec 22, 2021
A minimal Python package that produces slice plots through h5m DAGMC geometry files

A minimal Python package that produces slice plots through h5m DAGMC geometry files Installation pip install dagmc_geometry_slice_plotter Python API U

Fusion Energy 4 Dec 02, 2022
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