How to Create a 3D Pie Chart in Power BI with AI

Cody Schneider

Power BI is an incredible tool for data visualization, but you’ve probably noticed one popular chart type is missing: the 3D pie chart. While dashboard purists might argue about its effectiveness, sometimes you just need that extra visual pop for a presentation. This guide will show you how to bypass Power BI’s limitations and create a dynamic 3D pie chart using its built-in Python integration, all with a little help from AI to write the code for you - no scripting experience required.

Why Bother with a 3D Pie Chart? (And a Quick Word of Caution)

Flat, 2D charts are generally easier to read and more accurately represent data proportions. However, 3D charts, especially pie charts, have a strong visual appeal that can make your reports and presentations more engaging. They add a sense of depth and can make a simple chart feel more professional or polished, which is often exactly what stakeholders are looking for.

Just be mindful: the 3D perspective can sometimes distort the perceived size of the slices, making smaller sections look even smaller and larger ones more dominant. Our goal here isn’t to replace all your charts with 3D versions, but to give you the option when you need to grab your audience's attention.

Setting Up Your Power BI Environment for Python

Before we can get AI to write our code, we need to make sure Power BI is ready to run it. This involves installing Python and two essential data visualization libraries, Matplotlib and Pandas. If you already have these set up, feel free to skip ahead.

Step 1: Install Python

Power BI doesn't come with Python pre-installed. You’ll need to install it on your local machine first.

  • Go to the official Python website (https://www.python.org/downloads/) and download the latest version.

  • During installation, make sure to check the box that says "Add Python.exe to PATH." This is a critical step that allows Power BI to find and use Python automatically.

Step 2: Install Required Libraries

Next, we need the tools that will actually build the chart. We'll use the command line to install them.

  1. Open the Command Prompt (on Windows, search for "cmd" in the Start Menu).

  2. Install the Pandas and Matplotlib libraries by typing the following commands and pressing Enter after each one:

Pandas helps manage the data that Power BI sends to the Python script, and Matplotlib is the library that will draw our 3D pie chart.

Step 3: Enable Python Scripting in Power BI

Finally, let's tell Power BI where to find your new Python installation.

  1. Open Power BI Desktop.

  2. Go to File > Options and settings > Options.

  3. In the Options window, select Python scripting from the left-hand menu.

  4. Power BI will often auto-detect your Python home directory. If it doesn't, browse to the folder where you installed Python.

  5. Click OK to save the settings.

With that, your environment is ready. Now we can start building the chart.

Using AI to Generate the Python Code for Your Chart

Here’s where the magic happens. You don’t need to learn Python because we can ask a generative AI tool like ChatGPT, Google Bard, or Microsoft Copilot to write the code for us. The key is to provide a clear and specific prompt.

Crafting the Perfect Prompt

You want to tell the AI exactly what you need. A good prompt includes the libraries to use, the type of chart, and the structure of the data Power BI will provide.

Here’s a fantastic starting prompt you can copy and paste:

“Write a Python script for a Power BI visual. The script should use Matplotlib to create a 3D pie chart. The data will be in a Pandas DataFrame called 'dataset' with two columns: a category column named 'Category' and a numerical value column named 'Sales'. The pie chart should show the sales distribution by category."

The AI-Generated Code

The AI will likely give you something that looks like this. Don't be intimidated by the code, we'll quickly break down what it does.

What does this code do?

  • import matplotlib.pyplot as plt: This line imports the plotting library we need.

  • categories = dataset['Category'] and values = dataset['Sales']: This takes the data from the Power BI-provided 'dataset' and assigns the 'Category' and 'Sales' columns to variables.

  • ax.pie(...): This is the main function that creates the pie chart. It uses our values for the slice sizes and categories for the labels.

    • autopct='%1.1f%%': This formats the labels on the chart to show percentages with one decimal place.

    • shadow=True: This is the simple trick that adds a shadow effect, giving the chart a subtle 3D look.

    • startangle=90: This rotates the chart so the first slice starts at the top.

  • plt.show(): This command tells Power BI to display the chart we just created.

Adding the Python Visual to Your Power BI Report

Now that we have the code, let's put it all together in Power BI.

Step 1: Get Your Data Ready

First, make sure your report has the data you want to visualize. For this example, let's assume you have a simple table with product categories and their corresponding sales figures, like this:

  • A text column for the categories (e.g., "Electronics," "Apparel," "Home Goods").

  • A numerical column for the values (e.g., "Total Sales").

Step 2: Add the Python Visual

In the Visualizations pane in Power BI, click on the Python visual icon (it looks like "Py"). This will add a blank Python visual placeholder to your report canvas and open the Python script editor at the bottom.

Step 3: Drag in Your Data

With the new blank visual selected, drag your category field (e.g., "Category") and your value field (e.g., "Sales") from the Data pane into the Values box for the Python visual. This makes the data available to our script inside the 'dataset' DataFrame.

Step 4: Paste and Run the Code

Go to the Python script editor that appeared at the bottom of the screen. Copy the code generated by the AI and paste it into this editor. Once pasted, click the "Run script" icon (a play button) in the editor's toolbar.

In a few moments, your static 3D pie chart will appear on the report canvas!

Customizing Your 3D Pie Chart with AI

What if you want to make some changes? The great thing about using AI is that you don’t need to learn a bunch of Matplotlib syntax. You can just ask the AI to modify the code for you.

Example 1: Exploding a Slice

Let's say you want to highlight the largest sales category. You can "explode" a slice to make it stand out.

Your follow-up prompt to the AI: "Modify the previous script to 'explode' the first slice of the pie chart by a factor of 0.1."

The AI will add a few lines to your code:

Just copy this updated code back into the Power BI Python script editor and re-run it. Your chart will update with the first slice pulled out slightly.

Example 2: Changing the Colors

The default colors might not match your brand. Let's change them.

Your follow-up prompt to the AI: "Modify the script to use a custom color palette for the pie chart slices. Use these colors: '#66b3ff', '#ff9999', '#99ff99', and '#ffcc99'."

The AI will likely add a colors list:

Again, just replace your old code with the new version and run the script to see your newly colored chart.

Alternative Method: Using Plotly for Interactive 3D charts

While Matplotlib is great for static charts, Plotly is another fantastic Python library that can create interactive charts. An AI can just as easily write Plotly code for you.

First, install it via command line:

Then, try this prompt with your favorite AI tool:

“Write a Python script for Power BI using Plotly Express to create an interactive 3D pie chart from a DataFrame called 'dataset'. It has a 'Category' and a 'Sales' column. Make the chart title 'Sales by Category'.”

This code will generate an HTML-based visual inside your Power BI report that you can hover over to see details and even rotate. Keep in mind that Plotly visuals can sometimes be a bit heavier on performance than static Matplotlib charts, but the added interactivity is often worth it.

Final Thoughts

Creating custom visualizations like a 3D pie chart in Power BI doesn't have to be limited by the default options. By combining the power of Python scripting with the convenience of AI code generation, you can build just about any chart you can imagine, even without being a developer.

While digging into Python scripts offers a lot of control, we know that the real goal is to get insights fast. Hunting down code, even with AI, and managing different platforms can still fragment your workflow. At Graphed, we’ve built the AI-native experience right into your analytics so you can simply ask for what you need in plain English. Instead of setting up a Python environment and generating code, you just connect your sources and ask, "Create a pie chart showing Shopify sales by product category for the last 30 days," and we build the live, interactive dashboard instantly. It saves you the technical setup and helps you move directly from question to insight in seconds.