3D Bar Charts in Python and Matplotlib
Skills:
AI-Assisted Code Review90%
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
▶
53
54
55
56
57
58
59
60
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
Python Encryption Tutorial with PyCrypto
sentdex
Python's Logging Function
sentdex
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
wxPython Programming Tutorial 4: Panels
sentdex
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
wxPython Programming Tutorial 8: Custom Button Images
sentdex
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
Basic PHP Tutorial 12: Associative Array
sentdex
Basic PHP Tutorial 14: Foreach loop
sentdex
Basic PHP Tutorial 16: Include and Require
sentdex
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
Basic PHP Tutorial 4: Variables and Comments
sentdex
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
Basic PHP Tutorial 1: Intro to PHP
sentdex
Basic PHP Tutorial 3: HTML with PHP
sentdex
Basic PHP Tutorial 9: While Loop
sentdex
Basic PHP Tutorial 10: Switch Statement
sentdex
Basic PHP Tutorial 2: Print and Echo
sentdex
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
Basic PHP Tutorial 19: Finding words in strings
sentdex
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
Basic PHP Programming Tutorial 24: String similarity
sentdex
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
Sikuli Tutorial 1: Visually programming in python!
sentdex
Sikuli Tutorial 2: Program visually in python!
sentdex
Sikuli Tutorial 3: Program visually in python!
sentdex
3D Bar Charts in Python and Matplotlib
sentdex
3D Plane wire frame Graph Chart in Python
sentdex
Raspberry Pi Part 1 Introduction
sentdex
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
Raspberry Pi Part 11: Remote Desktop
sentdex
Twitter Analysis: How to rank a user's influence
sentdex
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex
More on: AI-Assisted Code Review
View skill →Related Reads
📰
📰
📰
📰
The Collapse of ‘Who Understands, Who Maintains’ — A Software Landscape in the AI Age
Medium · Programming
Building an OpenAI + Azure SQL App from Scratch
Medium · Python
Breaking the Abstraction Tax: Mastering Custom C++ Operations for High-Performance Edge AI on Android
Dev.to · Programming Central
Five Habits I'm Unlearning After Claude Code 101
Medium · Data Science
🎓
Tutor Explanation
DeepCamp AI