transcribe

Chapter 1.5 - Plotting, Importing:Exporting Data

Nathan Kutz · 16m · transcribed 17d ago
More from Nathan Kutz Business
𝕏 Share ▶ YouTube 📥 PDF 🤖 .md

Section Insights

# 0:00

Importance of Plotting

Why is plotting important in coding?

Plotting is crucial for evaluating code as it allows you to visually check if your results are correct. It serves as a sanity check and helps identify mistakes early in the coding process.

  • Always plot your results to verify correctness.
  • Plotting helps in debugging and understanding data.
  • Visual representation of data is essential for analysis.
# 0:01

Basic Plotting Techniques

How do you perform basic plotting in Python?

Basic plotting can be done using libraries like NumPy and Matplotlib. You can create plots by defining X and Y values and using the plot function to visualize them.

  • Use NumPy for data manipulation and Matplotlib for plotting.
  • The command 'plt.plot(X, Y)' is fundamental for creating plots.
  • Understanding data structures is key to effective plotting.
# 0:04

Improving Plot Aesthetics

How can you enhance the appearance of your plots?

You can improve plots by adding colors, markers, labels, titles, and legends. This makes the plots more interpretable and visually appealing.

  • Use different colors and markers to distinguish data sets.
  • Add titles and labels for clarity.
  • Legends help in identifying different data series.
# 0:08

Subplots in Python

What are subplots and how do you create them?

Subplots allow you to display multiple plots in a single figure. You can define the layout using the 'subplots' function and specify the number of rows and columns.

  • Subplots help in comparing multiple datasets visually.
  • Use 'plt.subplots(rows, columns)' to create a grid of plots.
  • Maintain consistent subplot numbering for clarity.
# 0:10

Importing and Exporting Data

How do you import and export data in Python?

Data can be imported and exported using various methods, including saving as NumPy arrays or using pickle files for more complex data structures.

  • Use 'np.save' and 'np.load' for NumPy arrays.
  • Pickle files allow for easy serialization and deserialization of data.
  • Organizing data efficiently is crucial for data management.
# 0:12

Using Pickle Files

What are pickle files and how are they used?

Pickle files are used to serialize Python objects, allowing you to save complex data structures into a single file for easy storage and retrieval.

  • Pickle files simplify data management by consolidating multiple objects.
  • Use 'pickle.dump' to save and 'pickle.load' to retrieve data.
  • Serialization is key for preserving data integrity.
# 0:14

Saving Files in Loops

How can you save multiple files in a loop?

You can create a loop to generate and save multiple files with unique names based on the loop index, allowing for organized data storage.

  • Dynamic file naming in loops enhances data organization.
  • Use formatted strings to create unique filenames.
  • Automating file saving can streamline data processing.
# 0:15

Saving Plots

What are the methods for saving plots?

Plots can be saved in various formats such as PDF, PNG, or JPEG using the 'savefig' function in Matplotlib, allowing for easy sharing and presentation.

  • Use 'plt.savefig' to save your plots in desired formats.
  • Specify resolution and format for quality output.
  • Saving plots is essential for documentation and reporting.

Transcript

0:09 before finishing up kind of this real very introductory material we want to talk about plotting because I think it's one of the most important things you'll be doing as you write your code as you want to really see everything but also we want to be able to Import and Export data and save files and so it's pretty important that you understand understand how to be doing these things so I'm going to walk through the architectures of this we're going to start first with

0:35 plotting because essentially I would say that the most important thing you're going to do when you're evaluating code you're going to plot things and so plot everything it doesn't cost you much to plot things but normally you're going to see if you did something right or wrong immediately by plotting it and so always plot your results as you're going along as a sanity check to see if things are looking at least right or in the ballpark that doesn't guarantee they are

1:03 right because you can make small mistakes or debugging issues are in there that you have to take care of that but in general plotting will at least make sure that you get some of the big things out of the way so plotting is pretty simple you're going to bring in now from you know normally this is the kind of command lines you'd have the Imports you'd put into your python notebook numpy MP but map plot lib

