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
Geocoding library for Python.

geopy geopy is a Python client for several popular geocoding web services. geopy makes it easy for Python developers to locate the coordinates of addr

geopy 3.8k Jan 02, 2023
Visualizations for machine learning datasets

Introduction The facets project contains two visualizations for understanding and analyzing machine learning datasets: Facets Overview and Facets Dive

PAIR code 7.1k Jan 07, 2023
🌀❄ī¸đŸŒŠī¸ This repository contains some examples for creating 2d and 3d weather plots using matplotlib and cartopy libraries in python3.

Weather-Plotting 🌀 ❄ī¸ 🌩ī¸ This repository contains some examples for creating 2d and 3d weather plots using matplotlib and cartopy libraries in pytho

Giannis Dravilas 21 Dec 10, 2022
ICS-Visualizer is an interactive Industrial Control Systems (ICS) network graph that contains up-to-date ICS metadata

ICS-Visualizer is an interactive Industrial Control Systems (ICS) network graph that contains up-to-date ICS metadata (Name, company, port, user manua

QeeqBox 2 Dec 13, 2021
Lime: Explaining the predictions of any machine learning classifier

lime This project is about explaining what machine learning classifiers (or models) are doing. At the moment, we support explaining individual predict

Marco Tulio Correia Ribeiro 10.3k Dec 29, 2022
WebApp served by OAK PoE device to visualize various streams, metadata and AI results

DepthAI PoE WebApp | Bootstrap 4 & Vue.js SPA Dashboard Based on dashmin (https:

Luxonis 6 Apr 09, 2022
Bokeh Plotting Backend for Pandas and GeoPandas

Pandas-Bokeh provides a Bokeh plotting backend for Pandas, GeoPandas and Pyspark DataFrames, similar to the already existing Visualization feature of

Patrik Hlobil 822 Jan 07, 2023
:art: Diagram as Code for prototyping cloud system architectures

Diagrams Diagram as Code. Diagrams lets you draw the cloud system architecture in Python code. It was born for prototyping a new system architecture d

MinJae Kwon 27.5k Dec 30, 2022
FURY - A software library for scientific visualization in Python

Free Unified Rendering in Python A software library for scientific visualization in Python. General Information â€ĸ Key Features â€ĸ Installation â€ĸ How to

169 Dec 21, 2022
Small U-Net for vehicle detection

Small U-Net for vehicle detection Vivek Yadav, PhD Overview In this repository , we will go over using U-net for detecting vehicles in a video stream

Vivek Yadav 91 Nov 03, 2022
visualize_ML is a python package made to visualize some of the steps involved while dealing with a Machine Learning problem

visualize_ML visualize_ML is a python package made to visualize some of the steps involved while dealing with a Machine Learning problem. It is build

Ayush Singh 164 Dec 12, 2022
These data visualizations were created as homework for my CS40 class. I hope you enjoy!

Data Visualizations These data visualizations were created as homework for my CS40 class. I hope you enjoy! Nobel Laureates by their Country of Birth

9 Sep 02, 2022
Create matplotlib visualizations from the command-line

MatplotCLI Create matplotlib visualizations from the command-line MatplotCLI is a simple utility to quickly create plots from the command-line, levera

Daniel Moura 46 Dec 16, 2022
Draw interactive NetworkX graphs with Altair

nx_altair Draw NetworkX graphs with Altair nx_altair offers a similar draw API to NetworkX but returns Altair Charts instead. If you'd like to contrib

Zachary Sailer 206 Dec 12, 2022
Automatically visualize your pandas dataframe via a single print! 📊 💡

A Python API for Intelligent Visual Discovery Lux is a Python library that facilitate fast and easy data exploration by automating the visualization a

Lux 4.3k Dec 28, 2022
Python Data. Leaflet.js Maps.

folium Python Data, Leaflet.js Maps folium builds on the data wrangling strengths of the Python ecosystem and the mapping strengths of the Leaflet.js

6k Jan 02, 2023
An open-source tool for visual and modular block programing in python

PyFlow PyFlow is an open-source tool for modular visual programing in python ! Although for now the tool is in Beta and features are coming in bit by

1.1k Jan 06, 2023
Function Plotter: a simple application with GUI to plot mathematical functions

Function-Plotter Function Plotter is a simple application with GUI to plot mathe

Mohamed Nabawe 4 Jan 03, 2022
Investment and risk technologies maintained by Fortitudo Technologies.

Fortitudo Technologies Open Source This package allows you to freely explore open-source implementations of some of our fundamental technologies under

Fortitudo Technologies 11 Dec 14, 2022
Show Data: Show your dataset in web browser!

Show Data is to generate html tables for large scale image dataset, especially for the dataset in remote server. It provides some useful commond line tools and fully customizeble API reference to gen

Dechao Meng 83 Nov 26, 2022