A Beginners Guide to Programming and Automated Analysis: Part 2 data and files
Welcome to part 2 of the series where we will separate the data from the code. Doing this allows us to be more flexible in what we can do. It also keeps everything more organized.
In the last part, you learned how to graph. In this part we will make basic refinements
Here is the code from last time
Lesson01.py
#plot stuff
Import matplotlib.pyplot as plt
plt.plot([1,2,3],[3,2,1])
plt.show()
The problem with this code is it would be hard to read if the x and y values are long.
lets refine this a bit
import matplotlib.pyplot as plt
x1=[]
y1 =[]
x2= []
y2= []
fig_name = “the title”
plt.plot(x1, y1)
plt.plot(x2, y2)
plt.xlabel(“stuff”)
plt.ylabel(“things”)
plt.title(fig_name)
plt.grid(“”)
plt.show()
x1 and y1 are called variables. Variables are names for data. Python allows the storage of data in different ways called types. Python has numerous basic types and from these types we can make more complex types. Lists can contain any type supported by python.
You can learn more about the basic types here but for now, let’s talk about three basic types: integers, string, float.
Integers (int) are whole positive and negative numbers. On most machines an int can store any number from -2^32 to 2^32. Any fraction or number with a decimal is not an integer. 1, 0 , and -40003 are ints but 1.0, 40.3, and .1 are not. The type of variable that x1 and y1 is is a list of integers.
Floating point numbers (float) can roughly represent any number positive or negative number including fractions. On most computers, they range between -2^32 to 2^32.
Numeric types such as ints and floats can be manipulated the common arithmetic operators like +,-,/, and * to produce a numeric result. For instance
a = 3
b = 4
c = b + a
c is 7
You can combine floats and numerics but the resulting type will become a float.
Non numeric types, as the name implies, do not work like the regular numbers you are familiar with. Lists and strings are examples of such types. Just as numeric types have operators that allow you to manipulate and produce other numerics so do lists and strings. Lists can grow and shrink and each individual element can be accessed and changed on there own. One way to grow a list is to use the addition sign like so:
Strings are basically lists of characters with some special properties. The value of the string must be surrounded by quotes. Open or closed quotes are fine as long as you are consistent. For example
s = “a string”
z = ‘a string’
s and z are equivalent.
import matplotlib.pyplot as plt
import numpy
x1=[]
y1 =[]
x2= numpy.exp(x1)
y2= []
plt.plot(x1, y1)
plt.plot(x2, y2)
plt.xlabel(“”)
plt.ylabel(“”)
plt.title(“”)
plt.grid(“”)
plt.show()
We can even chain these together
x1=[]
y1 =[]
x2= numpy.exp(x1)
y2= []
plt.plot(x1, y1)
plt.plot(x2, y2)
plt.xlabel(“”)
plt.ylabel(“”)
plt.title(“”)
plt.grid(“”)
plt.show()