
Image by Editor | ChatGPT
While Lux excels at creating insightful visualizations with zero effort, there may be times when you want to customize these visualizations to make them more presentation-ready or align with your specific aesthetic preferences.
This article will show you how to discover interesting visualizations with Lux and then customize them using Matplotlib.
Discovering Interesting Visualizations with Lux
Let’s start with our familiar Iris dataset and express a simple intent to explore the relationship between petal dimensions:
import pandas as pd
import lux
import seaborn as sns
# Load the Iris dataset
df = sns.load_dataset('iris')
# Express intent to see relationships between petal_length and petal_width
df.intent = ["petal_length", "petal_width"]
# Display the dataframe
df
When you toggle to the Lux view, you’ll see a scatterplot showing the relationship between petal length and petal width. What’s particularly valuable is that Lux automatically generates enhanced views in the right panel:

Image By Author
Notice how in the “Enhance” tab, Lux has automatically colored the points by species, revealing clear clustering patterns without you having to specify this. This is the power of Lux’s recommendation system – it discovers interesting patterns in your data that you might not have thought to look for.
Customizing Specific Visualizations with Matplotlib
While Lux’s automated visualizations are informative, you might want to refine a specific visualization for reporting or presentation. Instead of customizing all Lux visualizations, let’s focus on exporting and customizing just the species-colored scatterplot that shows the clear separations between iris types.
Once you find a visualization you like in the Lux interface, click on it and select “Export” to add it to your exported list.

Image By Author
Then you can customize it with familiar Matplotlib code:
# Access the exported visualization
selected_vis = df.exported[0] # First exported visualization
# Get the Matplotlib code
matplotlib_code = selected_vis.to_matplotlib()
print("Lux generated the following code:")
print(matplotlib_code[:200] + "...") # Show first 200 characters for brevity
# Let's modify the exported code to customize it
import matplotlib.pyplot as plt
from matplotlib.cm import ScalarMappable
from matplotlib.colors import ListedColormap
# Set global matplotlib style parameters
plt.rcParams.update({
"axes.titlesize": 20,
"axes.titleweight": "bold",
"axes.labelweight": "bold",
"axes.labelsize": 16,
"legend.fontsize": 14,
"legend.title_fontsize": 15,
"xtick.labelsize": 13,
"ytick.labelsize": 13,
})
# Create our figure and plot
fig, ax = plt.subplots(figsize=(7, 5))
# Plot the data points
x_pts = df['petal_length']
y_pts = df['petal_width']
# Create custom color mapping for species
# Map species to numeric values for coloring
color_map = {'setosa': 0, 'versicolor': 1, 'virginica': 2}
colors = [color_map[s] for s in df['species']]
# Use a custom colormap
species_colors = ['#2ca02c', '#1f77b4', '#ff7f0e'] # green, blue, orange
custom_cmap = ListedColormap(species_colors)
# Create scatter plot with custom colors
scatter = ax.scatter(x_pts, y_pts, c=colors, cmap=custom_cmap, s=80)
# Add a better legend
ax.legend(
handles=scatter.legend_elements()[0],
labels=list(color_map.keys()),
title='Species',
markerscale=1.5,
loc='upper left',
frameon=True,
fontsize=13
)
# Style the scatter points
scatter.set_alpha(0.8)
for collection in ax.collections:
collection.set_edgecolor('white')
collection.set_linewidth(0.5)
# Customize axes
ax.set_title("Iris Species by Petal Dimensions", loc='left')
ax.set_xlabel('Petal Length')
ax.set_ylabel('Petal Width')
ax.grid(color='#EEEEEE', linestyle='-', linewidth=0.5)
# Adjust layout
fig.tight_layout()
# Display the customized visualization
fig
The resulting visualization maintains the insights discovered by Lux but with enhanced styling that makes it more suitable for presentations or reports:

Image By Author
This approach gives you the best of both worlds:
- Lux’s automated discovery capabilities identify interesting patterns
- Matplotlib’s flexible styling options let you refine the presentation
Why This Approach Works Well
Using Lux to discover interesting visualizations and then customizing specific ones with Matplotlib offers several advantages:
- Efficiency: You don’t waste time creating visualizations that don’t reveal interesting patterns
- Focus: You only invest effort in customizing the most insightful visualizations
- Familiarity: You can use your existing Matplotlib knowledge rather than learning a new styling system
- Control: You have complete control over every aspect of the visualization’s appearance
Working with the Generated Code
One important insight from this exercise is understanding how Lux’s export functionality works. When you export a visualization, Lux gives you the complete code needed to recreate that visualization in Matplotlib. Rather than ignoring this code, we’ve built upon it for our customization.
The exported code includes:
- Data preprocessing steps
- The correct figure and axis setup
- The necessary scatter plot configuration with proper coloring
- Legend setup for the species categories
By building on this foundation rather than starting from scratch, we ensure we’re preserving the insights that Lux discovered while adding our own styling preferences.
Conclusion
Lux and Matplotlib complement each other perfectly in a data analysis workflow. Lux quickly discovers interesting patterns and relationships, while Matplotlib provides the tools to refine and polish those visualizations for sharing with others.
While we’ve focused on Matplotlib in this post, similar approaches work with other visualization libraries like Altair, which Lux uses by default to generate Vega-Lite visualizations. Altair’s declarative syntax and seamless integration with Lux make it an excellent choice for creating interactive, web-ready charts. The key takeaway is that Lux isn’t meant to replace your favorite visualization libraries—it’s meant to help you discover which visualizations are worth creating in the first place.
Check out our Altair visualization series and the official documentation on styling custom plot settings to learn more about this alternative.
