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 a sample machine learning data set called the Iris flower data set. Download a copy of the data set and open it in Excel for Windows to follow along with the samples: 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 the following screenshot for an example.

  3. You can keep the visualization as an image object to continue using it for Python calculations, or you can extract the image to the Excel grid to resize it and view each plot in more detail. To learn how to extract the image, 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. See the following screenshot for an example.

    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. You can keep the visualization as an image object to continue using it for Python calculations, or you can extract the image to the Excel grid. To learn how to extract the image, see the Extract an image object to the Excel grid 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.

To extract the image to the Excel grid, take the following steps. 

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

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

    The visualization 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. To increase the size of the image, use the Create Reference button that appears when the cell containing the image is selected. The Create Reference button creates a copy of the image that floats over the Excel grid and can be resized easily. The original image remains in the original cell.

    Use the Create Reference icon to make a copy of the plot visualization.

  3. Select and drag the reference image to move it around your worksheet. Select and drag the nodes at the corners and sides of the image to resize it. See the following screenshot for an example.

    Create a floating plot visualization with the Create Reference icon.

    Note: Instead of creating a floating image object, you can also increase the size of the visualization by using the Merge Cells option in the Excel ribbon to create a larger area, and then placing the image in the merged 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 data with 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!

×