PyXLL The Python Excel Add-In
  • Product
    • Features
    • Get Started
    • Request Demo
    • Download
  • Pricing
  • Resources
    • Documentation
    • Blog
    • Videos
    • FAQ
    • Learn Python
    • Customer Portal
    • About Us
  • Support
    • Documentation
    • Videos
    • FAQ
    • Contact Us
  • Contact Us
Table of Contents
  • PyXLL Documentation
  • Introduction to PyXLL
  • User Guide
    • Installing PyXLL
    • Configuring PyXLL
    • Worksheet Functions
    • Macro Functions
    • Real Time Data
    • Cell Formatting
    • Charts and Plotting
      • Matplotlib
      • Pandas
      • Plotly
      • Seaborn
      • Bokeh
      • Altair
      • Others
      • Plotting from Worksheet Functions
      • Plotting from Menus, Macros and Elsewhere
      • Moving and Resizing
    • Custom Task Panes
    • ActiveX Controls
    • Using Pandas in Excel
    • Customizing the Ribbon
    • Context Menu Functions
    • Working with Tables
    • Python as a VBA Replacement
    • Menu Functions
    • Reloading and Rebinding
    • Error Handling
    • Deploying your add-in
    • Workbook Metadata
  • Video Guides and Tutorials
  • API Reference
  • What’s new in PyXLL 5
  • Changelog
Close

Altair¶

To plot a altair figure in Excel you first create the figure in exactly the same way you would in any Python script using altair, and then use PyXLL’s plot function to show it in the Excel workbook.

Altair supports interactive charts and these are displayed in Excel using an interactive web control, where available [1].

The code below shows an Excel worksheet function that generates an altair figure and displays it in Excel.

# This example requies vega_datasets.
# Install using 'pip install vega_datasets'
from vega_datasets import data
from pyxll import xl_func, plot
import altair as alt

@xl_func
def altair_plot():
    # Get the sample data set
    source = data.cars()

    # Create the chart
    chart = alt.Chart(source).mark_circle(size=60).encode(
        x='Horsepower',
        y='Miles_per_Gallon',
        color='Origin'
    )

    # Enable some basic interactive features
    chart = chart.interactive()

    # Show it in Excel using pyxll.plot
    plot(chart)

When this function is run in Excel the plot is shown just below the calling cell.

Altair chart in Excel

If the interactive web control is not available, the figure will instead be exported as a static image. This is done using altair_saver which also requires Selenium. Both of these must be installed before Altair can be used with PyXLL unless using the web control [1].

  • altair_saver can be installed using pip install altair_saver or conda install -c conda-forge altair_saver.

  • The easiest way to install Selenium is to use Anaconda and install it using either of the following commands:

    conda install selenium geckodriver firefox -c conda-forge
    

    or

    conda install selenium python-chromedriver-binary -c conda-forge
    

    If you are not using Anaconda you can use pip install selenium but you will also need to install a suitable web browser backend. See https://pypi.org/project/selenium/ for additional details about how to install Selenium.

Tip

Whenever you change an input argument to your plotting function, the chart will be redrawn.

You can use this to create interactive dashboards where the Excel user can control the inputs to the plot and see it redraw automatically.

If you have any problems with exporting plots as html or using the interactive web control, you can tell PyXLL to use a static image format instead by passing allow_html=False to plot.

When exporting as an image and not html, an SVG image may be used. If you version of Excel does not support SVG images (Excel 2016 or earlier) or you are having problems with the SVG image not displaying correctly you can tell PyXLL to use the PNG format instead by passing allow_svg=False to plot.

Warning

If you are not using the interactive web control and the figure is being exported as an image, altair launches a Selenium subprocess to do the export.

The first time you export an image from altair it can take a few seconds.

If you have anti-virus software installed it may warn you about this subprocess being launched.

Footnotes

[1] (1,2)

The interactive web control for displaying html based charts is new in PyXLL 5.9.0. In earlier versions the chart will be displayed as a static image that is not interactive.

The web control for displaying html based charts requires the Microsoft WebView2 component to be installed. This is usually installed as part of the Microsoft Edge web browser.

« Bokeh
Other Plotting Packages »
  • Home
  • Product
  • Features
  • Documentation
  • Download
  • Pricing
  • Support
  • Documentation
  • Videos
  • FAQ
  • Learn Python
  • Contact Us
  • About
  • About Us
  • Legal
  • Blog
© Copyright PyXLL Ltd