Professional 3D Plotting in Matplotlib

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] what is going on guys welcome back in today's we're going to talk about three-dimensional plotting in matplotlib which means we're going to make visualizations in three dimensions rather than only two dimensions and i have made a video on this already on my channel it's quite old it's part of the data science tutorial series uh it's low quality so i'm going to make a new video here with more information with better quality and better explanations so let's get right into it we're going to start by importing matplotlib dot pi plot s plt and we're going to also import numpy as np now we're going to use numpy mainly for the generation of data we're not going to plot actual data we're going to generate data using numpy and we're going to use obviously matplotlib for the plotting now one thing that you might want to or might have to import i'm not sure sometimes i needed it sometimes i don't need it now in the last try i did need it so i don't know if it's actually necessary but we're going to import from mpl underscore toolkits we're going to import mplot3d now again the syntax highlighting is going to show that we're actually not using it and sometimes if i remove it it works sometimes it says okay i don't know the projection 3d and it doesn't work so for some reason i'm not sure if we need that or not but if you want to make sure that it works then you want to import this as well it's part of matplotlid and of course as always if you don't have matplotlib installed then you're going to say pip install matplotlib and numpy like that all right so those are the imports and the first thing we're going to do is we're going to talk about plotting single three-dimensional points in order to do that obviously we just have to specify coordinates but before we can actually start plotting in three dimensions we need to create an axis and this axis has to have a 3d projection now we have different projections we don't have only uh 3d there is in the map of the documentation i think a large list of different projections that we can use but if you want to create a new axis you basically say ax or whatever you want equals plt.axis and then the projection in this case is going to be 3d now the projection is basically the type of coordinate system you have the basic default which is the two dimensions we have three-dimensional and we have certain specific kinds of plots for different scientific purposes or anything like that uh however we are going to pick 3d and then we're going to just say ax dot scatter so the basic scatter function and this time we provide three coordinates not just two so we have x y and z let's say three five seven for example and then plt show we're going to click on run and we're going to hopefully see a three-dimensional plot now i'm not going to rotate too much here because when i'm recording and i'm using math a little bit at the same time it's usually very laggy but you can also move this around here as you can see it's lagging i hope my recording is not crashing on me here uh but basically we can turn this around we can look at it from different perspectives you can see the coordinates here and this is the point that we actually plotted so i'm not gonna rotate too much throughout this video but that's how you do it you just use the mouse um and that's how you plot a single point now if you wanna make a scatter plot based on multiple arrays of individual coordinates we're going to just say scatter plots here we are going to create a new axis here as well or actually we don't have to do all this in a row let's just replace this with uh with new code we're going to again do plt axes projection [Music] 3d and then we're going to say x data equals npa range and a range is basically you choose a starting point you choose an end point and then the step size it's not like lin space and length space you choose a starting point an end point and then how many values you want to generate in between here we specify the step size so this would mean uh start from zero and with a step size of 0.1 increase until you get to 50. we're going to do that for x we're going to then uh also generate y data the same way uh or actually let's do it in a different way let's say x data equals np dot because we don't want to have a structure we want to have some random stuff here so we're going to say np random dot rand int from 0 to 100 we want to have the shape 500 uh like that so basically one dimensional uh and then we're going to do the same thing for the y data for the z data and yeah that's basically just 100 uh or just 500 random points and for this we're just going to say ax dot scatter x data y data z data like that and of course plt dot show so that should look quite messy now there you go again we can rotate but it's going to be laggy but here you can see the individual data points so this is a very basic scatter plot and of course we can do the same thing as always now i'm not sure what the individual parameters were was it marker or was it m i'm not sure let's just try with with marker but i think it was m or something no there you go it was actually marker so you can change the marker you can change the color map you can change the alpha value so we can say alpha equals 0.1 i mean i think this should work here there you go then we have more transparency here uh so you can customize it like an ordinary 2d plot but it's in three dimension uh three dimensions now now let's say we don't want to plot points we want to plot let's say a function so we have some input values x and y and we want to have a function on the z coordinate if you want to do that of course let's just not delete all of this let's just delete this we're going to now say x data equals np a range from 0 to 100 shape 500 this makes more sense here because we want to have an uh oh sorry i'm stupid a range 0 50 and 0.1 this was what i wanted to do in this case it makes more sense because of course we want to have equally uh sized ticks on the x and y coordinates and then the z coordinates should be the result of whatever calculation we choose to make so we can say z data is basically just x data times y data a very simple function and if we do that we can say uh p or actually axis dot um was it scatter actually we should probably go with plot but let's see with scatter it works as well but it's more like a collection of points on a line but i see that in my prepared code for some reason i used scatter i think this is a mistake i think plot is actually the better function here you can see here that we have a function but it's actually consisting of um can i zoom in here come on however you can see it's individual points if you look closely you can see it's not actually aligned so i think this should be easy to fix by just using plot but i'm not sure since this is not the code i prepared here it's a little bit yeah there you go obviously it works better if you choose plot because then it's actually plotting a line um so this of course is also not true here so this is basically plotting a function now you can choose whatever function you want you can of course also say np dot sine of x data times np dot cosine of y data for example and then you'll get a different function obviously there you go in three dimensions uh but that's how you do a basic function or how you plot a basic function along an axis now one thing that i also want to do here before we get to the next uh topic which are surface plots i'm gonna talk a little bit about customization because of course we always wanna have some titles and labels and all that and that is not too different from what we usually do you basically plot something and then you say ax dot set underscore title and then we can say okay i don't know uh funny function could be the title and then i can say ax dot set underscore x label and this is going to be my x values in centimeters for example then we can go with my y label or my y values in i don't know vault and then we can say my fancy results in in this case i don't know centimeters times vault doesn't really make sense however we're going to do it like that just so we have something and then we can show that and you will see that we have some labels here as well all right now let us get to more interesting stuff and talk about surface plots this means that we're not just going to plot individual lines in three dimensions but we're going to actually plot the whole surface of the function because up until now what we did is let's say those are the x values one two three four five six seven and so on and then one two three four five six seven and so on are also the y values what we did up until now is let's say the function was z equals x times y what we did actually was not computing the whole function for a surface plot we just took the individual positions and combined them so 1 times 1 is 1 2 times 2 is 4 3 times 3 is 9 then 16 25 36 49 and so on this is basically what we did it was just taking the individual positions and combining them together however in order to plot the full function as a surface we need to combine all the values of x with all the values of y so we have to take 1 times 1 1 times 2 1 times 3 and so on until we have all the values and then we have one row of the z coordinates and this is what we're going to do now we're going to create a so-called mesh grid we're going to use a meshgrid function to create a grid of the value so that we have basically for each element in y we have a row with all the elements in x so that we can then combine them in z together so that we have this final grid of the results and we can use this grid to plot a surface plot in the end i hope this was somewhat understandable basically we would have something like that we would have one two three four five six and then one two three or actually let's just copy that uh that would be for example the mesh grid of x and then we would have if the values are the same obviously we would have the same thing for y and then when we combine them we would have the individual results in a uh six by six matrix or six by six array as the z values again and then we could plot a surface plot so for this we're going to start again with ax equals plt axis projection projection equals 3d and then we're going to say x data equals npa range 0 250 with a step size 0.1 y data is the same and now these are just one row of the values so we have the values from 0 to 50 with the step size but we just have them one time we got to take this and uh duplicate the rows if you want as many times as there are elements in y data and then we do the same for y as well with the amount of elements in x data um so what we're going to do is we're going to say capital x capital y equals np dot mesh grid and we're going to pass x data and y data there you go and we can then print x for example to see what x looks like so to get a basic idea of what we're actually doing here and there you go you can see that we have one row of the values and we have multiple rows of the same values and y is looking is going to look the exact same way and now what we're going to do is we're going to define the z function by just saying x times y of course capital x and capital y and then z of course is also such a grid but it's going to look a little bit different you go so z is basically also such a grid with the individual results in order to plot this grid as a surface plot now what we're going to do is we're going to say ax dot plot underscore surface and we're going to plot x y and z like that and then plt dot show there you go and this is what we end up with again i hope this is not going to lack too much let me just see if i'm crashing my recording uh but this is as you can see the basic surface i'm going to rotate it a little bit so that you get an idea of how it works um yeah that's basically it the surface plot of x times y if x and y are the same values this is a very simple plot let's go ahead and do something more fancy let's just change this to np dot sine of x plus uh np or actually times np dot cosine of y let me just see what range i chose here because if we choose that range it's going to be extremely messy so i'm going to change this here to negative 5 up until 5 and negative 5 up until 5 here as well with the same step size and then we can run this hopefully and see a somewhat interesting plot there you go so it's all blue as you can see but we have these little bumps here uh we can also rotate this a little bit there you go you can see these waves that are of course produced by the sine and cosine function and what we can now do as well this is very interesting in my opinion is we can add a color map to it which basically means that the higher the values um or when the height changes the color changes as well and it changes based on the provided color map so we can say cmap equals and then plasma uh now you can choose a different column map you can go to the map of the documentation and look at the color maps but i'm going to just choose plasma for now and if we run this you're going to see that this looks actually kind of sick so you can see that the higher the values the more yellow they are and the lower the values the more purple they are in the middle they're orange pink red whatever but this is actually quite a cool plot in my opinion a basic surface plot now the last thing i want to show you is how we can rotate the plot uh programmatically so not just with a mouse but how we can actually set the values by default so maybe when we run the script we don't want to see the plot from this perspective here but we want to see it from a different perspective again please don't crash there you go so maybe you want to see it from that perspective and what's important here is uh the two values that you see here on the upper right when we rotate so when i drag this here you can see we have this azimuth and we have the elevation um this is basic those are basically the two values that determine the position that we look at this from so let's just say we want to look from added top down like that this would be azimuth of negative zero or basically zero and elevation of roughly 90 degrees so let's note that we had the azimuth of zero and the elevation of 90. and we can choose these two values now manually in order to uh to initialize the view so we can say ax oh sorry ax dot view underscore init and here we can say asm equals zero and lf equals 90. can then remove this and basically we don't change anything else we just run this and you're going to see that the default perspective is going to be top down alright so that's it for today's video if you enjoyed hope you learned something if so let me know by hitting the like button leaving a comment in the comment section down below and of course don't forget to subscribe to this channel and hit the notification bell to not miss a single future video for free other than that thank you much for watching see you next video and bye [Music] you
Info
Channel: NeuralNine
Views: 2,732
Rating: 4.9633026 out of 5
Keywords: matplotlib, python, plotting, 3d plots, 3d plotting, three dimensional, 3 dimensional, 3d plots matplotlib, python 3d plots, 3d graph, 3d graphs, visualization, 3d, three dimensions, data science
Id: fAztJg9oi7s
Channel Id: undefined
Length: 17min 23sec (1043 seconds)
Published: Tue Oct 05 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.