3D Bar Charts in Python and Matplotlib

sentdex · Intermediate ·💻 AI-Assisted Coding ·12y ago

Key Takeaways

Creates 3D bar charts in Python using Matplotlib

Full Transcript

Hello everybody welcome back to another mat plot lib 3D tutorial in this tutorial we're going to be building some skyscrapers not really but that's what it's going to look like so anyway let's go ahead and hop right on into it the first thing that we're want to do is import the necessary Imports and these Imports are going to be from MPL toolkit. lot 3D import axis 3 capital D next we want to go import Matt plot live. pyplot as PLT and then import lovely numpy as MP as we get further into all these kinds of tutorials you're going to start to really just adore the creators of numpy there's so many cool things in numpy that just make life so much easier continuing on figure equals plot figure nothing new there and then we're going to Define our axis Oneal fig. addore subplot and it's going to be 111 and a projection equal 3d nothing new here besides obviously the 3D uh tutorial section um so what I'm going to do here and kind of explain right before I start coding this is with the bar chart it's not the same as a plot like with the plot chart so far we've had an x a y and a z that was it really because a plot is just literally a singular position on a chart whereas a bar chart um really has more than just XYZ it's got like an XYZ and then like another XYZ and probably the best way to say this is like an XYZ and then you've got a Delta X Delta y Delta Z right how much has it how much does it from that one plot and then it changes a little bit to another plot right so for example you've got so if if you don't quite get the Delta part then it's where do you start your X y's and Z's and then where do you end the X y's and Z's so if you're not quite U following just yet luckily this is all completely visual so if you don't quite understand just yet you'll be able to kind of toy around and I'll do a little bit of toying around and kind of drive that point home um otherwise just play with the numbers a little bit and see what like different visualization pops up for you and uh you should have no problem kind of discerning uh what's going on so with that let's go ahead and continue we're going to have these are going to be our starting um our starting positions right so you're going to have X position equals and then let's make this easy we'll just do 1 2 3 4 5 6 7 8 9 and a 10 next we have y position y starting point point now we're going to do 2 3 4 5 1 uh 6 2 1 7 and a 2 now the next thing you have is a z position now first I'm going to show you guys the literal looking Z position and then I'm going to come through and show you guys from now on the way you ought to do this but in order to understand exactly what we're coding this is what we're going to go ahead and do just fill it with zeros next um and you might know the proper function to do this uhu but I do suggest you just fill it with zeros for now so you can follow along with the tutorial um because Z position does uh matter for this tutorial the next thing uh that we want to do is Define our changes our Delta X deltay Delta Z and so on so we'll say uh Delta x equals um and for this we're just going to say uh one but there is a function for this built in the numpy which you just do np. On's and then you specify how many you want and we want 10 cuz that's how deep our array is so what this is going to do is for DX it's going to set an array of ones in the length of 10 so now we're going to do the same thing with Delta y it's going to be equal mp1s 10 and then we're going to do Delta Z and just just to kind of uh make this an interesting looking chart we're going to go uh 1 2 3 4 5 68 n and 10 and also again this is so it's a little easier to follow along and understand what's going on here if you don't already so now we want to actually plot this so plotting is a1. bar 3D so it's just you know we're going to it's almost just like ax1 plot right it's just bar 3D tells it hey we're going to we're going to throw this funky stuff at you so X position y position Z position Delta X Delta y Delta Z and you could just do this but let's add some color to it make it pretty we're going to say color equals and in here as always you can always uh put in HEX codes so we're going to do 00 C EAA it's going to give us kind of like a lightish blue almost like a c probably and plot show so save that we'll run hopefully we didn't get any typos I hate when I typo charts nope so here is our 3D chart looks awesome now to give you an idea about a couple of things before we change things up um first of all as far as like scrolling around and zooming is concerned it's all the same that we've already kind of covered um so anyway the next thing I'd like to show you guys because I've zoomed now it's not on the bottom let me see if I can get the zero back on the there okay so now let's see if I can get to the point yeah so see this how they line up 1 2 3 4 5 6 seven eight and there is a 10 I promise there it is so the what's happening here this is our Delta variable right remember that that Delta change if we bought back over here Delta X Delta y Delta Z see how we did 1 2 3 4 5 6 7 9 10 now what if we did um 10 here and now we'll save that we'll rerun it bop back over and all of a sudden No Doubt this one's messing up it's not part of the steps this suddenly these stairs are unusable anyway um so that's the Delta X um that hopefully you can start to understand Delta X now now what if we're going to return that back to five now the other thing I was saying was about these positions um now normally you would just say you would for the Z position you would use the MP zeros because because right all we really wanted was a bunch of zeros so for Z position and the reason we want a bunch of zeros actually let me go ahead and just show you that let's replace this with a three now we'll save that we'll run it and you'll see one of these yep uh one of these stairs now starts at the three obviously we're not seeing let me see if I can zoom in yeah now we got the three there so as you can see it's starting at three so the z um start point is a three and it's still changing by a Delta of four right but it starts at three and it goes all the way up till seven so just to give you an idea so like if you wanted to do like a stacked bar chart like I mean that's a valid way to do bar charts then you would want to utilize the Z positions but if you're not doing a stacked bar chart then you would really just do mp0 and you want 10 of those save that and you would run it and sure enough we're back to our you know steps our beautiful steps so anyway um that's how you would how you would uh go about doing uh either a stacked bar chart or just a regular bar chart CU IE I I imagine a lot of you guys just want to start at zero for Z but it's really helpful to know that you can start at four right and then you can start building stacked 3D bar charts epic stuff so anyways as always thanks for watching thank you for your support your subscriptions and until next time

