0
0
Matplotlibdata~30 mins

3D bar charts in Matplotlib - Mini Project: Build & Apply

Choose your learning style9 modes available
3D Bar Charts
📖 Scenario: You work in a small store that sells three types of fruits: apples, bananas, and oranges. You want to show how many fruits were sold in three different months using a 3D bar chart. This will help your team quickly see which fruit sold the most each month.
🎯 Goal: Create a 3D bar chart using matplotlib to display the sales of apples, bananas, and oranges over three months.
📋 What You'll Learn
Create lists for fruit names and months
Create a list of sales numbers for each fruit in each month
Use matplotlib to create a 3D bar chart
Label the axes with fruit names, months, and sales numbers
Display the 3D bar chart
💡 Why This Matters
🌍 Real World
3D bar charts help visualize data with two categories and one numeric value, such as sales over time for different products.
💼 Career
Data analysts and scientists use 3D bar charts to present complex data clearly to stakeholders and decision makers.
Progress0 / 4 steps
1
Create the sales data lists
Create a list called fruits with these exact values: 'Apples', 'Bananas', 'Oranges'. Create a list called months with these exact values: 'January', 'February', 'March'. Create a list called sales with these exact values: [20, 35, 30] for January, [25, 32, 34] for February, and [22, 30, 35] for March. Store these three lists in a variable called sales as a list of lists, where each inner list corresponds to a month.
Matplotlib
Need a hint?

Use three separate lists. The sales list should be a list of lists, each inner list for one month.

2
Set up the 3D plot
Import matplotlib.pyplot as plt and import Axes3D from mpl_toolkits.mplot3d. Create a figure called fig using plt.figure(). Add a 3D subplot to fig and store it in a variable called ax.
Matplotlib
Need a hint?

Use fig.add_subplot(111, projection='3d') to create the 3D axes.

3
Plot the 3D bars
Use range(len(months)) and range(len(fruits)) to loop over months and fruits. For each month and fruit, use ax.bar3d() to draw a bar. Use the month index for the x position, the fruit index for the y position, and 0 for the z position. Use 0.5 for the width and depth of each bar. Use the sales number for the height. Store the x positions in xpos, y positions in ypos, z positions in zpos, widths in dx, depths in dy, and heights in dz. Use nested for loops with variables i for months and j for fruits.
Matplotlib
Need a hint?

Use ax.bar3d() inside nested loops. The height dz is the sales number for month i and fruit j.

4
Label axes and show the plot
Set the x-axis ticks to the month indices and labels to the months list using ax.set_xticks() and ax.set_xticklabels(). Set the y-axis ticks to the fruit indices and labels to the fruits list using ax.set_yticks() and ax.set_yticklabels(). Set the z-axis label to 'Sales' using ax.set_zlabel(). Finally, call plt.show() to display the 3D bar chart.
Matplotlib
Need a hint?

Use ax.set_xticks(), ax.set_xticklabels(), ax.set_yticks(), ax.set_yticklabels(), and ax.set_zlabel(). Then call plt.show().