Matplotlib Tutorial 10 - basic customizations, rotating labels

sentdex · Beginner ·🛠️ AI Tools & Apps ·10y ago
Skills: CV Basics80%

Key Takeaways

The video tutorial covers basic customizations in Matplotlib, including rotating labels, configuring subplots, and adding grids to charts. It demonstrates how to use Matplotlib functions such as plt.figure(), plt.subplot(), ax.set_xticklabels(), and grid=True to customize plots.

Full Transcript

what's going on everybody Welcome to the 10th python with map plot live for data visualization tutorial video in this video we're going to be talking about some real basic customizations of this graph mainly uh rotating that the labels and the axes but also how we actually start building uh the actual subplot and the figure and all that so uh let's go ahead and just hop right in first of all when you're building graphs there's really two two major ways that you can have multiple subplots and we'll be talking about those later on but for now we'll just use one of them also uh implied when you make a graph is this uh figure um I'm going to put fig equals PLT do figure here um and that's just again that's just kind of like an an an implied thing you don't have to have it there but if you want to start modifying the figure you have to reference the figure somehow so that's how we do it so pt. figure just are automatically already existed but this is how you can begin to modify it if you want now fig equals that and then what we're going to do is we're going to define ax1 a lot of people just say ax and then if they add more it's like ax2 and stuff I prefer to have like ax1 ax2 ax3 do whatever you want it's not a big deal so ax1 equals and then we're going to say PLT do subplot to grid and this is just one of the ways where we can have many subplots for now we're just going to have one but this the first like little pair here this first tle is the shape of the grid basically it's a 1 by one okay and then the second tle is the starting point of this uh plot 0 0 and then there's a few others here there's row span and column span we don't need it we're just doing a one by one here it's not necessary so uh we're not going to really add anything else there now what we're going to do is now that we've got ax1 ax1 is what we plot to not PLT so this is our first axes we're going to plot to that so instead of PLT do plot we do ax1 do plot so ax1 is a subplot it's going to take up as much space as all the other plots that we've done it's just a subplot so when we have pt. X label and Y label and all that uh what's happening there is we can leave that there most likely that should be fine later on we'll talk about how you can have you know like if you've got multiple plots how do you put the labels there and all that we'll get to that uh but for for now mainly what we have to change is the is what we're plotting so we've got that now uh let's go ahead and run that and make sure everything works up to this point before we do one more modification okay so everything's looking good the labels are where they want to where we want them and all of that what we want to do now is rotate the dates here and if we rotate them slightly we could fit a lot more dates but also they'll pretty much never run over each other like they do like in this example okay so we want to rotate them and rotating them is not too bad it just takes a little modification but like I was telling you everything in map plot lib is customizable it's really uh really well thought out so on ax1 we do this we do the plot uh right underneath that we're going to say uh for label in ax1 dox axes. getor tick labels so this gets all those objects that constitute these tick labels and then what we can do is we can modify them so label do we can do label. setor rotation rotation uh and then we set the degrees of the rotation so we set them out of 45° so that should be it's like a perfect slant um so we're modifying each label as e they like objects in these get tick labels and then we're using the set rotation method to set their uh tilt basic basically another interesting thing that we can go ahead and show as well well let's let's bring this one up and then I'll show one more thing and then we'll close this one out so now they're like kind of tilted like that so that's pretty cool uh so now we can zoom in and then we start finding ah we got one more problem now so we're tilted they're not running over each other but dang it they're off the screen so like I was saying before to we would use this configure subplots and then we can see that okay well let's see they're falling off the bottom so let's modify the the bottom a little bit give it a little more space and that looks about good so really the bottom needs to be 0.18 let's say and then we could bring some of these other things over but let's just do the bottom for now or we'll do them all I guess but let's just close this out and let's just let's start modifying those so we close close close and coming down to to basically the very very bottom here I usually do it I tend to like keep everything clumped together so uh first you know you've defined your figure you define all your axes then you define your date or your data sorry uh so your data goes there and then you start defining everything that goes onto each individual subplot which is your axis so all the ax1 stuff will go here and then if we have when and if we have ax2 you'll have a clump of ax2 stuff then ax3 and so on so uh and then at the bottom all the specific you know PLT stuff that's going to apply across the entire graph you put that stuff down here so we're going to do PLT do subplots adjust and then in here we put all the parameters now um we'll just kind of throw some in here and we'll throw them all in here and then later on we can adjust them as needed so we'll say left equals 0.9 bottom will be equal to 0.16 right will be equal to 0.94 and that needs to be right not Ry then we got top 0 point top equals 0.9 9 5 w space equals uh for now we'll just do 0.2 and then h space equals zero uh we'll talk about that later but basically this and this we're not even going to see but once you have multiple Figures it's like padding between the figures you got top padding and side padding that's what that is uh so with that the bottom I think we said the bottom should be 18 actually so we'll do 18 and the rest should be fine so let's go ahead and on that now what have we done okay so the left is screwed up somehow so uh left 0.09 try again okay that's better so uh we we're kind of squishing the top a bit that's okay we'll bring that down um and then like if we zoom in here even with like full date although as we zoom in we're still running out of space so we can give the bottom maybe a little bit more let's do 20 and then top needed a little more space let's do top is uh 93 let's do that see how that works out let's do 90 okay and then let's zoom in a little bit okay so that's good enough um let's zoom into a time too yeah so everything else that's all looking good we've got the date the time the uh titles fit the labels are fitting we're all good good the only other thing I want to show is like okay so say we're we're like we're looking at prices but we're like okay well what's the price right here right we could put our Mouse over that I guess and you can read in the bottom um the bottom right corner you can see okay y equals 190 but what if you just like want to visually look at that well generally we have a grid okay so we can add a grid really simply uh by doing um and we can add this by chart so we would say a x1. grid and then in here we just put true so we can save and run that and up Pops a chart and it's got a grid now the grid can also be customized a lot like your lines that you might plot so for example we could say true we could say color and we can say the color is equal to G for green we can also say line style equals and instead of that Dash we could say it's a solid line so let's run that one for now okay so now it's this green grid and then we could take it a step further in absurdity and we could say line width equals 5 okay so you can do all kinds of crazy stuff okay with the grid generally just grid equals true is good enough so I'm just going to close this off here and comment the rest of that out but you can take note that you can do all these other things if you want um along those same lines I'm going to go ahead and take this and get rid of all this wasted space I'll save this for now and then um probably will get rid of that line shortly but anyways so now we've customized the chart a little bit obviously we still have a long way to go in customization but that's some basic starting points uh for customizing and pulling data offline and getting a relatively okay looking chart but we have a lot of other things to cover I promise so anyways if you have any questions comments concerns whatever up until this point please feel free to leave them below otherwise as always thanks for watching thanks for all the support and subscriptions and until next time

