<jupyter_start><jupyter_text>Simple plot of data from txt file txt file contains text lines and numerical data<jupyter_code>#Comments: # for single line comment and ''' lines ''' for multilines comment
#Shortcut command to run file.ipynb in Jupyter in windows OS: Shift+Enter
#open txt file
#create emply lists for x and y values
#read line by line from txt file
#if line starts with any character (here #), continue without doing anything
#if line starts with other than #, then slpit the line by delimiter (here is a space)
#specify which are which
#add those values in respective parameters, if needed
#plot interested lists
#format plot
#save plot
import matplotlib.pyplot as plt
import random as random
import json as json
import pandas as pd
import numpy as np
%matplotlib inline
import seaborn as sns
sns.set()
with open ("testdata1.txt",'r') as f:
    i = 0                                       #it works even without use of i
    X = []                                      # create blank list for x axis values
    Y = []
    R = []
    for line in f:                              #each lin in file f
        #print(line)
        if line.startswith("#"):                #if line start with # character, continue without doing any work
            #print(line)
            continue
        else:                                   #if line start with othern than # then slpit the line by a space ' '
            lineParts = line.split(' ')
            #print(lineParts)
            x = float(lineParts[0])             # 1 intexed value is x, check w/wo float()
            #print(x)
            X.append(x)                         # add values of x in list X
            #print(X)
            y = float(lineParts[1])        
            #print(y)
            r = y / x
            #print("resistance = ", r,"Ohm")
            Y.append(y)
            '''print(Y)'''                       # two ways of commenting
            #print(Y)
            R.append(r)
            #print(R)
            i = i + 1
#print(i,x,y)
#print(Y)
#print(R)
#print(len(R))
avgR = sum(R)/len(R)
myMean = np.mean(R)
print(myMean)
#print("resistance = ", avgR ,"Ohm")
#print(X,Y)
plt.figure(figsize = [14,10])                      #figsize(width, height) in inches
#plt.bar(X,Y)       # ^ . try withh plt.plot
plt.plot(X,Y,color = 'red', marker = 'o')       # ^ . try withh plt.plot
plt.axis([0, 2.0, 0, 5.0])                         #axis[xmin,xmax, ymin,ymax]
#another way to set axis limit
#plt.xlim(xmin,xmax)
#plt.ylim(ymin,ymax)
plt.title('Practice, NPSC2020', fontsize=36)
plt.xlabel('Current (A)', fontsize=34)
plt.ylabel('Voltage (V)', fontsize=34)
plt.tight_layout()                             # this is for margin
plt.grid(True)                                 # comment this if u do not need grid
plt.savefig("testplot.png")                    # png, jpg, eps, pdf as u like
plt.show()

with open ("testdata.txt",'r') as f:
    i = 0                                       #it works even without use of i
    X = []                                      # create blank list for x axis values
    Y = []
    R = []
    for line in f:                              #each lin in file f
        #print(line)
        if line.startswith("#"):                #if line start with # character, continue without doing any work
            #print(line)
            continue
        else:                                   #if line start with othern than # then slpit the line by a space ' '
            lineParts = line.split(' ')
            #print(lineParts)
            x = float(lineParts[0])             # 1 intexed value is x, check w/wo float()
            #print(x)
            X.append(x)                         # add values of x in list X
            #print(X)
            y = float(lineParts[1])        
            #print(y)
            Y.append(y)
            '''print(Y)'''                       # two ways of commenting
            #print(Y)
            R.append(r)
            #print(R)
            i = i + 1
#print(i,x,y)
#print(Y)
#print(R)
#print(len(R))
avgR = sum(R)/len(R)
myMean = np.mean(R)
print(myMean)
#print("resistance = ", avgR ,"Ohm")
#print(X,Y)
plt.figure(figsize = [14,8])                      #figsize(width, height) in inches
#plt.bar(X,Y)       # ^ . try withh plt.plot
plt.plot(X,Y,color = 'red', marker = 'o')       # ^ . try withh plt.plot
plt.axis([0, 35, 0, 50])                         #axis[xmin,xmax, ymin,ymax]
#another way to set axis limit
#plt.xlim(xmin,xmax)
#plt.ylim(ymin,ymax)
plt.title('Practice, NPSC2020', fontsize=22)
plt.xlabel('Current (A)', fontsize=18)
plt.ylabel('Voltage (V)', fontsize=18)
plt.tight_layout()                             # this is for margin
plt.grid(True)                                 # comment this if u do not need grid
plt.savefig("testplot.png")                    # png, jpg, eps, pdf as u like
plt.show()<jupyter_output>2.75
StarcoderdataJupyterStructuredCleanDedup