Using Matplotlib in Preswald Data Apps

Amrutha GujjarAmrutha Gujjar4 min read

Category: Product


Preswald now supports Matplotlib as a first-class visualization backend. If you're coming from a Jupyter or scientific Python background, this opens the door to reuse existing plots — without rewriting them for a new framework.

Why This Matters

Matplotlib is the default plotting library for a huge number of researchers, scientists, and analysts. It’s powerful, flexible, and already baked into many internal scripts, notebooks, and academic tools.

Until now, using Matplotlib in modern data apps often meant exporting static images, re-implementing in a different library (like Plotly or Vega), or skipping interactivity altogether.

Preswald changes that. You can now embed Matplotlib charts directly inside your data apps — with no extra setup.

img


Basic Example

Here’s how to add a Matplotlib figure to your Preswald app:

from preswald import matplotlib
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([1, 2, 3], [4, 6, 5])
ax.set_title("Simple Line Chart")

matplotlib(fig)

That’s it. Preswald takes care of rendering the chart in your app, no export or image conversion required.

img


Use Cases

  • 🧪 Scientific Visualizations: Histograms, scatter plots, and custom subplots

  • 🏫 Education + Research: Bring notebook figures into shareable UIs

  • 🔬 Healthcare, Engineering: Use Matplotlib’s fine-grained control for precision plotting

  • 🧬 Legacy Codebases: Migrate incrementally — keep the same plots, add interactivity


What You Can Do

Matplotlib charts behave like any other component in a Preswald app:

  • They appear inline in the UI, inside your layout

  • You can combine them with other components (e.g., sliders, checkboxes)

  • They render deterministically every time your script runs

This is especially helpful when reusing code from existing notebooks or tutorials. You don’t need to rewrite the logic — your figures just show up.


Notes and Limitations

  • Matplotlib charts are currently rendered as static images (PNG), not interactive canvases

  • You can still combine them with other reactive Preswald components

  • Interactivity (zooming, clicking) is better suited to Plotly or Vega — but for presentation and precision, Matplotlib is hard to beat


Acknowledgements

This feature was contributed by Anirudh Kohli, who also provided examples and feedback during implementation. Thank you!

View on GitHub