Week 4: 2D Arrays and Plotting
Week 4: 2D Arrays and Plotting
• You can look at an individual pixel (say, (512,512)), and you will find
that that pixel has a number/value (which for jpeg relates to how
bright/what color that pixel should be).
• The simpler case in astro imaging is usually that each pixel contains
monochromatic information- it is just an intensity. ˜
Defining a 2D array
• Example
2D Arrays
• More often than not, we pull 2D arrays out of data files rather than
constructing them ourselves
• Note: You can have even higher dimensional arrays- it all depends
on how much information you need to store.
Matrices
• Numpy has functions for defining matrices. (np.matrix)
• arr = np.zeros(10)
• A = np.vstack((arr,arr,arr,arr,arr,arr,arr,arr,arr,arr))
Better Solution
• A = np.zeros((10,10))
• B = np.ones((5,5))
Exercise
[1,2,3]
• construct a 2d array, 3x3, that looks like this: [4,5,6]
[7,8,9]
• (Use np.arange)
Solution
• a1 = np.arange(1,4)
• a2 = np.arange(4,7)
• a3 = np.arange(8,10)
• A = np.vstack((a1,a2,a3))
Better Solution
• A = np.arange(1,10)
• A =A.reshape((3,3))
• in_one_line = np.arange(1,10).reshape((3,3))
Plotting
• The first change you can make to this is to plot individual data
points rather than a continuous line (since data is never continuous
right??)
Plotting points individually
• plt.plot(x,y, ‘r+’) would plot the data points as red plusses (there are
a lot of shortcuts)
Fake data: x = np.arange(100)
y = x**2
y2 = y + 550 * np.random.normal(size=x.shape)
plt.plot(x,y2) plt.plot(x,y2,’r+’)
plt.plot(x,y2,’r+’,label=‘Measured position’)
plt.legend(loc=2)
We can plot multiple data sets on the same graph
just by plotting one after the other without creating a new figure
(But it will only look good if they are in similar ranges)
You can combine a color and a symbol in one string, e.g. ‘yD’ for yellow Diamond
Fun note: once you learn latex, you can use latex commands in your plot labels
On colors
• If those aren't enough colors for you, matplotlib also allows you to
select color by rgb value or hex…
• Experimentation and google are really the only way to be sure about
those
• Other shortcuts include c=‘r’ for specifying a color, ls for line style,
etc… Its a mess
Errorbars
• You can use the plt.errorbar function to plot data with error bars.
Basically you can either specify a single error value for all data
points, or have arrays (same length as x and y) with the errors for x
and y
• By default it assumes the same error above and below a point, but
you CAN change that (rarely have to)
y_error = y2/np.random.randint(1,20)
plt.errorbar(x,y2,yerr=y_error, fmt='s', c='r', label='Data')
Advice
• Always title and label the axes of your graphs (you can see how in
earlier tutorials).