Lab 02 - Jay1337/computational_physics GitHub Wiki

Goal: Our goal in this lab was to introduce us to basic plotting and visualization functions. In this lab we were introduced to a variety of different visualization techniques and functions, like plotting, polar plots, histograms, and 3D plots.

Overview: In this lab we used matplotlib and numpy which were essential, as in order to plot anything onto a graph these are needed. This lab was broken up into different parts where we learned about how a modulo(%) function works, and read data of a file then plotted said data as well as well as creating visualization plots for it. The visualization plots created included plots such as a polar plot, displaying an image, creating a line of best fit with data, histogram of data, a multipanel plot, and a 3D plot. In the first section we learned modulo(%) returns the remainder of the division problem. We then created a function to indicate when a number is even and when it's odd, as well as making sure it would justify that when we type letters it will tell us to enter a number. image image

In the next section we were given data in a file and then plotted said data. For this we had to download the data in which we used wget, and then we needed code that would read our data, so we used np.loadtxt. This code takes it in and then assigns a variable for the data. The data has two columns so we set the first one to be the temperature and the second column to be the magnitude in the code that will be shown. We then created a plot with certain x and y ranges on the graph, inverted x-axis, and y-axis, as well as created labels for specific points of the graph. We added alpha into our main plt.plot to control how clearly you can see the stars more individually than when running it, where we can adjust the alpha number from 0-1. After creating this graph, we created another one using the same features, but instead we did it using the scatter plot method. This is done by plt.scatter(x,y,c=color) We did our color by the temperature of the star. image image image image

For our next section we were given an example of how to create a polar plot that we needed to fill in, where we created an array of radial values and angles. For the next example, we were given half the code to create a polar plot of the sine and cosine of theta with some of the code being there. For this we made r be sintheta and r2 be cosinetheta having the plot be in polar it needs to have subplot_kw = {'projection':'polar'} which creates the graph to be polar. The next section was to learn how to display an image using matplotlib. Using !wegt to download the file, which is a Linux command, and then using image.imread(image name) to display the image. This is also able to be done with Google, where we have to import a module for Google colab, which is from google.colab import files then you have to upload = files.upload() so you can choose a file or import a downloaded image from Google. To show any image we use plt.imshow(); to print it which creates a variable(X) for your image.imread(file name) to make it easier to print. image image image image image image image

For our next two sections in the lab we had to fill in some parts of the code to create a line of best fit and a histogram. In the line of best fit, one of our most important lines in the code is np.polyfit; which creates the line of best fit itself. The histogram code was also given where we had to fill in some parts and we got introduced to np.random.normal(), which generates a random normal distribution in our code. For this code we are generating 1500 and creating it into a histogram and in order to do so we must use plt.hist(). For the histogram, we added visual lines so that we could see the average of the numbers and two other dotted lines of the average + standard deviation and average - standard deviation. image image image image image

The last three sections were here to teach us how to create different kinds of plots, such as Multipanel plotting and 3D plotting, and then any plot of our interest. With creating a Multipanel plot we use plt.subplot(nrow,ncol,nplot), which displaces where the graph will be located when looking at it. This means that there could be a total of two rows and two columns(so 2x2), which it can only contain four graphs and you can set up the plt.plot how you would set up every other. For creating a 3D plot we used plt.axes(projection ='3d') where the plt.axes is used to try and place its axes so when putting the projection 3d our graph create will have its axes in 3d format. image image image image image