Displaying a Matplotlib Plot

Matplotlib is one of the more popular plotting libraries in the python ecosystem, and zero-true has built in integrations for working with matplotlib plots. There is some slight nuance to how to work with matplotlib in zero-true though so take a look at the example below:

import matplotlib.pyplot as plt 
import zero_true as zt

# Create a figure and an axis 
fig, ax = plt.subplots() 

# Plot some data 
ax.plot([0, 1, 2, 3], [10, 20, 25, 30]) 

# Add labels and a title 
ax.set_xlabel('X Label') 
ax.set_ylabel('Y Label') 
ax.set_title('Basic Line Plot') 

# Render the plot in zero-true 
zt.matplotlib(id='plt',figure=fig,height=300,width=300)

Run the cell in your notebook, and watch the line plot appear below! Matplotlib components require an ID as well as a matplotlib figure passed as arguments to render. Since matplotlib plots are static components in zero-true there are no values for you to access that users interact with.

Interactive Matplotlib Plots in Zero-True

Let’s take the code above and make it a little bit more exciting by interactively setting the last Y value in the plot:

import matplotlib.pyplot as plt 
import zero_true as zt

#slider for plot
slider = zt.Slider(id='slider',min=0,max=50)

# Create a figure and an axis 
fig, ax = plt.subplots() 

# Plot some data 
ax.plot([0, 1, 2, 3], [10, 20, 25, slider.value]) 

# Add labels and a title 
ax.set_xlabel('X Label') 
ax.set_ylabel('Y Label') 
ax.set_title('Basic Line Plot') 

# Get the plot and show it in zero-true
zt.matplotlib(id='plt',figure=fig,height=300,width=300)

Combining matplotlib’s awesome capabilities with zero-true’s interactive notebook and UI can help take your visualization game to the next level.