ipyvizzu Documentation

repository·main·Indexed 21 days ago

https://github.com/vizzuhq/ipyvizzu

An animated charting tool for Jupyter and other notebook environments built on the Vizzu JavaScript/C++ engine. It allows data scientists to create animated data stories using Python syntax via the Chart.animate() method, Config objects for data mapping (channels, geometry, coordinate systems), and Style objects for visual themes. Compatible with JupyterLab, Google Colab, Streamlit, and various other notebook and app platforms.

Tokens
56.4K
Snippets
200
Records
235
Agent score
75%

What's inside ipyvizzu

  1. Supported environments for ipyvizzu

    main

    ipyvizzu is compatible with a wide range of notebook and application platforms:

    • Notebooks: Jupyter Notebook, JupyterLab, JupyterLite, Google Colab, Databricks, Kaggle, Deepnote, DataCamp, Noteable.
    • App Platforms: Streamlit, Flask, Panel, Mercury, Voilà.
    • BI Tools: Mode.
    • IDEs: PyCharm, VSCode Python.
  2. Transition between data states using regroupStrategy

    main

    When changing multiple dimensions simultaneously (adding and removing dimensions), you can control how markers transition between the current state and the target state using the regroupStrategy parameter in chart.animate().

    Available strategies:

    • Default (Implicit): Markers are aggregated to the common base of the two states, then drilled down to the target state.
    • drilldown: The chart drills down to the union of the two states, then aggregates to the target state.
    • fade: The chart fades the markers between the two states.
    # Example using 'drilldown' strategy
    chart.animate(
        Config(
            {
                "channels": {
                    "x": {
                        "detach": ["Genres"],
                        "attach": ["Kinds"],
                    },
                }
            }
        ),
        regroupStrategy="drilldown",
    )
    
    # Example using 'fade' strategy
    chart.animate(
        Config(
            {
                "channels": {
                    "x": {
                        "detach": ["Kinds"],
                        "attach": ["Genres"],
                    },
                }
            }
        ),
        regroupStrategy="fade",
    )
  3. Use ipyvizzu-story for presentations

    main
    If you want to present animated charts live from your notebook or share them as an interactive HTML file, use the ipyvizzu-story extension. Note that ipyvizzu-story uses a different syntax than the core ipyvizzu library.
  4. Configure Chart display targets in DataCamp

    main

    When initializing a Chart, you can specify how and when animations are displayed using the display parameter with DisplayTarget values.

    Note: For DisplayTarget.MANUAL, you must switch from the default DataCamp editor to the JupyterLab-style editor for ipyvizzu to work.

    Supported DisplayTarget options in DataCamp:

    • DisplayTarget.BEGIN: Automatically display all animations after the first cell.
    • DisplayTarget.ACTUAL: Automatically display all animations after the currently running cell.
    • DisplayTarget.END: Automatically display all animations after the last running cell.
    • DisplayTarget.MANUAL: Display animations only after calling the .show() method or when the _repr_html_ method is triggered (requires JupyterLab-style editor).

    When using any target other than DisplayTarget.MANUAL, you can rerun any cell without needing to rerun the first cell.

    from ipyvizzu import Chart, DisplayTarget
    
    # Example initialization with a specific display target
    chart = Chart(width="640px", height="360px", display=DisplayTarget.BEGIN)
  5. Use the noop channel for grouping without affecting element parameters

    main

    The noop (no operations) channel allows you to include a dimension in your chart without affecting any visual parameters (like size, color, or position) of the elements. It only affects the count of the elements. This is useful for grouping or stacking elements in charts that do not use axes (like treemaps or bubble charts) when you want to maintain a single group instead of splitting elements into multiple sub-groups based on a dimension.

    chart.animate(
        Config(
            {
                "channels": {
                    "size": {"detach": "Genres"},
                    "noop": {"set": "Genres"},
                }
            }
        )
    )
  6. Understand the basic logic of ipyvizzu

    main

    The core concept of ipyvizzu is animation-driven visualization. Instead of drawing static charts, you define states. A single state represents a static chart configuration (data, coordinate system, labels, etc.).

    To create movement, you use the animate method to transition from the current state to a new state. ipyvizzu automatically calculates the transitions between these states.

    When calling animate, you provide three types of non-keyword arguments to define the new state:

    • data: The data to be visualized.
    • config: Settings for series (adding/removing), chart titles, geometry, and alignment.
    • style: Visual styling of the chart elements.
  7. Save and reuse chart states and animations with store()

    main

    The store functionality allows you to capture the current state of a chart or a specific animation to a variable, which can then be reused later without re-defining the entire configuration.

    Storing a Chart State (Snapshot)

    To save the current visual state of a chart (e.g., its current alignment or configuration), call chart.store(). You can return to this state later by passing the stored object to chart.animate().

    Storing an Animation

    To save a specific animation sequence, call chart.control.store(). This captures the animation itself. You can replay this animation later by passing the stored object to chart.animate().

    Reusing Stored Objects

    Once stored, pass the variable directly into chart.animate() to apply the snapshot or replay the animation.

    # 1. Save a chart state (snapshot)
    snapshot = chart.store()
    
    # 2. Save an animation
    chart.animate(Config({"align": "stretch"}))
    animation = chart.control.store()
    
    # 3. Reuse them
    chart.animate(animation)
    chart.animate(snapshot)
  8. Styling charts with Style objects or CSS

    main

    You can style ipyvizzu charts using two methods:

    1. The Style property: Pass a dictionary-like object to chart.animate() to apply styles directly.
    2. CSS: Use CSS to set styles, which is useful for applying consistent styling across multiple charts.

    Note: CSS parameter usage is disabled by default. To enable it, use: chart.feature("cssProperties", True).

    When using CSS, color palettes are set via --vizzu-plot-marker-colorPalette and font sizes via --vizzu-title-fontSize or --vizzu-fontSize.

  9. Supported ipyvizzu features in JupyterLite

    main

    The following ipyvizzu features are supported in JupyterLite:

    Chart Configuration

    • Change the vizzu URL (vizzu)
    • Change the Chart width (width)
    • Change the Chart height (height)
    • Use scroll into view (scroll_into_view=True)

    Display Targets

    JupyterLite supports various DisplayTarget options to control when animations are displayed:

    • DisplayTarget.MANUAL: Display animations only after the _repr_html_ method or .show() method is called.
    • DisplayTarget.BEGIN: Automatically display all animations after the first cell.
    • DisplayTarget.ACTUAL: Automatically display all animations after the currently running cell.
    • DisplayTarget.END: Automatically display all animations after the last running cell.

    Note: When using any target other than DisplayTarget.MANUAL, you can rerun cells without needing to rerun the first cell.

  10. Understand the ipyvizzu chart layout structure

    main

    An ipyvizzu chart is composed of three main layout parts:

    1. Plot area: The central region containing the actual chart.
    2. Title: Located at the top of the chart.
    3. Legend: Located on the left side of the chart.

    ipyvizzu manages visibility automatically: the legend is hidden when unnecessary, and the title area is hidden when no title is in use.

    Each part has its own padding on all four sides. By default, these paddings adjust automatically based on the chart size, but they can be manually configured using the Style object. Size parameters (like width or padding) can be specified using pixels, percentages, or em units.

  11. Configure Chart display behavior with DisplayTarget

    main

    The Chart object accepts a display parameter which determines when animations are triggered and how the chart interacts with the notebook execution flow.

    Available DisplayTarget options:

    • DisplayTarget.MANUAL: Animations are displayed only after the _repr_html_ method is called (e.g., by simply typing the chart object at the end of a cell). This mode allows you to rerun any cell without rerunning the first cell.
    • DisplayTarget.BEGIN: Automatically display all animations after the first cell.
    • DisplayTarget.ACTUAL: Automatically display all animations after the currently running cell.
    • DisplayTarget.END: Automatically display all animations after the last running cell.

    Supported features in Mode include changing the vizzu URL, width, height, and enabling scroll_into_view=True.

    from ipyvizzu import Chart, DisplayTarget
    
    chart = Chart(width="640px", height="360px", display=DisplayTarget.MANUAL)
  12. Add data by series, records, or data cube

    main

    If you are not using DataFrames, you can manually construct data using several methods:

    1. Specify data by series

    Use add_series(name, values, type="dimension" | "measure") to add columns one by one.

    2. Specify data by records

    Use add_record(list) for a single row, or add_records(list_of_lists | list_of_dicts) for multiple rows. Records must be in first normal form.

    3. Using data cube form

    This is useful for dense data where you define dimensions and then a matrix of measures.

    • Use add_dimension(name, values) for each dimension.
    • Use add_measure(name, matrix) where the matrix is a list of lists representing the values for each combination of dimensions.

    4. Using JSON

    Load data directly from a JSON file using the class method Data.from_json(path).

    from ipyvizzu import Data
    
    # Data Cube Example
    data = Data()
    data.add_dimension("Genres", ["Pop", "Rock"])
    data.add_dimension("Kinds", ["Hard", "Smooth"])
    data.add_measure("Popularity", [[114, 96], [56, 36]])
    
    # JSON Example
    data = Data.from_json("path/to/data.json")