Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 79c8694

Browse files
Fixed typos
1 parent 88e1bdc commit 79c8694

File tree

1 file changed

+16
-23
lines changed
  • content/posts/pyplot-vs-object-oriented-interface

1 file changed

+16
-23
lines changed

content/posts/pyplot-vs-object-oriented-interface/index.md

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "Pyplot vs Object Oriented Interface"
33
date: 2020-05-27T20:21:30+05:30
4-
draft: false
4+
draft: false
55
description: "This post describes the difference between the pyplot and object oriented interface to make plots."
66
author: Tejas Sanap
77
displayInList: true
@@ -15,9 +15,9 @@ resources:
1515

1616
## Generating the data points
1717

18-
To get acquainted with the basics of plotting with `matplotlib`, let's try plotting how much distance an object under free-fall travels with respect to time and also, it's velocity at each time step.
18+
To get acquainted with the basics of plotting with `matplotlib`, let's try plotting how much distance an object under free-fall travels with respect to time and also, its velocity at each time step.
1919

20-
If, you have ever studied physics, you can tell that is a classic case of Newton's equations of motion, where...
20+
If, you have ever studied physics, you can tell that is a classic case of Newton's equations of motion, where
2121

2222
$$ v = a \times t $$
2323

@@ -33,7 +33,7 @@ velocity = np.zeros_like(time, dtype=float)
3333
distance = np.zeros_like(time, dtype=float)
3434
```
3535

36-
We know that under free-fall, all objects move with the constant acceleration of 9.8 m/s<sup>2</sup>.
36+
We know that under free-fall, all objects move with the constant acceleration of $$g = 9.8~m/s^2$$
3737

3838
```python
3939
g = 9.8 # m/s^2
@@ -42,7 +42,7 @@ velocity = g * time
4242
distance = 0.5 * g * np.power(time, 2)
4343
```
4444

45-
The above code gives us two `numpy` arrays populated with the distance and velocity data points.
45+
The above code gives us two `numpy` arrays populated with the distance and velocity data points.
4646

4747
## Pyplot vs. Object-Oriented interface
4848

@@ -90,7 +90,7 @@ The plot below shows us how the velocity is increasing.
9090
<b>Fig. 1.2</b> Velocity is increasing in fixed steps, due to a "constant" acceleration.
9191
</div>
9292

93-
Let's try to see, what kind of plot we get when we plot both distance and velocity in the same plot.
93+
Let's try to see what kind of plot we get when we plot both distance and velocity in the same plot.
9494

9595
```python
9696
plt.figure(figsize=(9,7), dpi=100)
@@ -103,31 +103,25 @@ plt.grid(True)
103103
```
104104

105105
![png](figure/distance-and-velocity-same-axes.png)
106-
<div class="image-caption">
107-
<b>Fig. 1.3</b> Distance and Velocity are plotted against the same geometrical axis.
108-
</div>
109106

110-
Here, we run into some obvious and serious issues. We can see that since both the quantities share the same axis, but have very different magnitudes, the graph looks disproportionate. What we need to do is separate the two quantities on two different axes. This is where the second approach to making plot comes into play.
107+
Here, we run into some obvious and serious issues. We can see that since both the quantities share the same axis but have very different magnitudes, the graph looks disproportionate. What we need to do is separate the two quantities on two different axes. This is where the second approach to making plot comes into play.
111108

112109
Also, the `pyplot` approach doesn't really scale when we are required to make multiple plots or when we have to make intricate plots that require a lot of customisation. However, internally `matplotlib` has an Object-Oriented interface that can be accessed just as easily, which allows to reuse objects.
113110

114111
### Object-Oriented Interface
115112

116-
When using the OO interface, it helps to know how the `matplotlib` structures it's plots. The final plot that we see as the output is a 'Figure' object. The `Figure` object is the top level container for all the other elements that make up the graphic image. These "other" elements are called `Artists`. The `Figure` object can be thought of as a canvas, upon which different artists act to create the final graphic image. This `Figure` can contain any number of various artists.
113+
When using the OO interface, it helps to know how the `matplotlib` structures its plots. The final plot that we see as the output is a 'Figure' object. The `Figure` object is the top level container for all the other elements that make up the graphic image. These "other" elements are called `Artists`. The `Figure` object can be thought of as a canvas, upon which different artists act to create the final graphic image. This `Figure` can contain any number of various artists.
117114

118115
![png](figure/anatomy-of-a-figure.png)
119-
<div class="image-caption">
120-
Courtesy of <a href="https://matplotlib.org/3.2.1/gallery/showcase/anatomy.html">matplotlib</a>.
121-
</div>
122116

123117
Things to note about the anatomy of a figure are:
124-
1. All of the items labelled in "blue" are `Artists`. `Artists` are basically all the elements that are rendered onto the figure. This can include text, patches (like arrows and shapes), etc. Thus, all the following `Figure`, `Axes` and `Axis` objects are also Artists.
118+
1. All of the items labelled in *blue* are `Artists`. `Artists` are basically all the elements that are rendered onto the figure. This can include text, patches (like arrows and shapes), etc. Thus, all the following `Figure`, `Axes` and `Axis` objects are also Artists.
125119
2. Each plot that we see in a figure, is an `Axes` object. The `Axes` object holds the actual data that we are going to display. It will also contain X- and Y-axis labels, a title. Each `Axes` object will contain two or more `Axis` objects.
126120
3. The `Axis` objects set the data limits. It also contains the ticks and ticks labels. `ticks` are the marks that we see on a axis.
127121

128122
Understanding this hierarchy of `Figure`, `Artist`, `Axes` and `Axis` is immensely important, because it plays a crucial role in how me make an animation in `matplotlib`.
129123

130-
Now, that we understand how plots are generated, we can easily solve the problem we faced, earlier. To make Velocity and Distance plot to make more sense, we need to plot each data item against a seperate axis, with a different scale. Thus, we will need one parent `Figure` object and two `Axes` objects.
124+
Now that we understand how plots are generated, we can easily solve the problem we faced earlier. To make Velocity and Distance plot to make more sense, we need to plot each data item against a seperate axis, with a different scale. Thus, we will need one parent `Figure` object and two `Axes` objects.
131125

132126
```python
133127
fig, ax1 = plt.subplots()
@@ -150,9 +144,9 @@ plt.show()
150144

