Matplotlib Tutorial 1 - Introduction and Line

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

Key Takeaways

Introduces Matplotlib and line graphs for data visualization with Python

Full Transcript

What is going on everyone? Welcome to a data visualization with Python and Mattplot lib tutorial series. I'm going to be redoing this series. I have an older version of this series on this channel, but it was actually one of the first series I ever did and it could stand to be updated quite a bit, especially as that we get into the more advanced aspects of using Mattplot lib. So, here we go. We're going to go ahead and update this series. is I'm not going to release this series like I normally do, uh, where I do like one video a day. I'll be releasing this series in rather large chunks at a time. And the reason for that is the series doesn't really have to be followed linearly. The first few introductory tutorials, you if you're not familiar with Mattplot Lib, you probably should follow along linearly. Uh, but then from there, you should be able to make your choice into kind of what kind of topics that you're specifically interested in. By that same token, a lot of times I release the series first and then I go back and put put the, you know, text versions of the tutorials on pythonprogramming.net. Uh, and and this time I'm planning to have everything released all together. So, you should be able to get the source code and the videos at the exact same time and then you can go to pythonprogramming.net and kind of figure out which of the main topics you're mostly interested in, what you're looking for and all that. So anyways, in this series, we're going to cover everything just, you know, from the the basics of graphing graphs with Matt Plow lib, you know, your typical scattered plots and line graphs and bar charts, stuff like that. And then we'll get into a bunch of the customization options, things like plotting date time, doing fills, having multiple plots on the same figure and sharing axes, all kinds of stuff like that. We'll talk about basemap and we'll talk about a little bit of live graphs and then we'll talk about 3D graphing and then that will pretty much round out the the series. So feel free to bounce around if you don't know the basics. Kind of stick around for the basics, but otherwise you should be able to kind of bounce around. Some stuff we'll we'll reference like we'll teach legends and then I'll show you how to customize legends and then later on we may actually customize legends based on a legends tutorial. So for that reason you might want to follow linearly but you shouldn't have to. So anyways first things first how do we get mapplot libs? So there's you know a few different ways that we can go about actually acquiring uh mapplot libs. So the first way would be via your command prompt. Okay. So cmd.exe you should be able to go there and uh let's open up that. And if you have Python 3 or a new version of Python 2, this tutorial is going to be doing Python 3 and using Python 3, but feel free to use Python 2 if you want. Just understand there's some changes you'll have to make, and I'm not really going to uh touch on those. So, first of all, the easiest way is if you have a 32-bit version of Python, which you do have if uh if you just installed like the default, it doesn't matter what your operating system is. You might have a 64-bit version of Windows unless you tried to. you have 32-bit Python. If you want to check, you can always just type in, you know, Python and read at the very top here. See, my Python is 64-bit. But if yours says maybe 32bit, then you have 32-bit. So, uh, just do quit here. So, to get mapplotib, the easiest thing to do is pip install mapplot lib and that should be it. If it says pip is not a recognized command, then you'll have to give the full path. So, on Windows, that would be CL if you have, you know, for Python and then the version. I'm using 3.4. So we would say python 34 slashscripts slashpipin install and then mapplot lib. Okay. Uh that should be good enough. Uh later on you might find that it says you don't have a module and there's like a few of them like um let's say six. Six comes up a lot. People go import mapplot lib and it says no module named six. Well you have to get six too. anytime it Python says hey you don't have this module uh you need to get it this is how you would get it same way you got mapplot lib so go pip install six okay and that would give you six so that's with the command prompt uh next we could use uh let's go up here let me pull over a new tab and you can go to mapplotib.org mapplotib.org For some reason whenever I go to mattplotive.org I get a lot of times like this website like it's like it's down all the time. I don't know what the deal is there. But anyway, you can come to mattplotlib.org. Scroll down to downloads. We'll click on the mattplotibs downloads page. And then here you get the downloads for 64-bit and 32-bit Windows. You can also get the wheels. You can install files with wheels. Uh but if you're on Windows, just get this or this. Right. Win32 would be for 32-bit. Win 64 or Win AMD 64 for uh 64-bit. There's your OSX images and uh Linux you would just install via the source. So once you have Mattplot lib, you should be able to um import Mattplot Lib without a problem. So the next thing that we're going to go ahead and do is just show a really simple line graph with Mattplot Lib. So to do that and to use mapplot lib the first thing we need to do is import the mapplot lib but uh more specifically what we use is piplot and piplot is the main kind of functionality that you're going to be using all the time you're pretty much every application I can't think of one that is not going to be using piplot so you're going to do this a lot so you're going to need this import mattplot libpipplot as PLT. We do that just to shorten it and that's a standard. So most people shorten piplot as plt. So if you're looking at other people's code, uh you can kind of you'll understand that. You won't have to usually trace that back to their imports. You'll just know plt. It's kind of like with numpy, people import numpy as np. Everyone does that. So it's a good idea to do that too so people can understand your code when you need help. So uh import mapplotib.pipplot as plt. And that's fine. The next thing that we're going to go ahead and do is um is plot. So when we go to plot, we invoke the piplot module which we say is plt. And then we say dot and plot. And when you go to plot, you plot x's and you plot y's. Now we don't really have anything for x and for y. So uh let's fill those in. Let's just create two lists. So we'll say x is 1 2 3. And then y you can pretty much say whatever you want. We could say five not 76 57 let's do four. Okay. So our graph does something interesting I guess. Okay. So we plot there and then now what happens with graphics is everything is drawn to a background first and then when everything's all ready to be shown that's when we bring forward whatever's been you know we've been working on in the background. Mattplot Lib is no different. So in the background, this graph has been drawn, but we need to bring it to the forefront. So the way that we do that is just with a simple plt.show empty parameters. And that's it. So now we can save and run this. And I suppose we wait for a moment. There we go. And there is our graph. So we'll be talking a little bit more about how this works. Uh but basically, you know, you've got this graph. It's a line. And you've got like this kind of like gooey window that you have. Uh, and then you got these buttons down here. So, we can run through those buttons real quick. First of all, you can resize the graph, no problem. You should be able to move that around a lot. Then you've got these first three buttons. We can't really do anything with them just yet. So, we'll talk about those at the end. We'll start with this little cross here. If you click that, you can now click on your graph, click and hold, and like drag the graph around a little bit. Okay. So, that's one thing. Then you've got like the zoom. So you can zoom into a specific area if you wanted. You can also zoom out. So that was with a left click and hold to draw the the uh triangle. You can do the same thing by right click and hold. And this instead will zoom out. So we're able to zoom out that way. Um then you've got configure subplots. Right now this doesn't really mean much to us, but we can move a few things. Like you'll notice there's a lot of empty space on the left, the right, the bottom, and the top. we can kind of we can uh use this to adjust that space and later on we'll use it because you can actually program program in these values to change them. Uh so we'll talk about that when uh that comes up. Then you have this save the figure. This is just so you can actually save the image or even a vector of the graph and a bunch of other options too. Then we go back to uh these buttons. So first you've got this little home that'll return you to the original view. So you could click that and now you're at the original view. Everything's back to normal. You can use the back to previous view to go back one. You can keep hitting this and keep going back. Conversely, you can go forward, right, and go back to the original views before that. So those are the basics of mapplot lib. Hopefully you guys are up to this point. If you have any questions or comments, please feel free to leave them below. Otherwise, as always, thanks for watching. Thanks for all the support and subscriptions and until the next

Original Description

Welcome to a Matplotlib with Python 3+ tutorial series. In this series, we're going to be covering most aspects to the Matplotlib data visualization module. Matplotlib is capable of creating most kinds of charts, like line graphs, scatter plots, bar charts, pie charts, stack plots, 3D graphs, and geographic map graphs. First, in order to actually use Matplotlib, we're going to need it! If you have a later version of Python installed, you should be able to open cmd.exe/terminal and then run: pip install matplotlib Note: You may need to do C:/Python34/Scripts/pip install matplotlib if the above shorter version doesn't work. 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

Related AI Lessons

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