1:31 pip plot bring in as plot so now plot is your PLT is the command you're going to be using to plot your a lot of your U vectors and matrices and so forth so here's some very basic plotting and just just you know plot works exactly almost like you how you might expect it to you're going to plot some function y is a function of X so let's start say at the bottom here plot

2:01 plot X comma y so this is plotting the X points versus the Y points that's all that does that's what plotting means so let's make up some data and look at some of the different architectures I'm going to make three functions here first I'm going to make a function X is going to be a range from -10 to 10 in steps of 0.1 so this is -10 9.9 9.8 all the way to 10 and I'm going to plot the sign

2:27 function so this has actually quite a few points X2 is four is is three four points-5 < tk3 pi and 4 and I'm going to plot the sign at those four points and this one here is going to be X3 is linear space so the linear space command is what I'm introducing here so linear space goes from -10 to 10 in 64 points what's really nice about linear space is that it's allowing you to decide how to chop

2:59 up an interval so if I want to go from 0 to 20 and 100 points I just say 0 comma 20 comma 100 boom that sets that up and that's what Y3 is here and I'm going to plot all three of these functions so easy enough to do and here's what they are this is what it looks like if you don't do anything with the plot couple things to point out actually what did I plot here so a Y3 is

3:24 X3 * sin X3 so that's the green curve so typically when you plot subsequentially they plots in different colors so the first plot is blue the second one is this orange and the third is the green now the orange doesn't look like a sign function but what it's doing is connect the dots it's basically saying you gave me four points and I draw a line between all four points so that's what it's actually doing there and so we can

3:53 improve on this plotting because it's misleading to have the connect the dock type framework so let's start talking about how to make nice plots so for instance here what I'm going to do I'm going to come back to the plot function but now I'm going to plot them all in one command X comma y then X2 Y2 and notice what right after this in these brackets or hash it's I have green star so what this means the G is the color

4:21 and star is going to be what I'm actually plotting it's going to plot a little asterisk in Green at these positions this one here X3 Y3 it's going to be a magenta with dots and a dotted line okay and so let's take a look at what this looks like there this is what it looks like so the reason we want to plot these is because now what we're being able to get is a little bit more sophistication in

4:48 our plot structure so the first thing we Plott is the blue this is just the sinx there's nothing we did there special but then you see the green dots there's one two and three and the fourth one is in there leave behind here oh here we go so yeah 1 2 3 4 so now I'm just only plotting the locations of the green dots and then the third one is it's plotting a cir dot on where the data is

5:15 and a dotted line between in magenta so these are the kind of things you can do to sort of make plots nicer and also more interpretable and also disambiguate a which one I plotting you can remember the colors because you actually specified them ahead of time okay so this is just some ways of helping improve your visualization capabilities you can also do things like you know making you know labels

5:46 putting in Access labels so for instance in the plotting we can put X labels here plot. x label X values y labels y values title example graph and we can even make a legend and by the way the legend how you would do this if I do the plot command XY label I put a label on each one of the data set data set one data set 2 three and then I do the legend now what it's going to do is create this

6:13 graph here now notice what I have in this graph I both have an X axis a y AIS I have a title and I have a legend okay so all of these are things that you can now modify your your graphs with to make much nicer plots so for instance if you want to make nice plots and put them up on eventually you my hope is you have your own GitHub account where you would show some of your work

6:41 you'd make nice plots so making nice plots is step one in actually being sort of a little bit more professional than just whatever the screen plots out I print that out you actually want to make a very nice graph that people look at can understand very quickly and easily okay by the way in fixing these things up there there are quite a bit of different line Styles colors and also kind of symbols we could plot at the data points

