Python 3 Programming Tutorial - Matplotlib Styles

sentdex · Beginner ·🛠️ AI Tools & Apps ·11y ago

Key Takeaways

This video tutorial covers Matplotlib styles, which work similarly to CSS stylesheets, allowing for parameter specification and automatic styling of plots in Python 3, using tools like Matplotlib and ggplot.

Full Transcript

Hello everybody and welcome to another mattplot lib in Python 3 uh video in our little minieries here. In this video what I want to be talking about is styles in mapplot lib. Um so mapplot liib uh has this functionality for styles and I was under the impression that it was contained in one of the newer downloads of mapplot liib. However, I could not find it uh the last time I downloaded mapplot lib. Uh but I was able to find a thirdparty example and so I use I've been using that and so I thought when I did these videos, hey, I get to use a new version of mapplot lib since I'm on a new version of Python. And I thought I could just download the three, you know, version and find styles, but I I could not find it there. So, um someone happens to know where they're hiding styles in the original mapplot live download, let me know. But anyway, moving on. I'm going to show you guys um my version of styles and uh so let's get started. So we've got mapplot lib here. We're plotting this chart and just for example got this epic chart. You know it looks cool. We've charted stuff but it's pretty boring chart. So we can close out of that. And the next thing we want to do is we want to add styles. And if you're familiar with like CSS like HTML and CSS, you know you have things called stylesheets. And what these do is you can specify a bunch of parameters to your stylesheet. And then when you add new pages, like new uh content and stuff, they just you don't have to code in how you want it to look. It just automatically does it because you you've made the stylesheet. And that's basically how Mattplot lib styles work. I'm guessing they got their name from it. Um and so it's the same thing. And then what you can do even further though is you can specify many stylesheets. So you can say like this is the styles that I want to use for line charts. This is the style I want to use for pie charts. This is a style for uh histograms and bar charts, all this stuff. So anyway, uh let's go ahead and get started. So come over here and we're going to have to first grab styles. And so I've uploaded my version of styles to pythonprogramming.net. And then you want to go downloads and then slashstyles.zip or so I thought. Oh, sorry. It's actually style.zip. style.zip. There we go. And we get our style.zip download. Once you have that, you're going to want to uh go back, go to your downloads. Uh so we'll go here, downloads. And where did I stick style? There it is. And then we'll go ahead and extract all the style here. And then we get this. And if you click on style, you come right into here. Okay. So, uh just for reference. Okay. And then you come to style li and then here are styles. Uh but no need to worry about that. What we're curious about is style right here the style folder. So then what you want to do go to my computer computer whatever C python 34 or whatever Python version you happen to be using lib site packages mattplot lib. And here's where you want to stuff the style folder. So let's move this over here. This one over here. And then we take style from here. Just click and drag it into here. And now we have style. So uh let's minimize this. Minimize this. And we'll come over here. And now uh the only thing that we need to do from this point is first we have to import style. So from mattplot lib import style. And just for the record um importing style is importing literally what we just moved in. So, uh, mattplot lib style. So, when you go from mattplot lib import style, if you remember, uh, the tutorial on what imports are and all that, that's literally importing this this file right here. And then it takes all of this. And then initiation or the initialize basically is what we're about to do from core import use context blah blah blah. You'll see that we're about to use that right now. So, from mapplot lib import style. Then here we go style. And then we specify the style that we want to use. So if we come back over to our uh distribution here of style, click on style liib, we've got ggbot plot, grayscale, and dark background. All of these I did not create. These are pre-created by somebody else. Um ggplot is one that I've just kind of edited a little bit. I didn't really like the original one, so I kind of edited it and uh got what I wanted. But to use it, all you you skip MPL style and you just say we want to use ggplot. Okay. So now we can save and run this. And now we get a chart that's kind of changed, right? So the background isn't this ugly gray nastiness. We've got nice grid lines, but they're not like in your face black grid lines. Uh a red line, but you can see here that actually um we let's let's do let's just copy this right here and paste. And then let's change up our y a little bit. 6726. And we'll call this x2 y2. And then we'll plot some more. So plt.plot x2 y2. We'll save and run that. And we see that there's another color. And these colors are just more I don't know uh goodlooking than the the natural colors that we get. Um so for example, we can go back to what we were using. I'm going just comment out style use. Save and run that. And here we are back to the original. So it's just kind it just doesn't look that good. And so anyway, we start style ggplot, but then there's also these other ones like dark background. So we could go dark background. And you can see how easy it is to customize your plots very quickly. So this is a dark background. So changes all the data to like lighter colors and the background to black. Um so we can change get out of that. And then finally we have grayscale. And this basically it just uh plots in, you know, grayscale. It's very true to its name, grayscale, right? So, we can save and run that. And we get this chart that's just pure grayscale. It actually looks pretty good if you have a pretty complex chart. The grayscale charts can look pretty decent. Right now, it's pretty boring, but anyway. So, uh that's that with uh styles. Now, I do just want to show a couple of things. Uh, for example, like if we let's say we comment out style.use. Um, and actually I'll probably be uploading this code. So I want to take out gray grayscale because it's ugly. Um, so we'll use gplot. Anyway, I'll comment this out and I now I'm going to show you guys how you would actually um color this stuff on your own. So here we've got x. Uh, so let's run it real quick just to see where we are. So we have this now. So the default plot. Close this. Close this. And we don't need printing length anymore. So now plot x y x2 y2. Now when you plot stuff, here's how you can change the color yourself. You can actually go uh we could say the first line uh we want this line to be uh blue. Actually, I think the default is blue. So we'll call it green. Thank you, dog, for barking. Anyway, we'll call that green. Uh then we're going to say line width equals five. And this is going to change how thick the line is. So we can save and run that. And as you can see, we've made the first line this green color. The second one came out blue because we didn't specify it. So that's like the first default color that's going to come out. Um but what we've done here is we got green. And as you can see, it's significantly thicker than the other line. Um and then we can come down here and we can do something special for this one, too. So instead of blue, we'll call it C for cyan. And then we'll say line width equals 10. This will be fun. Save and run it. And then here we get epic info. And we get, you know, these lines here. Um, so that's just some some quick simple things as far as, you know, changing some stuff. Now again, there are just an incredible amount of changes that you can make. And in fact, let's pull up ggplot real quick and see all of the changes that ggplot makes for us. Um, whoops. That was it right here. So, we come over here and immediately, okay, line width changed for us. Uh, face color, which is the background um of background of things basically face color. Edge colors changed. I don't even know what Oh, it's anti-aliasing. It's like I don't know what that is. Anyway, font size, I actually changed that. The default one has a really small font. Uh, the default like whoever made ggplot face colors are changed. all of your uh plots are changed. Uh just for the record, uh I did this. I added this list. It's basically the same list copy and pasted over and over and over. It says axis.color cycle in Python 2.7. It does not cycle through the colors once it gets at the end of the list. It just keeps repeating itself uh the last color and that was very irritating to me because I had a plot that contained a lot of lines and uh so that was very problematic. So I I changed that. I I would probably assume in Python 3 that's fixed, but anyway, that's why there's so many there. And then down here, it just is just commented out saying what all these colors are. X ticks are changed, Y ticks are changed, grids are changed, line styles are changed, uh face color, this is the face color of the whole figure. So the background there, and then the edge color is, you know, 0 50. I don't know what that is, if that's a uh alpha or what. Anyway, um so as you can see, there's a lot of customization here and each of the ones like dark scale, same thing. It's going to have a lot of specific customization to it. Um normally these these are all things you'd have to type out to get that to change. But as you can see, there's a lot of things that you can change um about your graphs, but again, not going to get too deep into that. Uh nor am I going to get too deep into fancy charting or anything like that. Uh if you want to see that, you can check out uh the other series I have on mattplot lib. So that's going to conclude this video. A lot of stuff that we kind of threw at you, but really just keep in mind styles and then you can change line width. Congratulations. Uh so anyways, if anybody's has any questions or comments on this video, feel free to leave them below. As always, thanks for watching. Thanks for all the support and subscriptions and until next time.

