By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
Hyper-practical, zero-fluff guide for production-grade visuals
You’re a data scientist or analyst, and your boss just asked for a shareable, interactive dashboard—not a Jupyter notebook. Or maybe you’re prepping for a certification (PL-300, DP-100) and need to export visuals programmatically for reports, emails, or web apps.
Why this matters in production:- Static exports (PNG/PDF) are required for emails, PowerPoint decks, or printed reports.- Interactive HTML lets stakeholders explore data without Python (e.g., sales teams, executives).- Automation (e.g., cron jobs, Airflow) needs headless rendering—no GUI, no manual clicks.- Reproducibility breaks if you rely on Jupyter’s "Save As" button. You need code-controlled exports.
cron
Real-world scenario:You built a matplotlib/seaborn dashboard in a notebook. Now: - The marketing team wants a PNG for their weekly email.- The CFO wants an interactive HTML to drill into sales data.- Your CI/CD pipeline needs to auto-generate reports when new data arrives.
matplotlib
seaborn
If you don’t know how to export programmatically, you’re stuck manually screenshotting—or worse, rewriting everything in Tableau.
plt.style.use('seaborn')
plt.rcParams
sns.barplot()
plt.bar()
sns.set_theme(context='talk')
plotly
plotly.express
fig.write_html("dashboard.html", include_plotlyjs='cdn')
altair
chart.save('chart.html')
kaleido
pip install -U kaleido
fig.write_image()
selenium
playwright
weasyprint
pdfkit
nbconvert
--no-input
pyenv
pip install matplotlib seaborn plotly kaleido pandas nbconvert weasyprint
Goal: Generate a sales trend plot and save it as sales_trend.png.
sales_trend.png
import matplotlib.pyplot as plt import pandas as pd # Sample data data = pd.DataFrame({ 'month': ['Jan', 'Feb', 'Mar', 'Apr'], 'sales': [1200, 1500, 1800, 2200] }) # Plot plt.figure(figsize=(10, 6)) plt.plot(data['month'], data['sales'], marker='o', linewidth=3) plt.title("Monthly Sales Trend", fontsize=16, pad=20) plt.xlabel("Month", fontsize=12) plt.ylabel("Sales ($)", fontsize=12) plt.grid(True, linestyle='--', alpha=0.6) # Save as PNG (300 DPI for print quality) plt.savefig( "sales_trend.png", dpi=300, # High resolution bbox_inches='tight', # No cropped labels facecolor='white' # White background (not transparent) ) plt.close() # Free memory
Verify:
ls -lh sales_trend.png # Should be ~100KB file sales_trend.png # Should show "PNG image data"
Goal: Create a plotly scatter plot and save it as interactive_dashboard.html.
interactive_dashboard.html
import plotly.express as px import pandas as pd # Sample data df = pd.DataFrame({ 'product': ['A', 'B', 'C', 'D'], 'sales': [1200, 1500, 800, 2000], 'profit': [300, 450, 200, 600] }) # Create interactive plot fig = px.scatter( df, x='sales', y='profit', color='product', title="Sales vs. Profit (Interactive)", hover_data=['product'] ) # Save as HTML (use CDN to reduce file size) fig.write_html( "interactive_dashboard.html", include_plotlyjs='cdn', # Load JS from CDN (smaller file) full_html=False # Embed as a <div> (for web apps) )
ls -lh interactive_dashboard.html # Should be <50KB # Open in browser: python -m http.server 8000
Goal: Export a notebook to a clean HTML report (no code cells).
# Convert notebook to HTML (hide code) jupyter nbconvert --to html \ --no-input \ # Hide code cells --no-prompt \ # Hide prompts (In [1]:) --output report.html \ analysis.ipynb # Convert to PDF (requires LaTeX or weasyprint) jupyter nbconvert --to pdf --no-input analysis.ipynb # OR (if LaTeX fails) weasyprint report.html report.pdf
ls -lh report.html report.pdf
Goal: Generate a plotly PNG in a CI/CD pipeline (no GUI).
import plotly.express as px import pandas as pd df = pd.DataFrame({ 'x': [1, 2, 3, 4], 'y': [10, 11, 12, 13] }) fig = px.line(df, x='x', y='y', title="Automated PNG Export") fig.write_image( "ci_plot.png", scale=2, # 2x resolution width=800, # Fixed width height=500 # Fixed height )
Verify in CI:
# In GitHub Actions / GitLab CI: file ci_plot.png # Should show "PNG image data"
os.path.join()
pathlib.Path
filename = re.sub(r'[^\w\-_]', '_', title)
eval()
json.loads()
include_plotlyjs='cdn'
pip install pillow
Image.save('plot.png', optimize=True)
figsize
width
height
bbox_inches='tight'
report_20231015.html
try/except
report.html > 5MB
plt.show()
savefig()
plt.savefig()
rcParams
Typical question patterns:1. "Which library exports interactive HTML?" - ❌ matplotlib (static only) - ✅ plotly (interactive) - ❌ seaborn (static)
fig.write_image("plot.png")
❌ fig.show() (opens browser, not headless)
fig.show()
"What’s the best way to hide code in a Jupyter report?"
jupyter nbconvert --no-input
Trap distinctions:- matplotlib vs. plotly: - matplotlib: Static, lightweight, good for PDFs. - plotly: Interactive, heavier, good for web dashboards.
Task: Export a seaborn heatmap to PNG with a white background and 300 DPI.
Solution:
import seaborn as sns import matplotlib.pyplot as plt data = sns.load_dataset("flights").pivot("month", "year", "passengers") plt.figure(figsize=(10, 8)) sns.heatmap(data, annot=True, fmt="d", cmap="YlGnBu") plt.title("Passenger Traffic Heatmap") plt.savefig( "heatmap.png", dpi=300, bbox_inches='tight', facecolor='white' ) plt.close()
Why it works:- facecolor='white' ensures the background isn’t transparent.- dpi=300 guarantees print-quality resolution.
facecolor='white'
dpi=300
plt.savefig("plot.png", dpi=300, bbox_inches='tight')
dpi
bbox_inches
fig.write_html("plot.html", include_plotlyjs='cdn')
fig.write_image("plot.png", scale=2)
jupyter nbconvert --to html --no-input notebook.ipynb
weasyprint report.html report.pdf
dpi=100
include_plotlyjs=True
cdn
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.