Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typos and errors #190

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions website/_containers/dataviz/2013-01-03-part-2.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Graph our sample data with matplotlib.
- We include the parse function here so we build on the process of parse → plot. We need to parse the data into the list of dictionaries so that we can easily tell matplotlib what and how to plot. We could, however, imported it from `parse.py`. As a **challenge** to you, try editing away the parse function in `graph.py` and import it from your `parse.py`.

### Visualize Functions
Let’s first take a look at a chuck of data that we just parsed to get a better idea of what sort of data we’re working with:
Let’s first take a look at a chunk of data that we just parsed to get a better idea of what sort of data we’re working with:

```bash
{
Expand Down Expand Up @@ -121,7 +121,7 @@ item["DayOfWeek"] for item in data_file
This is called a list comprehension. You can read it as, “iterate every dictionary value of every dictionary key set to ‘DayOfWeek’ for every line item in `data_file`.” A list comprehension just a for-loop put in a more elegant, “Pythonic” way.

<div class="well">
<b>Challenge yourself:</b> write out a for-loop for our <code>counter</code> variable.
<b>Challenge yourself:</b> Write out a for-loop for our <code>counter</code> variable.
</div>


Expand All @@ -145,6 +145,10 @@ The counter object is a dictionary with the keys as days of the week, and values

Here, `data_list` takes each key of `counter` to grab the value associated with each day. Because we manually write out each `counter` key, we force the order that we want. **Note:** a dictionary does _not_ preserve order, but a list does; this is why we’re electing to manually key into each value of a dictionary to make a list of each value.

<div class="well">
<b>Challenge yourself:</b> Write out the <code>data_list</code> variable code as a list comprehension.
</div>

The `day_tuple` is just a tuple of strings that we will use for our x-axis labels. **A quick note:** we had to make our `day_tuple` variable a tuple because `plt.xticks()` only accepts tuples for labeling the x-axis. This is because tuples are an immutable type of data structure in Python’s library, meaning you can’t change it (not without making a copy of the variable onto a new variable), as well as it preserves order.

We now tell `matplotlib` to use our `data_list` as data points to plot. The `pyplot` module, what we’ve renamed as `plt`, has a function called `plot()` which takes a list of data points to plot on the y-axis:
Expand All @@ -163,7 +167,7 @@ Just creating the variable `day_tuple` for our x-axis isn’t enough — we also
plt.xticks(range(len(day_tuple)), day_tuple)
```

We give `plt.xticks()` two parameters, one being a list and the other being our tuple, `labels`.
We give `plt.xticks()` two parameters, one being a list and the other being our tuple, `day_tuple`.

The first parameter is `range(len(day_tuple))`. Here, we call `len()` on our `day_tuple` variable — `len()` returns an integer, a count of the number of items in our tuple `day_tuple`. Since we have seven items in our `day_tuple` (**pop quiz:** why do we have seven items?), the `len()` will return 7. Now we have `range()` on our length of the `day_tuple`. If you feed `range()` one parameter `x`, it will produce a list of integers from `0` to `x` (not including `x`). So, deconstructed, we fed `plt.xticks()` the following:

Expand Down