What will we cover in this tutorial?
And if you also, like to program in an object oriented fashion, then most tutorial will make you feel wondering if no one loves the art of beautiful code?
Let me elaborate. The integration and interaction with Matplotlib is done in a functional way with a lot of side effects. Not nice.
Not sure what I talk about? We will cover that too.
Step 1: How NumPy is demonstrated to make plots with Matplotlib and what is wrong with it
Let’s make a simple example.
import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 5, 11) y = x ** 2 plt.plot(x, y) plt.xlabel("X Label") plt.ylabel("Y Label") plt.title("Title") plt.show()
This will result in the following chart.
That is nice and easy! So what is wrong with it?
What is a side effect in programming?
…that is to say has an observable effect besides returning a value (the main effect) to the invoker of the operation.https://en.wikipedia.org/wiki/Side_effect_(computer_science)
What does that mean?
Well, let’s examine the above example.
We call plt.plt(x, y) and what happens? Actually we don’t know. We do not get anything in return.
Continue to call plt.xlabel(…), plt.ylabel(…), and plt.title(…). Then we call plt.show() to see the result. Hence, we change the state of the plt library we imported. See, we did not create an object. We call the library directly.
This is difficult as a programmer to understand without having deep knowledge of the library used.
So how to do it in more understandable way?
Step 2: How to create a chart with Matplotlib with NumPy in an object oriented way and why it is better
Let’s look at this code and examine it.
import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 5, 11) y = x ** 2 fig, ax = plt.subplots() ax.plot(x, y) ax.set_xlabel("X Label") ax.set_ylabel("Y Label") ax.set_title("Title") fig.show() plt.waitforbuttonpress()
Here we do it differently but get the same result. It is more understandable that when we call a method on object ax, that the state of ax is changing and not something in the library hidden in some side effect.
You can also show the the figure fig by calling show() and not the library. This requires that we add waitforbuttonpress() on plt, otherwise it will destroy the window immediately.
Note, that you do not have these challenges in JuPyter notebook – the plots are shown without the call to show.
You could keep the plt.show() instead of fig.show() and plt.waitforbuttonpress(). But the above code is more intuitive and easier to understand.
How to create a chart with Matplotlib of a Pandas DataFrame in an object oriented way
This is straight forward as Matplotlib is well integrated with Pandas.
import matplotlib.pyplot as plt import numpy as np import pandas as pd x = np.linspace(0, 5, 11) y = x ** 2 df = pd.DataFrame(data=y, index=x) fig, ax = plt.subplots() ax.plot(df) ax.set_xlabel("X Label") ax.set_ylabel("Y Label") ax.set_title("Title") fig.show() plt.waitforbuttonpress()
Notice, that the DataFrame is created from the NumPy arrays. Hence, here we do not gain anything from using it. This is just to exemplify how easy it is to use s in an object oriented way with Pandas.
I have found that programmer either hate or love Matplotlib. I do not always know why, but I have discovered that this non-object oriented way of using Matplotlib is annoying some programmers.
This is a good reason to hate it, but I would say that there are no good alternative to Matplotlib – or at least, they are build upon Matplotlib.
I like the power and ease using Matplotlib. I do like that the option of using it object oriented, which makes the code more intuitive and easier to understand for other programmers.