Lab 2 - FouadM0426/comp.physics-spring-2025 GitHub Wiki
Goal
The Goal of this lab is to use python to allows us to visualize different forms of data sets these examples are histograms, scatter, polar plots, displaying images,multipanel plots and 3d plots.
using this image as a starting foundation we can manipulate many factors of a graph of python, in this case we will be changing the color based on the temp of the dwarfs.
# plot again using the scatter plot command,
plt.scatter(temp,mag,c=temp,cmap='inferno')
#
plt.xlim(0,13000)
plt.ylim(-5,20)
ax = plt.gca()
ax.invert_xaxis()
ax.invert_yaxis()
plt.xlabel('Temperature (K)')
plt.ylabel('Magnitude')
plt.title('mangitude vs temperature')
plt.text(10000,10,'white dwarfs')
plt.text(5000,0,'giants')
# and color code by the temperature of the star
# note that c=mycolor is how you add color to the scatter plot.
As you can see in the second image we used the color map of inferno which changed the color based off the domain of the function, but also by using the plt.text function we can also put text if we are given the coordinates to input said text data.
when using polor plots the major change in comparison to a regular graph due to we just dont use a simple plt function compared to the graph above
# create an array of radial values, r, that range from 0 to 2 with 200 steps
r = np.linspace(0,2,200)
# create an array of angles, theta, that is 2*np.pi*r
theta = 2*np.pi*r
# the main plotting commands are in place below
fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
ax.plot(theta, r)
ax.set_rmax(2)
ax.set_rticks([0.5, 1, 1.5, 2]) # Less radial ticks
ax.set_rlabel_position(-22.5) # Move radial labels away from plotted line
ax.grid(True)
ax.set_title("A line plot on a polar axis", va='bottom')
plt.show()
we can also plot into the same polar coordinate, in such a manner that they even overlap, such as the following example I will show
# make another polar plot showing sine and cosine of theta
# let theta range from zero to 2 pi, with 200 steps
theta = np.linspace(0,2*np.pi,200)
r = np.sin(theta)
fig, ax = plt.subplots(subplot_kw={'projection': 'polar'},figsize=(6,6))
ax.plot(theta, r)
# enter code to plot cos(theta)
theta = np.linspace(0,2*np.pi,200)
r = np.cos(theta)
ax.plot(theta, r)
ax.set_rticks([0.25, .5, .75,1]) # Less radial ticks
ax.grid(True)
We can now use pythons display feature to import photos into the line of code, the issue with this is that the exact name of the file and type of file must match the following line of code or else an error will occur in this example we will import a spongebob picture
from google.colab import files
uploaded = files.upload()
# code to plot your second image here
from google.colab import files
uploaded = files.upload()
myimage = image.imread("spongebob.jpg")
plt.imshow(myimage)
This will allow the following picture that will be ran into our python script to display the said image we needed, this can also be used in a similar function to import data sheets themselves.
we can use histograms in python as well to display data in a different manner for comparison reasons or for a better representation of a bell curve.
plt.figure(figsize=(8,6))
gauss_values = np.random.normal(size=200)
plt.hist(gauss_values)
print("average value = {:.2f}".format(np.mean(gauss_values)))
print("the STD = {:.2f}".format(np.std(gauss_values)))
# add code to plot a solid vertical line at the average
plt.axvline(np.mean(gauss_values),color='r')
# you can pick the color, but it should be different from the histogram
# use plt.axvline()
#plt.axvline(np.mean(gauss_values) + np.std(gauss_values),color='g')
#plt.axvline(np.mean(gauss_values) - np.std(gauss_values),color='b')
# add code to plot dotted vertical lines at
# you can pick the color, but it should be different from the histogram
# average + std
# average - std
plt.axvline(np.mean(gauss_values) + np.std(gauss_values),color='g',linestyle='--')
plt.axvline(np.mean(gauss_values) - np.std(gauss_values),color='b',linestyle='--')
# once your figure is set, try increasing the sample size
# what happens to the mean and std as the sample size increases? The standard deviation begins to shift towards the right when we increase the sample size of the data values
these can all be used will using a mulitpanel plot which allows us to plot 4 different graphs at one time, which allows for us to compare graphs without having to run them separately. Using this function still requires the subplot function which was shown in lab 1
# Your code here
plt.figure(figsize=(10,10))
plt.subplot(2,2,1)
theta = np.linspace(0,2*np.pi,100)
plt.plot(theta,np.sin(theta))
plt.subplot(2,2,2)
plt.plot(theta,np.cos(theta))
plt.subplot(2,2,3)
plt.plot(theta,np.tan(theta))
plt.subplot(2,2,4)
plt.plot(theta,np.sin(theta))
plt.plot(theta,np.cos(theta))
plt.plot(theta,np.tan(theta))
The last plot will be a 3D plot this will help us visualize data on a 3 dimensional scale on python, this helps in cases where we could be doing integrals of 3D items and can get a better understanding of how we are going to separate the figure for cylindrical shells
ig = plt.figure()
# keeping the projection = 3d
# ctreates the 3d plot
ax = plt.axes(projection = '3d')
time = np.linspace(0,10)
x = np.cos(time)
y = np.sin(time)
plt.plot(x,y,time)
# add your title here!
plt.title('exp. 3d plot')
These are many different ways to represent data that we were shown in the second lab, each one has a set reason for its usage based off the data type or audience that needs a certain form of representation to understand said data set.