[go: up one dir, main page]

0% found this document useful (0 votes)
18 views2 pages

Ex - 1 - Plotting - Ipynb - Colab

Uploaded by

abhishek36063
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views2 pages

Ex - 1 - Plotting - Ipynb - Colab

Uploaded by

abhishek36063
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

First, we're going to load some public libraries.

Libraries have pre-written code which you can use without having to write your own

1 import matplotlib.pyplot as plt


2 import numpy as np
3 from google.colab import files

If we want to save our data to google drive, we have to connect our Python notebook to our google drive account! Running this will ask us
to confirm that we want this notebook to be able to access drive. This will also make a folder called 'Python4Physics' in your google drive

1 from google.colab import drive


2 drive.mount('/content/drive')
3 !mkdir -p "/content/drive/My Drive/Python4Physics"
4 filepath="drive/My Drive/Python4Physics/"

Mounted at /content/drive

here we define two arrays of real valued numbers, the first, ranges from 0 to 1.99 in steps of 0.01 the second, ranges from 0 to 1.9 in steps
of 0.1

1 x=np.arange(0,2,.01)
2 x1=np.arange(0,2,.1)

let's print these out to visualize them

1 print("************************")
2 print("x = ",x)
3 print("")
4 print("************************")
5 print("x1 = ",x1)

here we define two functions, the first, returns x to the power of 1.5, and the second returns x squared

1 def f1(x):
2 return pow(x,1.5)
3
4 def f2(x):
5 return pow(x,2)

here we break the figure into three panels. In the first panel, we plot both f1 and f2 in red and blue, respectively

1 plt.subplot(311)
2 plt.plot(x,f1(x),color='red')
3 plt.plot(x,f2(x),color='blue')

the extra lines here limit the x-range of the plot

1 plt.subplot(311)
2 plt.plot(x,f1(x),color='red')
3 plt.plot(x,f2(x),color='blue')
4 ############################
5 plt.xlim([min(x), max(x)])

Now let's add some vertical and horizontal black lines of width=1

1 plt.subplot(311)
2 plt.plot(x,f1(x),color='red')
3 plt.plot(x,f2(x),color='blue')
4 plt.xlim([min(x), max(x)])
5 ############################
6 plt.axvline(x=0,color='k',linewidth=1)
7 plt.axhline(y=0,color='k',linewidth=1)

Now lets add the second panel, and in it we'll plot f1 as a continuous red line, comparing it to f1 evaluated on the array x1 as discrete red
circles. The red circles are plotted using the errorbar function
1 plt.subplot(311)
2 plt.plot(x,f1(x),color='red')
3 plt.plot(x,f2(x),color='blue')
4 plt.xlim([min(x), max(x)])
5 plt.axvline(x=0,color='k',linewidth=1)
6 plt.axhline(y=0,color='k',linewidth=1)
7 #####################################
8 plt.subplot(312)
9 plt.plot(x,f1(x),color='red')
10 plt.errorbar(x1,f1(x1),markersize=8,fmt='o',color='r',mfc='white',mec='r', elinewidth=2, capsize=4, mew=1.4)
11 plt.xlim([min(x), max(x)])
12 plt.axvline(x=0,color='k',linewidth=1)
13 plt.axhline(y=0,color='k',linewidth=1)

Now lets add a third panel, and in it plot f2 as a continuous blue line and compare it to the function f2 evaluated on the array x1 as blue
squares. We'll then save it to the file 'example_plot.pdf' which will appear in your Python4Physics folder in Google Drive!

1 plt.subplot(311)
2 plt.plot(x,f1(x),color='red')
3 plt.plot(x,f2(x),color='blue')
4 plt.xlim([min(x), max(x)])
5 plt.axvline(x=0,color='k',linewidth=1)
6 plt.axhline(y=0,color='k',linewidth=1)
7 ############################
8 plt.subplot(312)
9 plt.plot(x,f1(x),color='red')
10 plt.errorbar(x1,f1(x1),markersize=8,fmt='o',color='r',mfc='white',mec='r', elinewidth=2, capsize=4, mew=1.4)
11 plt.xlim([min(x), max(x)])
12 plt.axvline(x=0,color='k',linewidth=1)
13 plt.axhline(y=0,color='k',linewidth=1)
14 ###########################
15 plt.subplot(313)
16 plt.plot(x,f2(x),color='b')
17 plt.errorbar(x1,f2(x1),markersize=8,fmt='s',color='b',mfc='white',mec='b', elinewidth=2, capsize=4, mew=1.4)
18 plt.xlim([min(x), max(x)])
19 plt.axvline(x=0,color='k',linewidth=1)
20 plt.axhline(y=0,color='k',linewidth=1)
21
22 plt.savefig(filepath+'example_plot.pdf',
23 bbox_inches='tight',
24 transparent=True)

here save the figure

1 Start coding or generate with AI.

You might also like