Original Description

In this Matplotlib tutorial, we're going to be talking about some of the possible customizations to graphs. In order to start modifying the subplots, we have to define them. We will talk about them soon, but there are two major ways to define subplots, and to structure them. For now, we'll just use one of them, but we will be explaining them shortly. sample code: http://pythonprogramming.net http://hkinsley.com https://twitter.com/sentdex http://sentdex.com http://seaofbtc.com
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from sentdex · sentdex · 0 of 60

← Previous Next →
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
52 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

This tutorial teaches beginners how to customize Matplotlib plots by rotating labels, configuring subplots, and adding grids. It provides step-by-step instructions on how to use Matplotlib functions to achieve these customizations. By following this tutorial, viewers can improve their data visualization skills and create more effective plots.

Key Takeaways
  1. Create a figure object using plt.figure()
  2. Create a subplot object using plt.subplot()
  3. Plot data on the subplot using ax.plot()
  4. Add labels to the subplot using plt.xlabel() and plt.ylabel()
  5. Add a title to the subplot using plt.title()
  6. Configure subplots to adjust the layout
  7. Add a grid to the chart using grid=True
  8. Customize the grid with color and line style
  9. Remove wasted space with customization
💡 Customizing plots with Matplotlib can significantly improve the effectiveness of data visualization, and using functions such as plt.figure(), plt.subplot(), and grid=True can help achieve this customization.

Related AI Lessons

Up next
Salesforce Flow New Features (Summer '26) | Open Record, URL & Show Toast Messages
AITECHONE
Watch →