diff --git a/website/_posts/DataViz/2013-01-03-Part-2-Graph.md b/website/_posts/DataViz/2013-01-03-Part-2-Graph.md index 34a9f7a5..a0996eeb 100644 --- a/website/_posts/DataViz/2013-01-03-Part-2-Graph.md +++ b/website/_posts/DataViz/2013-01-03-Part-2-Graph.md @@ -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) ``` @@ -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 @@ -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 @@ -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 @@ -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