Pundit:Current Code

From PrattWiki
Jump to navigation Jump to search

This page will keep track of current trends in EGR 103. It is meant to be a companion piece to any previous tests and solutions in case things were done in a different way in the past.

Loading Data from a Text File

Prior to the Spring 2020 semester, text files were generally either tab or space separated data files and np.loadtxt() could be used to load them. Starting with Spring of 2020 we focused on learning how to use pd.read_table() and pd.read_csv() for these things.

Current

Determine the file type (tab separated; other-separated) and if other-separated, the separator. Determine if the file has a header row or not. See Pandas. For prior semesters, text files were almost always tab separated with no headers; the code for that would be:

data = pd.read_table("Filename", header=None)

Past

Prior semesters mainly used

data = np.loadtxt("Filename")

to load a rectangular array of data from a file with no headers and with columns separated by spaces or tabs.

Slicing a Data File

Since prior semesters used np.loadtxt(), the result would be an array. With the pd.read_table() or pd.read_csv(), the result is a dataframe which requires slightly different syntax.

Current

The simplest way to pull a particular column out of a dataframe is to use the index location; for instance, to get the information out of the column with index 2, use:

stuff = data.iloc[:, 2]

Past

Since the data points were an array, simple slicing would work. For instance, to get the information out of the column with index 2, use:

stuff = data[:, 2]

Starting a Figure

Starting a figure has evolved with the matplotlib.pyplot module.

Current

See Python:Starting Plots for more detail, but most commonly, to create a single figure with a single set of axes in it, we are using:

fig = plt.figure(num=1, clear=True)
ax = fig.add_subplot(1, 1, 1)

or, if the axes are definitely not containing surface plots,

fig, ax = plt.subplots(num=1, clear=True)

Past

Prior semesters used a variety of different techniques to get a clear plot started since the clear kwarg is a relatively new addition; usually, this required creating and clearing the figure then re-creating it:

plt.figure(num=1).clf()
fig, ax = plt.subplots(num=1)

or

plt.figure(num=1)
plt.clf()
fig, ax = plt.subplots(num=1)