npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@nulven/nodeplotlib

v1.0.3

Published

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

Downloads

7

Readme

NodePlotLib

NodeJS CI Coverage Status npm npm code style: prettier Gitter chat

Animation (View on Github)

Library to create plots directly in TypeScript or JavaScript in NodeJS on top of plotly.js without any front-end preparations. Inspired by matplotlib.

Installation

npm install nodeplotlib
# or
yarn add nodeplotlib

Usage

Creating a simple plot

Use with TypeScript/JavaScript:

import { plot, Plot } from 'nodeplotlib';

const data: Plot[] = [
  {
    x: [1, 3, 4, 5],
    y: [3, 12, 1, 4],
    type: 'scatter',
  },
];

plot(data);

Creating a stream that plots data in realtime

NodePlotLib makes use of the popular RxJS library, which provides functionality for streams, stream creator functions (e.g. from interval or from event), and tons of operators to modify your stream.

In this example we create a stream based on an interval which triggers every 100ms. Then we modify the output of the interval (which is just a counter) to be an actual Plot using RxJS' map operator. The output will be a sin function.

import { plot, Plot } from 'nodeplotlib';
import { interval, map } from 'rxjs';

const stream$: Observable<Plot[]> = interval(100).pipe(
  map(createSinusPlotFromNumber)
);

function createSinusPlotFromNumber(num: number): Plot[] {
  const data: Plot[] = [
    {
      x: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
      y: Array(10)
        .fill(0)
        .map((_, i) => Math.sin(num + i)),
      type: 'scatter',
    },
  ];
  return data;
}

As you can see, providing a function for a dynamic plot seems to be a good idea. The functions content looks almost the same as the "non-stream" version. Simple as that, you can just put the created Observable as an argument in the plot function:

plot(stream$);

API overview

There are three exports. The plot function and types for the Plot and for the Layout.

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

The plot function has the following structure

function plot(data: Plot[] | Observable<Plot[]>, layout?: Layout): void;

It does not return a Subscription for the Observables because you just need to close the listening browser window to unsubscribe from all Obserables.

Examples

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 | |

Contributing

Contributions in all forms are welcome.

Developers guide

You can find the developers guide in the repositories root README.md.

Contributors