Original Description

Besides 3D scatter plots, we can also do 3D bar charts. This again allows us to compare the relationship of three variables rather than just two. 3D bar charts with matplotlib are slightly more complex than your scatter plots, because the bars have 1 more characteristic: depth. Basically, the "thickness" of the bars is also define-able. For most, this will be simply a pre-determined set of data, but you can actually use this for one more "dimension" to your plot. Sample code: http://pythonprogramming.net/3d-bar-charts-python-matplotlib/ Full Playlist: http://www.youtube.com/playlist?list=PLQVvvaa0QuDfpEcGUM6ogsbrlWtqpS5-1 Sentdex.com Facebook.com/sentdex Twitter.com/sentdex How to make a 3D bar chart in python.
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from sentdex · sentdex · 52 of 60

1 Matplotlib Python Tutorial Part 1: Basics and your first Graph!
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
2 Python Encryption Tutorial with PyCrypto
Python Encryption Tutorial with PyCrypto
sentdex
3 Python's Logging Function
Python's Logging Function
sentdex
4 wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
5 wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
6 wxPython Programming Tutorial 3: Menu Bar and Menu Button
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
7 wxPython Programming Tutorial 4: Panels
wxPython Programming Tutorial 4: Panels
sentdex
8 wxPython Programming Tutorial 5: User Input Saved To Variables
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
9 wxPython Programming Tutorial 6: Multiple Choice Input
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
10 wxPython Programming Tutorial 7: Adding Static Text and Colors
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
11 wxPython Programming Tutorial 8: Custom Button Images
wxPython Programming Tutorial 8: Custom Button Images
sentdex
12 wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
13 Basic PHP Tutorial 13: Multi-dimensional Array
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
14 Basic PHP Tutorial 15: Functions and Global Variables
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
15 Basic PHP Tutorial 12: Associative Array
Basic PHP Tutorial 12: Associative Array
sentdex
16 Basic PHP Tutorial 14: Foreach loop
Basic PHP Tutorial 14: Foreach loop
sentdex
17 Basic PHP Tutorial 16: Include and Require
Basic PHP Tutorial 16: Include and Require
sentdex
18 Basic PHP Tutorial 7: Assignment, comparison and Logical operators
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
19 Basic PHP Tutorial 4: Variables and Comments
Basic PHP Tutorial 4: Variables and Comments
sentdex
20 Basic PHP Tutorial 11: Arrays part 1, basic array
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
21 Basic PHP Tutorial 6: If else and else if conditionals cont'd
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
22 Basic PHP Tutorial 1: Intro to PHP
Basic PHP Tutorial 1: Intro to PHP
sentdex
23 Basic PHP Tutorial 3: HTML with PHP
Basic PHP Tutorial 3: HTML with PHP
sentdex
24 Basic PHP Tutorial 9: While Loop
Basic PHP Tutorial 9: While Loop
sentdex
25 Basic PHP Tutorial 10: Switch Statement
Basic PHP Tutorial 10: Switch Statement
sentdex
26 Basic PHP Tutorial 2: Print and Echo
Basic PHP Tutorial 2: Print and Echo
sentdex
27 Basic PHP Tutorial 5: If else and else if conditional statements
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
28 Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
29 Basic PHP Tutorial 17: User Input Form Example / String Manipulation
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
30 Basic PHP Tutorial 18: HTML Entities and forms cont'd
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
31 Basic PHP Tutorial 19: Finding words in strings
Basic PHP Tutorial 19: Finding words in strings
sentdex
32 Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
33 Basic PHP Programming Tutorial 22: Hashing part 2: salting
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
34 Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
35 Basic PHP Programming Tutorial 21: MD5 Hashing For Security
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
36 Basic PHP Programming Tutorial 24: String similarity
Basic PHP Programming Tutorial 24: String similarity
sentdex
37 Basic PHP Programming Tutorial 25: Time and Time stamps
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
38 Basic PHP Programming Tutorial 26: Die and Exit
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
39 Basic PHP Programming Tutorial 27: MySQL Databases Part 1
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
40 Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
41 Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
42 Basic PHP Programming Tutorial 30: MySQL database in Use
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
43 Django Tutorial Web Development with Python Part 1: Installing Django
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
44 Python Tutorial: File Deletion and Folder Deletion / directory deletion
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
45 Python Tutorial: How to Rename Files and Move Files with Python
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
46 3D Graphs in Matplotlib for Python: Basic 3D Line
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
47 3D Plotting in Matplotlib for Python: 3D Scatter Plot
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
48 3D Charts in Matplotlib for Python: Multiple datasets scatter plot
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
49 Sikuli Tutorial 1: Visually programming in python!
Sikuli Tutorial 1: Visually programming in python!
sentdex
50 Sikuli Tutorial 2: Program visually in python!
Sikuli Tutorial 2: Program visually in python!
sentdex
51 Sikuli Tutorial 3: Program visually in python!
Sikuli Tutorial 3: Program visually in python!
sentdex
3D Bar Charts in Python and Matplotlib
3D Bar Charts in Python and Matplotlib
sentdex
53 3D Plane wire frame Graph Chart in Python
3D Plane wire frame Graph Chart in Python
sentdex
54 Raspberry Pi Part 1 Introduction
Raspberry Pi Part 1 Introduction
sentdex
55 Raspberry Pi Part 8: First Download and Update! (Firmware)
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
56 Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
57 Raspberry Pi Part 11: Remote Desktop
Raspberry Pi Part 11: Remote Desktop
sentdex
58 Twitter Analysis: How to rank a user's influence
Twitter Analysis: How to rank a user's influence
sentdex
59 GPIO Tutorial for Pi Part 2 - Programming the GPIO
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
60 GPIO Tutorial for Raspberry Pi Part 1 - Setting up
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex

Related Reads

📰
The Collapse of ‘Who Understands, Who Maintains’ — A Software Landscape in the AI Age
The increasing complexity of code due to AI is outpacing human understanding, leading to a collapse of the 'who understands, who maintains' principle in software development
Medium · Programming
📰
Building an OpenAI + Azure SQL App from Scratch
Learn to build an app combining OpenAI and Azure SQL from scratch, a unique approach differing from typical Postgres-based tutorials
Medium · Python
📰
Breaking the Abstraction Tax: Mastering Custom C++ Operations for High-Performance Edge AI on Android
Learn to optimize Edge AI on Android using custom C++ operations to break the abstraction tax and achieve high-performance
Dev.to · Programming Central
📰
Five Habits I'm Unlearning After Claude Code 101
Learn how to effectively use Claude Code 101 by unlearning common habits and understanding its true potential beyond a fancy autocomplete
Medium · Data Science
Up next
How to Start a SaaS Business in 2026
Learn With Shopify
Watch →