Matplotlib Python Tutorial Part 1: Basics and your first Graph!

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

Key Takeaways

Installs and uses Matplotlib for creating basic graphs in Python

Full Transcript

What is up everybody? Welcome to the first tutorial in the Matt plot lib tutorial series. Today I'm going to show you guys just the most basic of just getting some plots and uh graphing it. Um if you were like me originally all I wanted to do and it was just I have some some stuff. I just want to plot it on my computer. I I uh didn't really care about anything too fancy. I just wanted to plot some plots. And um if you're like me, you're going to do that. And then later on, you're going to long to uh make stuff colorful and have some um dynamic stuff to it. Maybe even get uh live updates, you know, so the graph itself refreshes automatically. You don't have to close and reopen and reload. Um so anyway, what I'm going to do is just start from the very very basics and move along. Um, right away what you're going to want to do is you'll have to import mattplot lib. Um, right now all we're doing is using piplot and we want to import that as plt. You don't have to do this. Uh, you could import it as whatever you wanted xxy if you wanted. But generally if you look anywhere for um documents or examples, this is how people are going to denote mattplot. Um, and the other thing I suggest you get, I'm not going to show you guys at the moment, but uh, numpy. Uh, you also import uh, numpy as np. However, again, we're not really going to be using that in this tutorial, so I'm just going to comment it out. Um, so, and how do you get mapplotib if you don't have it? Well, this was probably one of the easiest uh, imports to get. You just go to mattplotlib.org. Let me get back to the home for you. Um, and then you click on downloads and then you come down here to whatever version you have. If you listen to me and one of my others or listen to just about anybody else, you probably have uh Python 2.7 and um I personally have Win32. You might have AMD 64. You'll have to figure that out on your own. Or you could even have one of these. You could also download the tarball. Um, but if you The easiest thing is just to use the .exe. It works well. Although I will uh just mention that if you do have the um problem uh like if it says you you don't have Python installed uh the way around that again is to go into system control panel and you want to edit your environmental variables. So you go to control panel I'm sorry system advanced settings or something like that environment variables and then you need to add the uh Python directory to your path. just have I think it's a semicolon then the then that directory. So anyways back to this um so we've imported map plot lib pipplot as plt. So all you have to do it's very very simple all you have to do is you want to call the uh mapplot lib.plot pie plot with this. And then you want to tell it we want to plot. And so we're going to plot um our x will be 1 2 3 and four. And then we'll plot y's the corresponding y um 4 7 9 12. Okay? And you always always always have to have an equal number of variables on either side. I mean that sounds uh common sense but later on it can be kind of hard like if you're matching up two x's and because like later on usually what ends up happening is you have some sort of process that identifies x. So later on you'd have a variable just called x and you'll have x here and you'll have y here and that and these will correspond to maybe a huge list or a huge array and sometimes the arrays don't match up in length and so that'll be a problem later on. you almost certainly run into it at least once and uh that'll be your problem. And then so once you've told it to plot that, only other thing you have to do is plot show and then you run this and you'll get this popup here. And here we go. We've got all four plots here. This is the first, second, third, and fourth. Now you can do all kinds of fun fancy stuff with mattplot lib. Uh once you have more and more data, you you can use some of this stuff. But uh this is the zoom function. You just click on it, you'll see this little arrow and then you can zoom into plots. Now, we like I said, we don't really have too many, but you you still can um if you accidentally like you got here where you wanted to be and then you're like, "Ah, shoot. I just misclicked or whatever." You can use the back function. If you misclicked the back function, you're like, "Shoot," you could go back forward again. It's kind of like a browser. And then if you just happen to get so lost and you're just like, "Oh gosh, now I got to reload it." No, you don't. You can just hit home and it takes you back to the original image. Um then you can also use this and it's kind of like a click and drag, you know, you can move this around um up and down all that. Um but again, and again, you know, if you do that, you're like, "Ah, shoot. I don't want that." You can do that. And finally, this little button here. Oh, actually, no. Finally, we still have the save, but the save is pretty pretty simple. You just save the picture. You can save it as anything you want. You just type it in, and it's called a tagged image file format, but you can also just take a screenshot using the print screen. I've never really used this, but I think you can save like dynamics of the picture or of the graph if you uh wanted to. Uh what was Oh, this. Okay, this is the the only useful part of this to me. It brings up this window. Oh, shoot. I'm back. I hate Windows shake. I I don't know anybody else who has Windows 7, but uh this little like shake option, like I'll show you. You shake a window and everything goes away. When I first got this, I didn't know that you could reshake it again and everything would come back and I'd have like maybe like 30 windows open. I'm like, "Oh my gosh, I'm going have to like go open them and stuff." And then one of my buddies that actually has a Mac uh told me to just shake it again. And I was just thoroughly embarrassed. Anyway, um okay. So, what this does is you can kind of um or like reshape this stuff. So this is the left and it is usually corresponding to these uh the actual chart itself. So as you can see there's the chart and you've got like this like background space and then you've actually got the frame or the window right around here and so you can say like okay left here I want this to be like that and then from the bottom you can do this kind of stuff and uh yeah I mean you get the point. Um, I'm not sure what wspace is to be honest. I can't remember what this is. I think it's has something to do with uh if there's multiple like if there's another chart here. I want to say um it's the space between two charts cuz sometimes like if you have too many charts uh the numbers can overlap each other and you still will override it by squishing it like this. But if you have a lot of different charts um that's what that H space and wpace is. And actually, I can probably bring up one of my charts and show you an example. One moment. Okay. Now, bear in mind these charts are um well, first of all, you'll notice they're colorcoded and stuff. And these are also live updating charts. I believe it's on like a 15 or 30 second timer. So, don't pay any attention to that if they start changing color on us. But, I just want to show you the uh head space and I like width space or I'm not quite positive what they stand for, but I like to think of them as head space and uh I don't know, wide space. So, as you can see, since like this chart is actually made for like a my full screen monitor. So, they're normally not overlapping, but now they are. And so, what you can do is change this. Um, like we'll of course now I'm not being able to edit this stuff. Why won't you let me edit you? Okay, here we go. So, you can say, okay, I don't want any. And so, now they're allowed to just overlap each other. You can do the same thing with this. And now they're allowed to just overlap each other. Um, so if you were running out of space or whatnot, but generally you want to be able to read your charts. So you'd want to make it maybe bigger so we can really space them out. Anyway, um, so really the purpose for head space and whites space um, or width space or whatever, if you know what the spa, what the letters actually stand for, I just think of this as up and down, side to side. Um, what this will be useful for later is you can actually co hard code this stuff right in. So, um, like on that chart I have there, it's actually hardcoded in. But again, no matter how hard you code it, if you squish the window up enough, it'll still uh disobey. Um, but what you can do is just code it in. So, what you can do is use this window and kind of mess with these um parameters till you see something you kind of like and then take these numbers and you import uh that into your script. So, those are the basics to um putting up a chart. Now, the next thing um that you absolutely must have on every chart if you've been in grade school or anything and you should know and taken a math class that every uh graph has to have what a title no matter what otherwise the teacher, you know, took off a bunch of points from you. And so we'll just say mattplot liv example title. Okay. And then what else did you have to do? You had to label your x and y axis. So pretty simple. Uh x label. And we'll call this map plot x label y. Now you have to do your y label. do net plot y label. And now we'll save it and run it. And as you can see, we have mattplot example title, y label, x label. Later on, you can do all kinds of stuff with this. You can affect the size, the way it angles, um you can affect the way the numbers angle. Like if you have dates down here, a lot of times the dates will run over each other, so you can get them to be kind of crooked and sidewaysish and so they fit. Um, we can change the color of the lawn. You can do all kinds of stuff. So, I'm going to show you guys a few other things in some later tutorials. Although coloring is definitely not going to be the next one, I don't think. Um, the next most useful thing is not uh hard coding your your data cuz right now we're doing this. And so most people are going to either have a notepad file or a an Excel file, which really they're interchangeable. You could call an Excel a notepad or save a notepad as an Excel. As long as it's comma separated, it doesn't really matter. So, um I'll show you guys how to load that stuff in and continue plotting in the next tutorial. Thanks for watching. Don't forget to subscribe and uh stay tuned for the next video.

Original Description

Sample code: http://pythonprogramming.net/matplotlib-basics-first-graph/ Introduction of downloading matplotlib, the basic functions, and charting your first graph! To start, you will obviously need Matplotlib, as well as Python. This tutorial series uses Python 2.7. Almost everything can be ported to Python 3+ so long as you know the basic syntax differences. Matplotlib is one of the most popular modules for creating graphs and charts in Python. It is simple to use, yet has a wide range of abilities for data visualization. Sentdex.com Facebook.com/sentdex Twitter.com/sentdex
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from sentdex · sentdex · 1 of 60

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

Related AI Lessons

Up next
How to Open HPL Files (HP-GL Plotter)
File Extension Geeks
Watch →