Python 3 Programming Tutorial - Matplotlib Styles
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
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
52
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 Productivity Tools
View skill →
🎓
Tutor Explanation
DeepCamp AI