GithubHelp home page GithubHelp logo

axcient / nodeplotlib Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cat-ninja/nodeplotlib

0.0 1.0 0.0 1.91 MB

NodeJS frontend-less plotting lib using plotly.js inspired by matplotlib

License: MIT License

HTML 2.32% JavaScript 20.22% TypeScript 77.46%

nodeplotlib's Introduction

NodePlotLib

Build Status Coverage Status npm version npm code style: prettier Gitter chat

Library to create top-notch plots directly within NodeJS on top of plotly.js without any front-end preparations. Inspired by matplotlib.

Animation (View on Github)

Installation

npm install nodeplotlib
# or
yarn add nodeplotlib

Usage

Overview

Use with TypeScript/JavaScript:

import { plot, Plot } from 'nodeplotlib';
const data: Plot[] = [{x: [1, 3, 4, 5], y: [3, 12, 1, 4], type: 'line'}];
plot(data);

If ES5 use require() instead of import. Here is a short animation about howto and the results.

Detailed usage

Since Python provides with matplotlib a library for spawning plot windows, NodeJS isn't by default. But there are awesome plotting libraries for usage in front-end. So this lib targets people like scientists who easily want to create beautiful plots in a time-saving way.

The library provides a simple interface with (for now) just three functions. A plot, stack and a clear function. The plot() functions spawns a plot to the browser, if a plotdata is given as an argument. Otherwise it plots all the stack()ed plotdata. The arguments are of type Plot, which is a partial of plotly's PlotData type. With the clear() function the stack container can be cleared.

With the stack function the user is able to print multiple charts on one page.

import { plot, stack, clear, Plot } from 'nodeplotlib';

const data: Plot[] = [{
  x: [ 1, 3, 4, 6, 7],
  y: [ 2, 4, 6, 8, 9],
  type: 'scatter'
}];

stack(data);
stack(data);
stack(data);
plot();

The plot function plots all stacked plots and the plot given by parameter (if there is one). Afterwards the temporary container gets cleared and you can call stack() and plot() again without any predefined plots.

The functions are of the form:

import { plot, stack, clear, Plot, Layout } from 'nodeplotlib';

plot(data?: Plot[], layout?: Layout): void;
stack(data: Plot[], layout?: Layout): void;
clear(): void;

Quick start

In this section there are some examples to getting started. See the full plotly cheatsheet.

Line Plots

const trace1: Plot = {x: [1, 2], y: [1, 2], type: 'scatter'};
const trace2: Plot = {x: [3, 4], y: [9, 16], type: 'scatter'};
plot([trace1, trace2]);

Bar Charts

const trace: Plot = {x: [1, 2], y: [1, 2], type: 'bar'};
plot([trace]);

3D Line Plots

const trace: Plot = {x: [9, 8, 5, 1], y: [1, 2, 4, 8], z: [11, 8, 15, 3], type: 'scatter3d'};
plot([trace]);

3D Surface Plots

const trace: Plot = {colorscale: 'Viridis', z: [[3, 5, 7, 9], [21, 13, 8, 5]]};
plot([trace]);

Radial Plots

In order to style the plot, one is able to pass in the layout parameter, which internally is typeof Partial<Layout> from plotly's Layout. See the full layout documentation here.

With this parameter one is able to define styles like title, axis labels, subplots and many more.

const data: Plot[] = [{
  type: 'scatterpolar',
  r: [1.5, 10, 39, 31, 15, 1.5],
  theta: ['A','B','C', 'D', 'E', 'A'],
  fill: 'toself',
  name: 'Group B'
}];

const layout: Layout = [
  polar: {
    radialaxis: {
      visible: true,
      range: [0, 50]
    }
  }
];

plot(data, layout);

Plot types

Simple charts Advanced charts 3D Plots
Scatter 2d density plots Scatter
Line Histograms Surface
Bar Box-plots Lines
Pie charts Contour plots
Sankey diagrams Heatmaps
Tables Radar charts

Behind the scenes

The lib launches a webserver and opens new tabs for every plot located at http://localhost:8080/plots/:id. At this address a temporary html template file, the nodeplotlib script and plotly.min.js are available. The client side js requests the plot data at http://localhost:8080/data/:id. After all pending plots are opened in a unique tab and all the data is requested, the server shuts down. If you fire another plot the server starts again provides your plot and shuts down automatically.

Another port can be provided via PORT environment variable.

Contributing

Contributions in all forms are welcome.

Developers guide

Fork the Github repository and clone it to your PC. Install the npm dependencies using the install command. It installs the dependencies and copies plotly types to project source. These won't affect the git tree.

Roadmap

It would be nice to make the library compatible with Observable-streams and update the plots in real-time.

Get in touch

twitter github

Hi, I am Felix,
Senior Angular developer and Angular, RxJS and NgRX contributor.

avatar

If you like this library, think about giving it a star or follow me on twitter or github or check out my personal the website.

nodeplotlib's People

Contributors

ngfelixl avatar dependabot[bot] avatar medved-nsk avatar cat-ninja avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.