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

Some small fixes to Datviz Part II web tutorial #143

Open
wants to merge 5 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
19 changes: 15 additions & 4 deletions website/_posts/DataViz/2013-01-03-Part-2-Graph.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ If you are curious about the `plot()` function, open a `python` prompt in your t
Just creating the variable `day_tuple` for our x-axis isn’t enough — we also have to assign it to our `plt` by using the method `xticks()`:

```python
# Assign labels to the plot
# create the amount of ticks needed for our x-axis, and assign
# the labels
plt.xticks(range(len(day_tuple)), day_tuple)
```

Expand Down Expand Up @@ -281,7 +282,7 @@ def visualize_type():
# by category

# Set the labels which are based on the keys of our counter.
# Since order doesn't matter, we can just used counter.keys()
# Since order doesn't matter, we can just use counter.keys()

# Set exactly where the labels hit the x-axis

Expand Down Expand Up @@ -325,6 +326,16 @@ Next we finally use a bit of numpy magic (we had imported the numpy library as `

We have a new variable, `xlocations`, which will be used to help place the `plt.xticks()`. We’re using the `numpy` (aka `np`) library to access the `arange` function. This creates a list similar to what `range()` would make, into an array that you can manipulate a bit differently. Here, we're adding `0.5`. If you were to `print xlocations`, you would see `[0.5, 1.5, 2.5, ... , 16.5, 17.5]` where `0.5` was added to each int of the list. You’ll see why we need the `0.5` a bit later.

We need to set the width variable and assign data to the bar plot:

```python
# Width of each bar
width = 0.5

# Assign data to a bar plot
plt.bar(xlocations, counter.values(), width=width)
```

Now we assign our x- & y-ticks (should be familiar to `visualize_days()`):

```python
Expand All @@ -336,7 +347,7 @@ For the `plt.xticks()`, the first parameter should look similar to before, but h

Notice how we can pass `xticks()` more parameters than we did before. If you read the [documentation](http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.xticks) of that function, you can pass it `*args` and `**kwargs`, or arguments and keyword arguments. It mentions that you can pass matplotlib-defined [text properties](http://matplotlib.org/api/artist_api.html#matplotlib.text.Text) for the labels — so that would explain the `**kwargs` element there. If nothing is passed in for `rotation` then it’s set to a default defined in their text properties documentation.

Next, we just add a little bit of spacing to the bottom of the graph so the labels (since some of them are long, like `Forgery/Counterfeiting`). We use the `.subplots_adjust()` function. In matplotlib, you have the ability to render multiple graphs on one window/function, called subplots. With one graph, subplots can be used to adjust the spacing around the graph itself.
Next, we just add a little bit of spacing to the bottom of the graph so the labels (since some of them are long, like `Forgery/Counterfeiting`) aren't cut off in the graph. We use the `.subplots_adjust()` function. In matplotlib, you have the ability to render multiple graphs on one window/function, called subplots. With one graph, subplots can be used to adjust the spacing around the graph itself.

```python
# Give some more room so the labels aren't cut off in the graph
Expand All @@ -357,7 +368,7 @@ Again — here I just played with the numbers until I got something I liked. I e
Finally, our favorite — rendering and closing the graph!

```python
# Save the plot!
# Save the graph!
plt.savefig("Type.png")

# Close figure
Expand Down