In this tutorial, we will learn to plot a 3D Pie Chart using Python. Python’s vast library ecosystem offers several plotting libraries such as Matplotlib, seaborn, etc. Here, we will use Matplotlib, a handy library for generating static, animated, and interactive visualizations in Python.
Step 1: Installing Required Libraries
We need to install the Matplotlib library. If not already installed, it can be done using pip:
1 |
pip install matplotlib |
Step 2: Importing Required Libraries
After installation, import the required libraries.
1 |
import matplotlib.pyplot as plt |
Step 3: Preparing the Data
The next step is to prepare the data that will be plotted. Define the labels (categories) and the sizes (values) for your chart.
1 2 |
labels = ['Category1','Category2','Category3','Category4'] sizes = [15, 30, 45, 10] |
Step 4: Generating the Pie Chart
Once data is ready, you can create a Pie chart by calling the pie() method in matplotlib. Add the necessary parameters along with your data. As we plot a 3d pie chart, we use the shadow parameter to give the 3D effect.
1 |
plt.pie(sizes, labels=labels, shadow=True) |
Step 5: Displaying The Chart
Calling plt.show() will display your chart.
1 |
plt.show() |
Pie Chart Output
The pie chart should now be displayed on your screen.
Full Code
1 2 3 4 5 6 7 8 |
import matplotlib.pyplot as plt labels = ['Category1','Category2','Category3','Category4'] sizes = [15, 30, 45, 10] plt.pie(sizes, labels=labels, shadow=True) plt.show() |
Conclusion
A 3D pie chart is an excellent tool to visualize data in three dimensions in Python. Experiment with different parameters of the pie function to get varying visuals according to your preferences. Happy coding!