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.
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:
-
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.Note: If you encounter any errors, see Troubleshoot Python in Excel errors for more information.
-
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.
-
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.
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:
-
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]")) -
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.
-
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.
Note: If you encounter any errors, see Troubleshoot Python in Excel errors for more information.
-
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.
To extract the image to the Excel grid, take the following steps.
-
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:
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.
-
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.
-
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.
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.