Lets-Plot
Lets-Plot is an open-source, multiplatform plotting library for statistical data based on the Grammar of Graphics, heavily influenced by R's ggplot2. It provides a Python API for creating both static and interactive charts in notebooks and IDEs, supporting various data formats like Pandas DataFrames and NumPy. The current version is 4.9.0, with a regular release cadence often tied to its Kotlin counterpart.
Common errors
-
ImportError: DLL load failed while importing lets_plot_kotlin_bridge: The specified module could not be found.
cause This error typically occurs on Windows systems, especially when using Anaconda or Miniconda, and indicates missing native backend components required by Lets-Plot's Kotlin bridge.fixInstall the `m2w64-toolchain` using `conda install m2w64-toolchain` before installing `lets-plot` with pip, or ensure your environment has the necessary C++ build tools. -
Plots are not displayed in my Jupyter Notebook cell output.
cause The HTML rendering functionality for notebooks needs to be explicitly initialized once per session.fixAdd `from lets_plot import LetsPlot; LetsPlot.setup_html()` to the top of your notebook. -
Error: geom_contour : error if data is not sorted properly
cause The `geom_contour` geometry expects input data to be sorted correctly for proper contour calculation.fixEnsure your DataFrame is sorted by the x- and y-variables used in `geom_contour` before plotting. For example, `df.sort_values(by=['x_var', 'y_var'], inplace=True)`.
Warnings
- breaking The Geocoding API underwent a complete overhaul in Lets-Plot v2.0.0, rendering previous API implementations incompatible.
- breaking Version 4.0.0 introduced significant internal package refactoring. While the Python API was largely unaffected, default qualitative color palettes (now 'Set1' from 'Set2') and default sizes for points and lines changed. It also caused partial incompatibility with older Kotlin API versions.
- breaking In version 4.9.0 (primarily affecting Kotlin/JVM), JavaFX artifacts were removed, and the `lets-plot-image-export` module was deprecated. The `DefaultPlotPanelBatik` was replaced by `SwingPlotPanel` for JVM Swing applications.
- gotcha Forgetting to call `LetsPlot.setup_html()` in Jupyter, JupyterLab, Google Colab, or other notebook environments will result in plots not being rendered or displayed.
Install
-
pip install lets-plot -
conda install m2w64-toolchain && pip install lets-plot
Imports
- All functions
from lets_plot import *
- LetsPlot.setup_html()
from lets_plot import * # without calling setup_html()
from lets_plot import LetsPlot LetsPlot.setup_html()
Quickstart
import numpy as np
import pandas as pd
from lets_plot import *
LetsPlot.setup_html()
data = pd.DataFrame({
'x': np.random.normal(0, 1, 100),
'y': np.random.normal(1, 1.5, 100),
'category': np.random.choice(['A', 'B'], 100)
})
plot = (ggplot(data, aes(x='x', y='y', color='category')) +
geom_point(size=5, alpha=0.6) +
geom_smooth(method='lm', color='black', linetype='dashed') +
ggtitle('Scatter Plot with Regression Line')
)
plot