So a great job, if you made it this far, you have learned a lot about Python and Nampai already.
So the last thing that I want to present to you in this crash course is how you can visualize your data
and your results, because of course, we will also create figures, for example, of our results.
So we call this plotting or creating plots and to do this in Python.
It's very useful to import a library called Months Plot Clip.
So it's basically a mathematical plot library.
And then there's some some option here, which is called PI plot and you write import.
You could read just in parts Matlock lip.
And then every time you want to plot something, you write, plot live dot pi, plot dots and then the
actual command.
And since this is pretty long, we use an abbreviation, so we write import much plot loop PI plot as
Pulte.
So in the following, we only have to write Pulte, which is pretty similar as we did for nonpaying
when we wrote import numpy as NP.
So in most of our files on the following, we will basically run this command in the first cell and
in one of the first lines.
But here, just to make it a bit more or have a better overview of the notebook, I just keep it here.
So we run this.
And now we have loaded the library and we can use it and we can plot nice things.
So the most simple thing you can do is just plot some data points with some coordinates.
So you have some coordinates basically which have an X value and the Y value, and you have several
of these.
So we can just write plot dot scatter.
This is the command for such a list, plot or scatterplot.
And then you provide the two lists, and now it's important you do not provide x one by one.
This is not true.
You provide instead x one x two x three and then you provide a second list with the Y coordinates.
So it's really important to acknowledge here the order of the two things.
So first, all the X coordinates and then all the way coordinates, so we can just do an example here
one, two, three and then maybe four or five.
And for the Y coordinates, we just take some values five one minus one zero two.
So now I made a mistake, so let me check what I did wrong here.
I don't really see it just yet.
It says X and Y must be the same size.
Maybe I did a comma.
Yes, here I made a mistake with the comma.
So maybe it's a good idea that you saw this here.
So just a small mistake here.
I guess this large error message and and most of the cases, it's a good idea to scroll to the bottom
because there it gives you the the error message.
So now they have to stay, have the same size so I can run it.
And you see, we have now our coordinates.
The first point has to coordinate one and five, the second one, two and one and so on.
So this is how you create a scatterplot.
So now this is typically not how we get our data.
So let me show you a bit of a more realistic example.
So let's say, for example, we have some array which we call, I don't know, courts small.
And as I said, it's an array.
And typically at least this is how I program typically or most of the times is I have a first data point,
which has some coordinates.
And let's assume we even have three coordinates X, Y and Z.
So the first point has the coordinates one one one.
Then we have a second point, which has some other coordinates two for eight.
So the next one, you see just a square and cubic numbers.
Of course, I could also generate this list automatically.
But just for the just for the presentation here, let's just write them down.
OK, so now we have five different data points, which all have three coordinates.
So in such a scatterplot which is two dimensional, we can of course only use two of these coordinates.
We can, for example, plots the X and Y coordinates of all of these five points.
So how are we going to do this?
Well, we are, as I said, you really have to be careful here with the syntax because if I would just
right here plot scatter and then quotes one, it would of course give me an error because yeah, the
inputs has a completely wrong shape.
So first, we must shape our data and we do this in the following way.
First of all, he told you that you have to first provide the X coordinates and then the Y coordinates.
So what we're going to do is we are going to transpose the array.
So this basically just means reorder the array and now the first cell or the first brackets will contain
this value.
This this is this.
So basically all the X coordinates and you do this by writing down quotes one dot capital t.
So this Ensino has reordered this array.
So we started from here.
Now we are here and now what we can do is we can assign this array and individually these three rows
here, we can assign them to new variables.
So I call them X1 y one z one.
And now, if I called them just to show you next, one will just be this line here.
Why one will be the second line and so on.
And so this is not really nice, I think, because what we can do next is we are basically we don't
have to do anything anymore because we already have now the correct shape.
We can just use them.
So what we can do now is we can write, plot, scatter and just write x one comma y one.
And that's basically all we have to do, because now you see, this has the same syntax as this one
here.
So I can just run it and you see we get now the plot.
So on the x axis, we have this line here and on the y axis we have this one.
So the values are one, four, nine, 16 and twenty five.
So what's also nice is we can just plot in the same plot.
The Z components as well.
So now we have plotted the X components on the x axis and the Z component at the y axis.
So you see, they are different No.
One, eight, 27, 64 125.
So maybe to just make this a bit clearer and to understand it a bit better, we should add labels to
our plots and we can do this by writing before we do this here.
Plot X label.
X so now we have the X on the x axis, and to make the Y label, we write Peltier Dot Y label.
And here we I would say we just write y commas because we plot both of them.
The Y coordinate is blue and the Z coordinate is orange.
OK, so now we have a nice scatterplot, and I'll show you how you can handle such data points that
we will typically get from our calculations.
Now, of course, there are not only these scatter plots.
You also want to plot conventional functions and you want to connect them with a line.
And the truth is, most of the times you do not straightforwardly plot a function as a line.
Instead, what you do, you generate some data points which are very dense, you could say, and then
you connect these with lines and so you emulate the real function.
So let me show you what I mean by this.
So what we're going to do here is we are going to first generate some data points.
So I just write your data points.
And then what we're going to do is we first are going to generate some X coordinates.
And for this, we should do something like zero one two three four five like we did here.
So this would be our data points.
However, since we want to have a smooth function, we must make them very, very dense.
So we should do instead something like zero, 0.1 0.2.
And so.
So this is, of course, very, very tedious, and you need many of these points to generate a smooth
function.
So to generate such a list, what you can do is you can just write and p dot little space and then you
provide starting value, then the end value and the number of points between these two values.
So you could, for example, write one hundred and now I can show you what this looks like.
This is an array with 100 data points between zero and four.
So it's often better to add you add additional data points because then we will have such nice numbers
here.
This is because the first and the last data point to count as well.
So when we write here 101, we actually have a distance here of zero point zero four.
But it wouldn't be a problem to write 100 here.
OK, so now we have this list, and what we're going to do next is we are going to generate a loyalists
our actual values.
So let's say we want to plot the function just a random function one plus two times cosine, which would
be an PIMCO's.
And then I would just say three X. So this would be our function that we're going to plots.
And of course, the Y values must also be a list.
So the X values must also be a list, and we just take our list that we have just generated.
So I run this.
And now you see the Y values are both also a list or array betters, say, and we can plot.
Now both of these lists together, as we did before, so we can, of course, now just load this one
here.
The scatterplot?
And this will be the function, and if you now want to connect these very dense points, you can write.
Plots are Pulte Dot plots.
This is the correct comment.
And now you see it gets a rather smooth line here.
But for example, if I would decrease the number of data points to 21, then you would see these are
really just lines connecting the data points, which make a not so smooth function.
And of course, the higher the number goes, the smoother the function will look.
So I see I would say 101 was just fine.
And yeah, we can definitely work with this.
So what else can we do here?
Well, we can, for example, specify again the labels which we have done previously.
But I want to show you something new.
I want to show you the limits on the XS.
For example, the x axis.
And so the syntax here is x limb than normal brackets and in the cupboard or in the yeah, in the in
these brackets, we write zero comma eight, for example.
And so we have now changed the X range from zero to eight.
And what I'm going to do next is I'm going to add some more data points and I want to plot.
So let's say I want to create more data.
So I just copy this one here and say X to Y to X two.
And this time I want to plot the values of four x between four and eight to fill the remaining space
here.
So I write for Comma eight and I want to just continue with the same function.
So now I will add another plot here.
I will add the plots of X two and Y two.
And of course, if I do it like this, you see, we have now merged these two plots.
But you can also merge different types of plots.
For example, you could write plot dot scatter.
And now you would get such a graph.
All right, so you see, it's really nice to are really easy to plot stuff, you just have to remember
that you cannot just provide a function that you plot, but instead you must always generate lists or
arrays that you then plot.
So you have really the full control over the way you plot things.
So the very last thing that I want to show you is that you can also export figures and save them, and
this is pretty easy.
Interest rate potti dot safe, fake for, say, figure.
And then you just have to provide a name to write, test plot dot.
And then, you know, you could, for example, use a page file.
You run this and it gives you the output because we are in Jupyter notebook.
If you would be in a Python notebook, it would not give you the output here, but you would then export
the file and you can look it up in your directory where this notebook here is located.
And you see, I have opened here the image that we have just exported.