7:10 so the line styles are solid or dashed or dash dot dashed no line you can have different colors here here are the different color indications and then also here if you look at your symbols you have you know this is what you could plot on this instead of let's say the standard dot you could make you know a circle or X for where the data is so forth so lots of options for you in terms of making a much richer graph and

7:39 plot to convey the information that it's supposed to you can also position The Legend and Python's positions you can position it in the north the south east the West all these are different places you can put the legend around the box and often the legend you want to make sure to put the legend somewhere doesn't cover up critical data so this is partly why you have so many options the The Legend can be inside of

8:09 your plot box or outside of the plot box so these are things you specify and you can specify those positions as as you as you like so okay so those are some things you can do you can also make subplots and here is a way to do SUB plotting in Python you define a figure and that's right up here at the top figure subplots and I put 311 so the 311 stands for three

8:42 third three rows one column first picture and here's what I'm doing in the first picture I'm I'm plotting this here and then I go to the next subplot which is 312 now these have to be consistent I can't do 311 and then do 322 it's 31 1 312 so this is third row three rows one column second second figure this is what I'm plotting three rows First Column third figure and so when now I'm plotting

9:14 separately I have this plot instead where you can see now what I've done is I've plot them in a column format instead of putting all the same graph I'm plotting them in three separate subfigures I can make row figures I can make col column figures whatever you would like right so you can arrange these you can make multiple rows multiple columns so I could have you know three rows of figures two columns so forth all you need to do is specify

9:41 what's here and once you've done that specification then you have everything you need to make a nice figure okay so that's some Basics on plotting there will be some more lectures on Advanced plotting techniques later in the course for doing 2D 3D plots but for now just the basic plotting that you need is is right there for how you would use this now let's talk about importing and exporting of data or saving your work or saving

10:11 your files okay so so for instance here you can suppose you've done some computations and you have some Matrix that you've computed well you can save it as you know for instance an MP array so file. MP array and then like suppose the array to save suppose my I had a data Matrix a I could just save that to file MP and so I can call it back later so suppose it actually took me a long

10:39 time to make that computation you may want to save it so MP save will do that for you and you can just save your output as MP arrays for use later on and you don't have to redo the computations to load it back you just do MP load so MP save you can save MP arrays or whatever else you want and MP load just calls it backend okay so that's how you would import and output data very easily

11:05 within python you can also use more advanced structures so there are a lot of options here for how you might package data together I'm going to talk about what are called pickle files so what pickle files are is a way to they're kind of essentially or closets organizational structures where you can put all of your information or arrays or so forth in into one file that you can carry around and then you can just unpack all the

11:34 data that's in there so here the way it is import pickle and I'm going to make a data dictionary and I'm going to call array one it's going to be X1 array 2 X2 array 3 X3 and so I can serialize What's called serializing the dictionary to a pickle file so with open data. pickle so this is file I'm going to open and I'm going to dump all my data into here okay and so I take all the data into there

12:04 and in here with open I can also Des I can deserialize the dictionary from the pickle file so you want to both serialize the data or deserialize the data so either one one of them is loading the data some pickle file and the other one is creating the pickle file so if you have a ton of data that you want to keep then what you would do is you can just make it into a pickle file put it all together and then when

12:34 you want to you can just load that in later so with pickle. load right which is right here you can just basically say bring the entire file back open it up pull out the pieces that I need okay so pickle is just one way to do it so for instance if you wanted to access some of this data loaded arrays you would just say you know y1 is loaded data dictionary array one or two or three and

13:00 these are all the things that you loaded in here that you can give it names to okay and remember here I'm calling array one which was actually what was saved here was X1 and array two had X2 and array three had X3 and so I'm just loading them in as new files calling y1 Y2 Y3 okay that I got it from these pickle files so if you have a ton of data you can just put it all into a

13:25 pickle file and extract everything back out later on if you need okay you can also save files in a loop so another interesting thing here so for instance make a loop go from range one to six you maybe make some data up that you have and then you can save a file there so file name so all I'm trying to show you here is that I can save this Loop number and and then this will give me the loop value which

