Related topics
×
Sign in with Microsoft
Sign in or create an account.
Hello,
Select a different account.
You have multiple accounts
Choose the account you want to sign in with.

Python in Excel is currently in preview and is subject to change based on the feedback. To use this feature, join the Microsoft 365 Insider Program and choose the Beta Channel Insider level. 

Python in Excel is gradually rolling out to Excel for Windows customers using the Beta Channel. At this time, the feature is not available on other platforms.

If you encounter any issues with Python in Excel, please report them by selecting Help > Feedback in Excel.

New to Python in Excel? Start with Introduction to Python in Excel and Get started with Python in Excel.

Use open-source Python libraries to create plots and charts 

Python in Excel comes with a core set of Python libraries provided by Anaconda. This article describes how to use Python libraries, such as seabornand matplotlib, to create plots and charts. To learn more about the open-source libraries available with Python in Excel, see Open-source libraries and Python in Excel

The examples in this article use the Iris flower data set. Download this sample workbook to follow along with the article: python-in-excel-iris-dataset.xlsx

Create a pair plot with seaborn 

This example shows how to create a pair plot visualization of the Iris flower data set. A pair plot is a matrix of plots and charts that compares the relationship between each variable in a data set. In this case, the Iris flower data set contains four columns of numerical data: sepal_length, sepal_width, petal_length, and petal_width

Use Python in Excel to create a pair plot with the seaborn library.

Python in Excel creates the visualization with the seaborn library. The seabornlibrary is automatically imported for Python in Excel with the following import statement, letting you reference it with the alias sns.  

import seaborn as sns 

To create a pair plot of the Iris flower data set with the seaborn library, take the following steps:

  1. Enter the following code snippet in a Python in Excel cell. The code snippet stores the pair plot as a variable called pairplot.  It creates the pair plot with the seaborn library alias sns and the seaborn function pairplot

    pairplot = sns.pairplot(xl("Table1[#All]", headers=True))

    As the argument for the pairplot function, the code snippet uses the custom Python function xl() and references the first table in the worksheet, Table1. It includes the entire table contents (as [#All]), and notes that the table includes a header row (as headers=True). In this example, Table1 in the worksheet contains the Iris dataset.

    See the pair plot preview in a DataFrame.

    Note: If you encounter any errors, see Troubleshoot Python in Excel errors for more information. 

  2. After committing your Python formula, Python in Excel returns the pair plot visualization in an image object. Select the card icon in the image object cell to see a preview of the visualization.

    See a preview of the plot in a DataFrame card.

  3. Keep the visualization as an Image object to continue using it for Python calculations. Extract the image to the Excel grid to resize it and view each plot in more detail. To extract the image to the grid, right-click or Ctrl+click on the cell that contains the image object and select Display Plot over Cells from the menu.

    For additional information about extracting image objects, see the Extract an image object to the Excel grid article section in this article.

Create a scatter plot with Matplotlib 

This example describes how to create a scatter plot with the Iris flower sample data set. A scatter plot shows the relationship between two numerical variables in a data set. The example creates a scatter plot that looks like the following screenshot, comparing the sepal_width and sepal_length values.

Use Python in Excel to create a scatter plot with the matplotlib library.

Python in Excel creates the visualization with the Matplotlib open-source library. The Matplotliblibrary is automatically imported for Python in Excel with the following import statement, letting you reference it as plt.  

import matplotlib.pyplot as plt 

To create a scatter plot of the Iris flower data set with the Matplotlib library, take the following steps:

  1. In a Python in Excel cell, use the matplotlib scatter function and enter the sepal_length and sepal_width columns of the Iris data set as the arguments. In this example, Table1 in the worksheet contains the Iris dataset.

    plt.scatter(xl("Table1[sepal_length]"), xl("Table1[sepal_width]"))

  2. Add labels and a title to the scatter plot.  

    # Label the x and y axes of the plot. 
    plt.xlabel('sepal_length') 
    plt.ylabel('sepal_width') 
     
    # Add a title to the plot.  
    plt.title('Sepal length and width analysis')

    Note: You can add this code snippet as an additional line of code after the Python formula in the previous step, in the same Excel cell, or you can enter it in a new Python in Excel cell in your workbook. If you choose to enter it in a new cell, make sure to follow the row-major calculation order rules and enter it after the first cell.

  3. After committing your Python formulas, Python in Excel returns the scatter plot visualization as an image object. Select the card icon in the image object cell to see a preview of the visualization. 

    View a preview of the scatter plot in a DataFrame.

    Note: If you encounter any errors, see Troubleshoot Python in Excel errors for more information.  

  4. Keep the visualization as an image object to continue using it for Python calculations. Extract the image to the Excel grid to resize it and view the plot in more detail. To extract the image to the grid, right-click or Ctrl+click on the cell that contains the image object and select Display Plot over Cells from the menu.

    For additional information about extracting image objects, see the Extract an image object to the Excel grid article section in this article.

Extract an image object to the Excel grid

As demonstrated by the examples in this article, Python libraries such as seaborn and Matplotlib can return data visualizations to Excel cells. By default, Python in Excel returns these visualizations as image objects. 

Select the card icon in an image object cell to see a preview of the visualization.

See a preview of the plot in a DataFrame card.

There are two ways to extract an image to the Excel grid: display the image as a plot over cells or display the image within a cell.

Display the image over cells

Follow these steps to display the image as a plot over cells.

  1. Right-click or Ctrl+click on the cell that contains the image object and select Display Plot over Cells from the menu (or use the keyboard shortcut Ctrl+Alt+Shift+C). This creates a copy of the image that floats over the Excel grid and can be resized easily. The original image object remains in the original cell.

  2. Select and drag the plot image to move it around your worksheet. Select and drag the nodes at the corners and sides of the image to resize it.

    Create a floating plot visualization with the Create Reference icon.

Display the image within a cell

Follow these steps to display the image within a cell.

  1. Return the image object as an Excel value. Select the image object cell, go to the Python output menu in the formula bar, and select Excel Value.  

    Use the Python output menu next to the Formula Bar to change the output type.

    The plot image now displays within the cell.

    Tip: To toggle Python formula results between Python objects and Excel values, use the keyboard shortcut Ctrl+Alt+Shift+M. To learn more keyboard shortcuts, see Python in Excel keyboard shortcuts.

  2. The size of the plot image depends on the size of the cell. Increase the size of the image by increasing the size of the cell, or by merging cells.

Create your own plots and charts 

Now that you’ve learned how to create Python in Excel plots and charts with a sample data set, enter your own data into an Excel workbook and create custom visualizations.  

To import external data for use with Python in Excel, use Power Query. To learn more, see Use Power Query to import data for Python in Excel.

Related articles 

Need more help?

Want more options?

Explore subscription benefits, browse training courses, learn how to secure your device, and more.

Communities help you ask and answer questions, give feedback, and hear from experts with rich knowledge.

Was this information helpful?

What affected your experience?
By pressing submit, your feedback will be used to improve Microsoft products and services. Your IT admin will be able to collect this data. Privacy Statement.

Thank you for your feedback!

×