Learning MatPlotLib

Finding out how to use it for visualizing data

The last 2 weeks (or 4 weeks) in Data science we have looked at the presentation of data. This is useful because the unit is called “data science”, data in the name. The library we are using is called Matplotlib, this is a Python plotting library and well used with numerical mathematics extension called NumPy. It is very useful for its purpose and drawing random things (like Desmos), I have used this some time ago, but basically forgotten all about it, but now understand the basics again.

We had some tasks we were meant to complete; they were made from the tutorials from matplotlib. We had tasks mainly to make a type of graph. The task below required us to make the shown graph using the started code and using code from previous examples. The code given is strait forward, generating data and then using it, but the part that causes confusion is when you are making something based on a reference image.

import numpy as np
import matplotlib.pyplot as plt
np.random.seed(1)

# Generate data...
y_raw = np.random.randn(1000).cumsum() + 15
x_raw = np.linspace(0, 24, y_raw.size)

# Get averages of every 100 samples...
x_pos = x_raw.reshape(-1, 100).min(axis=1)
y_avg = y_raw.reshape(-1, 100).mean(axis=1)
y_err = y_raw.reshape(-1, 100).ptp(axis=1)

bar_width = x_pos[1] - x_pos[0]

# Make a made up future prediction with a fake confidence
x_pred = np.linspace(0, 30)
y_max_pred = y_avg[0] + y_err[0] + 2.3 * x_pred
y_min_pred = y_avg[0] - y_err[0] + 1.2 * x_pred

# Just so you don't have to guess at the colors...
barcolor, linecolor, fillcolor = 'wheat', 'salmon', 'lightblue'

# Now you're on your own!

The code that I added wasn’t as hard as I thought, I thought it would be harder because of how complex the graph and data was, but after tinkering with random things I got tit working. The hard part would have been trying to get the data from scratch rather than trying to only working out how to use provided data, luckily I didn't have to worry about that. [Image | Code]

fig, ax = plt.subplots()
ax.set_title("Future Projection of Attitudes")
ax.set_xlabel("Minutes Since class began")
ax.set_ylabel("Snarkiness (snark units)")
ax.set_xlim(0, 30)
ax.set_ylim(0, 100)

ax.fill_between(x_pred, y_min_pred, y_max_pred, color=fillcolor)

ax.plot(x_raw, y_raw, color=linecolor)
ax.bar(x_pos, y_avg, bar_width, color=barcolor, edgecolor="grey", yerr=y_err, ecolor="grey", capsize=5, align="edge")

plt.show()

The reason that I believe that we are learning about this (python and plotting), is because we need some way to show data, if we want to analyze it. This is basely what data science is, analyzing data. I found the teacher relatively helpful; this is because whenever I had questions (on topic or off topic) he would always answer them in a helpful way. My pears were also helpful in getting the graphs to work, I probably kept on distracting them though (still need to improve). If I was to do the week better, I would have spent more time doing the tasks so can do more (thus learning more). I would have spent more time on other subjects because they need priority too. When I came to class I came with the required materials (only computer, apparently, we need pen and paper, but never use so...), I would like to think I also came with a good attitude (but I can't say as it would be a bit biased, have to ask others). The ideas that made the most sense were the ones that were very simple, occasionally I get myself on a tangent when there is a simpler and more elegant way to do the thing.

TL;DR: Started new term, and doing more work. Learned more about Matplotlib.