13:56 is 1 2 3 4 5 or six and and then I save this here and I save it to this file name so it's going to be called right loop loop number one. MP Loop Number two. MP so I can in other words save in a loop different file names according that are associated with when I was going through this Loop so this is going to be very flexible framework when you want to save lots of different data when

14:23 you're going through a loop and you have different values that you're working through and you want to save it as different file names and this is gives you a way to do that you can also save plots of course the easiest way to do it is to just take your plots and save them in the PDF format and this is exactly what this code does fig. PDF format PDF so save fig so if I've got a plot or a

14:51 figure that I have up I can just simply say plot save fig and write it out as a PDF so that's one way to do do it you can also do it in a loop like if you have multiple figures that you want to do or you want to generate a bunch of figures in a loop and save you know five figures with different parameter values this is how you would do it so and this would save here I'm saving them as

15:13 pngs with where I specify the resolution here and in fact there's lots of different ways and formats you can save it in you can save it in PDF jpeg Tiff PNG EPS depending upon what you need you would just specify this output in Python to get essentially the plots the way you want them so those are the kind of important things for you to understand is that as

15:43 you're working on code you certainly want to make output files that are plots you want to make the plots nice and you may want to keep your data in and Export it or import it for so that you don't have to always rerun your notebook to generate this data that you've already done you could just save it and then then use it later on in a file so you can either save it as a pickle file or

16:06 NP array whatever you want to do and so understanding how to use Save and plot these things is is kind of an important piece of this so again all the code that I just showed you here you can get off of the GitHub site play around with it you can start it's super easy to run these codes and play around with both plotting exporting and importing

Summary

The discussion focuses on the importance of plotting in coding, particularly in Python, as a means of visualizing data and validating results. It also covers methods for importing, exporting, and saving data, emphasizing the use of libraries like NumPy and Matplotlib for effective data management and visualization.

- Plotting is essential for evaluating code and visualizing results; it helps identify errors quickly.
- Basic plotting in Python involves using commands from libraries like NumPy and Matplotlib.
- Different plotting styles (colors, markers, line styles) enhance the clarity and interpretability of graphs.
- Adding titles, labels, and legends to plots improves communication of data insights.
- Subplots allow for multiple graphs to be displayed in a single figure for comparative analysis.
- Data can be saved and loaded using NumPy arrays and pickle files, facilitating data management.
- Saving plots in various formats (PDF, PNG, etc.) is straightforward and can be automated in loops.
- Understanding these plotting and data management techniques is crucial for effective coding practices.

Questions Answered

Why is plotting important in coding?

Plotting is crucial for evaluating code as it allows you to visually check if your results are correct. It serves as a sanity check and helps identify mistakes early in the coding process.

How do you perform basic plotting in Python?

Basic plotting can be done using libraries like NumPy and Matplotlib. You can create plots by defining X and Y values and using the plot function to visualize them.

How can you enhance the appearance of your plots?

You can improve plots by adding colors, markers, labels, titles, and legends. This makes the plots more interpretable and visually appealing.

What are subplots and how do you create them?

Subplots allow you to display multiple plots in a single figure. You can define the layout using the 'subplots' function and specify the number of rows and columns.

How do you import and export data in Python?

Data can be imported and exported using various methods, including saving as NumPy arrays or using pickle files for more complex data structures.

What are pickle files and how are they used?

Pickle files are used to serialize Python objects, allowing you to save complex data structures into a single file for easy storage and retrieval.

How can you save multiple files in a loop?

You can create a loop to generate and save multiple files with unique names based on the loop index, allowing for organized data storage.

What are the methods for saving plots?

Plots can be saved in various formats such as PDF, PNG, or JPEG using the 'savefig' function in Matplotlib, allowing for easy sharing and presentation.

© transcribe · For agents Built with care and craft by Gokul Rajaram