151145
![png](figure/distance-and-velocity-different-axes-unfinished.png)
152146

153-
This plot as it is, is still not very intutitive. We should add a grid and a legend. Perhaps, we can also change the color of the axis labels and tick labels to the color of the lines.
147+
This plot is still not very intuitive. We should add a grid and a legend. Perhaps, we can also change the color of the axis labels and tick labels to the color of the lines.
154148

155-
But, something very weird happens when we try to turn on the grid, which you can see [here](https://github.com/whereistejas/whereistejas.github.io/blob/master/assets/jupyter-nb/double-pendulum-part-1-basics-of-plotting.ipynb) at In Cell 8. The grid lines don't align with the tick labels on the both the Y-axes. We can see that tick values `matplotlib` is calculating on its own are not suitable to our needs, and thus, we will have to calculate them, ourselves.
149+
But, something very weird happens when we try to turn on the grid, which you can see [here](https://github.com/whereistejas/whereistejas.github.io/blob/master/assets/jupyter-nb/double-pendulum-part-1-basics-of-plotting.ipynb) at Cell 8. The grid lines don't align with the tick labels on the both the Y-axes. We can see that tick values `matplotlib` is calculating on its own are not suitable to our needs and, thus, we will have to calculate them ourselves.
156150

157151
```python
158152
fig, ax1 = plt.subplots()
@@ -182,18 +176,18 @@ plt.show()
182176

183177
The command `ax1.set_yticks(np.linspace(*ax1.get_ybound(), 10))` calculates the tick values for us. Let's break this down to see what is happening:
184178
1. The `np.linspace` command will create a set of `n` no. of partitions between a specified upper and lower limit.
185-
2. The method `ax1.get_ybound()` returns a list which contains the maximum and minimum limits for that particular axis (which in our case is the Y-axis).
179+
2. The method `ax1.get_ybound()` returns a list which contains the maximum and minimum limits for that particular axis (which in our case is the Y-axis).
186180
3. In python, the operator `*` acts as an unpacking operator when prepended before a `list` or `tuple`. Thus, it will convert a list `[1, 2, 3, 4]` into seperate values `1, 2, 3, 4`. This is an immensely powerful feature.
187181
4. Thus, we are asking the `np.linspace` method to divide the interval between the maximum and minimum tick values into 10 equal parts.
188182
5. We provide this array to the `set_yticks` method.
189183

190-
The same process is repeated for second axis.
184+
The same process is repeated for the second axis.
191185

192186
![png](figure/distance-and-velocity-different-axes-finished.png)
193187

194188
## Conclusion
195189

196-
In this part, we covered some basics of `matplotlib` plotting, covering the basic two approaches of how to make plots. In the next part, we will cover how to make simple animations. If you like the content of this blog post, if you have any suggestions or comments, drop me an email or tweet at me or ping me on IRC. Nowadays, you will find me hanging arounfd #matplotlib on Freenode. Thanks!
190+
In this part, we covered some basics of `matplotlib` plotting, covering the basic two approaches of how to make plots. In the next part, we will cover how to make simple animations. If you like the content of this blog post, or you have any suggestions or comments, drop me an email or tweet or ping me on IRC. Nowadays, you will find me hanging around #matplotlib on Freenode. Thanks!
197191

198192
## After-thoughts
199193
This post is part of a series I'm doing on my personal [blog](http://whereistejas.me). This series is basically going to be about how to animate stuff using python's `matplotlib` library. `matplotlib` has an excellent [documentation](https://matplotlib.org/3.2.1/contents.html) where you can find a detailed documentation on each of the methods I have used in this blog post. Also, I will be publishing each part of this series in the form of a jupyter notebook, which can be found [here](https://github.com/whereistejas/whereistejas.github.io/blob/master/assets/jupyter-nb/Part-1-basics-of-plotting.ipynb).
@@ -204,7 +198,7 @@ The series will have three posts which will cover:
204198
3. Part 3 - Optimizations to make animations faster (blitting).
205199

206200
I would like to say a few words about the methodology of these series:
207-
1. Each part, will have a list of references at the end of the post, mostly leading to appropriate pages of the documentation and helpful blogs written by other people. **THIS IS THE MOST IMPORTANT PART**. The sooner you get used to reading the documentation, the better.
201+
1. Each part will have a list of references at the end of the post, mostly leading to appropriate pages of the documentation and helpful blogs written by other people. **THIS IS THE MOST IMPORTANT PART**. The sooner you get used to reading the documentation, the better.
208202
2. The code written here, is meant to show you how you can piece everything together. I will try my best to describe the nuances of my implementations and the tiny lessons I learned.
209203

210204
## References
@@ -213,4 +207,3 @@ I would like to say a few words about the methodology of these series:
213207
1. [Matplotlib: An Introduction to its Object-Oriented Interface](https://medium.com/@kapil.mathur1987/matplotlib-an-introduction-to-its-object-oriented-interface-a318b1530aed)
214208
2. [Lifecycle of a Plot](https://matplotlib.org/3.2.1/tutorials/introductory/lifecycle.html)
215209
3. [Basic Concepts](https://matplotlib.org/faq/usage_faq.html)
216-

0 commit comments

Comments
 (0)