Giter VIP home page Giter VIP logo

uplot's Introduction

uplot

python license jupyter

Unified API and style for Python plotting libraries.

Usage

plotly 5 matplotlib
import numpy as np
import uplot

x = np.linspace(0, np.pi*4, num=100)
phi = np.pi/4

fig = uplot.figure('plotly')
fig.plot(x, np.sin(x - 0*phi), name='#1')
fig.plot(x, np.sin(x - 1*phi), name='#2')
fig.plot(x, np.sin(x - 2*phi), name='#3')
fig.plot(x, np.sin(x - 3*phi), name='#4')
fig.xlabel('X').ylabel('Y')
fig.legend().show()
import numpy as np
import uplot

x = np.linspace(0, np.pi*4, num=100)
phi = np.pi/4

fig = uplot.figure('matplotlib')
fig.plot(x, np.sin(x - 0*phi), name='#1')
fig.plot(x, np.sin(x - 1*phi), name='#2')
fig.plot(x, np.sin(x - 2*phi), name='#3')
fig.plot(x, np.sin(x - 3*phi), name='#4')
fig.xlabel('X').ylabel('Y')
fig.legend().show()

๐Ÿ’ก See gallery for more examples.

Install

Recent stable version (without any plotting library):

pip install "uplot @ git+https://github.com/makarovdi/uplot.git@main"

To automatically install all optional dependencies (matplotlib, plotly, ...):

pip install "uplot[all] @ git+https://github.com/makarovdi/uplot.git@main"

If you need only matplotlib support:

pip install "uplot[matplotlib] @ git+https://github.com/makarovdi/uplot.git@main"

๐Ÿ’ก Replace [matplotlib] with [plotly5] for plotly-only installation

Plotting Libs - Pros & Cons

๐ŸŸข Highly configurable.
๐ŸŸข Good documentation and a lot of ready-to-use recipes (e.g. on StackOverflow).
๐ŸŸก Common API (MATLAB legacy).

๐Ÿ”ด Limited interactivity (especially for Jupyter).
๐Ÿ”ด API, behavior and parameter names are inconsistent (e.g. plt.xlim and axis.set_xlim).
๐Ÿ”ด Slow and limited 3D rendering.

๐ŸŸข Very good interactivity.
๐ŸŸข Native compatibility with Jupyter.
๐ŸŸข Possibility to save interactive plot (html-file).
๐ŸŸข Fast and interactive 3D plot.

๐Ÿ”ด Not well documented (a lot of parameters, small amount of examples).
๐Ÿ”ด High memory consumption (limited number of plots in Jupyter).
๐Ÿ”ด Some expected API functions are missing (e.g. imshow).
๐Ÿ”ด 3D and 2D axis parameters are not unified (e.g. layout.xaxis doesn't work for 3D).

Extending

Plugin

The plugin system allows extending uplot for visualizing custom objects.
For example, the DataFrame plugin enables this code:

import uplot
import pandas as pd

car_crashes = pd.read_csv(
    'https://raw.githubusercontent.com/mwaskom/seaborn-data/master/car_crashes.csv'
)

fig = uplot.figure()
fig.plot(car_crashes[['total', 'speeding', 'alcohol', 'no_previous']])
fig.show()

To implement the plugin, you can follow this structure:

import numpy as np
import pandas as pd

import uplot.plugin as plugin


class DataFramePlugin(plugin.IPlotPlugin):

    def extract_data(self, obj: pd.DataFrame) -> list[plugin.PlotData]:
        data = []
        for name in obj.columns:
            if not np.issubdtype(obj.dtypes[name], np.number): continue
            y = obj[name].values
            x = np.arange(len(y))
            data.append(plugin.PlotData(x=x, y=y, name=name.replace('_', ' ').title()))
        return data

plugin.register(pd.DataFrame, handler=DataFramePlugin())

๐Ÿ’ก Check test/plugin.py for a more advanced plugin example.

Engine

Adding a new plotting library is straightforward. Implement two interfaces IPlotEngine and IFigure:

import uplot
from uplot import IPlotEngine, IFigure

class MyEngine(IPlotEngine):
    ...
    def figure(self, ...) -> MyFigure: ...
    
class MyFigure(IFigure):
    def plot(self, ...): ...
    def scatter(self, ...): ...
    ...

# register the engine
uplot.engine.register(MyEngine(), name='test') 

Then use it in the regular way:

import uplot

fig = uplot.figure(engine='test')
fig.plot(...)
fig.show()

Verified Versions

Standalone JupyterLab
v4.0.6-v4.2.3
Jupyter
Notebook
7.0-7.2
matplotlib
3.7-3.9
gui ๐ŸŸข
save image ๐ŸŸข
inline ๐ŸŸข
ipympl ๐ŸŸข
inline ๐ŸŸข
ipympl ๐ŸŸข
plotly
5.16-5.22
chromium ๐ŸŸข
save image ๐ŸŸข
๐ŸŸข ๐ŸŸข

Dependencies

  • Python โ‰ฅ 3.10
  • numpy โ‰ฅ 1.21 v2.0 supported
  • pillow โ‰ฅ 10.2

Optional

  • matplotlib โ‰ฅ 3.7
  • plotly โ‰ฅ 5.17

License

This software is licensed under the BSD-3-Clause license.
See the LICENSE file for details.

TODO

Check the plan for new features here.

uplot's People

Contributors

makarovdi avatar

Stargazers

 avatar

Watchers

 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.