2025-03-11 12:33:00
medium.com
API design matters
The ecosystem for scientific visualization has come a long way since the early 2000s.
These days, there are many open-source Python visualization tools available. However, one limiting factor for scientists and other users is the high barrier to entry in learning how to use some of these libraries. Often, users are forced to learn complicated APIs that make it difficult to focus on their data or research questions.
In fastplotlib
, we aim to provide fast interactive visualization via an easy-to-use and intuitive API.
1) Data interaction
The premise behind our API design is that you should never have to think of your data as anything but an array.
If the data in our visualization maintains an array-like structure we are familiar with, interacting with the visualization becomes much easier.
Consider the following example:
Suppose we want to plot a simple sine wave.
import fastplotlib as fpl
import numpy as np# generate some data
xs = np.linspace(-10, 10, 100)
ys = np.sin(xs)
data = np.dstack([xs, ys])[0]
# create a figure
figure = fpl.Figure()
# add the data to the figure
sine_wave = figure[0, 0].add_line(data=data, thickness=10)
figure.show()
Now, say we wanted to change every third point of our sine wave to have the color red. If the data in our visualization can be thought of as an array, then doing this kind of fancy indexing isn’t any different than usual NumPy array manipulation.
sine_wave.colors[::3] = "red"
With this framework, we can perform all sorts of dynamic manipulations to different features of our graphic (e.g., color maps, colors, data, etc.) after we have initially plotted something.
The goal of taking this approach to the API design is to limit the amount of mental overhead required to use fastplotlib
, ultimately making the library flexible, intuitive, and easy to use.
2) Events
Another area of our API that we have tried to simplify for users is our events system. Using simple callback functions, we can define events between graphics or plots, which allows for the generation and build-up of interactive visualizations.
For example, suppose we wanted to define a simple click event. Let’s first generate some data.
import fastplotlib as fpl
import numpy as np# generate some circles
def make_circle(center, radius: float, n_points: int = 75) -> np.ndarray:
theta = np.linspace(0, 2 * np.pi, n_points)
xs = radius * np.sin(theta)
ys = radius * np.cos(theta)
return np.column_stack([xs, ys]) + center
circles = list()
for x in range(0, 50, 10):
circles.append(make_circle(center=(x, 0), radius=4, n_points=100))
# create figure
fig = fpl.Figure()
# add circles to plot
circles_graphic = fig[0,0].add_line_collection(data=circles, cmap="tab10", thickness=10)
fig.show()
Now that we have generated our data, we can define a click event so that when we click on the plot, we get the closest circle and “highlight” it.
# get the nearest graphic that is clicked and change the color
@fig.renderer.add_event_handler("click")
def click_event(ev):
# reset colors
circles_graphic.cmap = "tab10"# map the click position to world coordinates
xy = fig[0, 0].map_screen_to_world(ev)[:-1]
# get the nearest graphic to the position
nearest = fpl.utils.get_nearest_graphics(xy, circles_graphic)[0]
# change the closest graphic color to white
nearest.colors = "w"
In fastplotlib
, specifying an event is as simple as defining how we want to handle the event and then adding our handler to the appropriate graphic or plot the event corresponds to. The beauty of this approach is that it doesn’t require users to learn new, complicated, library-specific API features (knowing how to define functions is sufficient).
The streamlined design of our API for data interaction and event handling is a key strength of the library, making fastplotlib
both intuitive and easy to master.
Keep your files stored safely and securely with the SanDisk 2TB Extreme Portable SSD. With over 69,505 ratings and an impressive 4.6 out of 5 stars, this product has been purchased over 8K+ times in the past month. At only $129.99, this Amazon’s Choice product is a must-have for secure file storage.
Help keep private content private with the included password protection featuring 256-bit AES hardware encryption. Order now for just $129.99 on Amazon!
Help Power Techcratic’s Future – Scan To Support
If Techcratic’s content and insights have helped you, consider giving back by supporting the platform with crypto. Every contribution makes a difference, whether it’s for high-quality content, server maintenance, or future updates. Techcratic is constantly evolving, and your support helps drive that progress.
As a solo operator who wears all the hats, creating content, managing the tech, and running the site, your support allows me to stay focused on delivering valuable resources. Your support keeps everything running smoothly and enables me to continue creating the content you love. I’m deeply grateful for your support, it truly means the world to me! Thank you!
BITCOIN bc1qlszw7elx2qahjwvaryh0tkgg8y68enw30gpvge Scan the QR code with your crypto wallet app |
DOGECOIN D64GwvvYQxFXYyan3oQCrmWfidf6T3JpBA Scan the QR code with your crypto wallet app |
ETHEREUM 0xe9BC980DF3d985730dA827996B43E4A62CCBAA7a Scan the QR code with your crypto wallet app |
Please read the Privacy and Security Disclaimer on how Techcratic handles your support.
Disclaimer: As an Amazon Associate, Techcratic may earn from qualifying purchases.