Original Description

In this Python 3 and Matplotlib tutorial video, we cover Matplotlib styles. Matplotlib styles are a lot like css stylesheets, where you set the rules in the sheet and the customization applies to anything that you use the style script with. You can save specific styles for various charts, naming the style whatever you like, and calling it for specific graphs. Sample code for this basics series: http://pythonprogramming.net/beginner-python-programming-tutorials/ Python 3 Programming tutorial Playlist: http://www.youtube.com/watch?v=oVp1vrfL_w4&feature=share&list=PLQVvvaa0QuDe8XSftW-RAxdo6OmaeL85M http://seaofbtc.com http://sentdex.com http://hkinsley.com https://twitter.com/sentdex Bitcoin donations: 1GV7srgR4NJx4vrk7avCmmVQQrqmv87ty6
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 video tutorial teaches how to use Matplotlib styles to customize plots in Python 3, similar to CSS stylesheets, and how to create and apply custom styles. It covers the basics of Matplotlib styles, how to use them, and how to customize them.

Key Takeaways
  1. Download the third-party style example from pythonprogramming.net
  2. Extract the style folder from the downloaded zip file
  3. Move the style folder to the Matplotlib library site-packages directory
  4. Import the style module in Python using `from matplotlib import style`
  5. Specify the style to use in the plot using `style.use()`
  6. Save and run the code to apply the style
  7. Comment out style.use to revert to the original style
  8. Use style.use to apply a custom style
  9. Edit the original style to create a custom style
  10. Use the custom style name to apply the custom style
💡 Matplotlib styles work similarly to CSS stylesheets, allowing for parameter specification and automatic styling of plots, making it easy to customize and automate plot styling in Python 3.

Related AI Lessons

Up next
I Asked ChatGPT to Apply to 500 Jobs (8 Interviews in 48 Hours)
Sabrina Ramonov 🍄
Watch →