Understanding Matplotlib Twinxy

Hello Geeks, I hope all are doing great. Welcoming you back to another article in this journey of learning. So, today in this article we will learn about twinxy in the matplotlib library. We will first understand it and then see some demonstration over it to understand it more clearly. So, let’s get started.

Matplotlib

Before going to the core of our topic, let’s see what actually matplotlib is? So, matplotlib is the plotting library for the Python programming language. Moreover, it is much widely used for plotting data in python’s numerical mathematical library known as Numpy. It works as the object-oriented API for embedding and plotting graphs for various applications.

Now, for this article let’s assume that you have the basic idea of Matplotlib Library. Click here to learn more about matplotlib.

Now, matplotlib provides us with hundreds of features to work upon. And these features are available in various sub-module of the library. Discussing each of them in this article is quite a tedious task. So we will stick to twinxy() method in this article.

Twinxy Method

So, this method is available in matplotlib.axes.Axes class of the matplotlib library. This class is responsible for creating axis, lines and texts, and polygons for our plot. Moreover, it is also responsible for plotting axes ( x-axis & y-axis) for our graph.

Now, many times we are in the condition in which we plot more than one function in a plot and we want different axis representations for them. However, this is not necessarily true but sometimes we want the axis to be represented on both sides of the graph for the y-axis and both up&down representations for the x-axis. In this condition, we use twinxy() method.

Now, a very important thing to note here is that there is no method named twinxy() available in the Axis class. There are two different functions available for the twinning x-axis and y-axis separately named twinx() and twiny(). First, take a look at them.

So, this method creates twin axes sharing the x-axis and independent y-axis on the right side of the plot. Let’s see an example.

Example 1:

import numpy as np
import matplotlib.pyplot as plt

# Create some mock data
t = np.arange(0.01, 10.0, 0.01)
data1 = np.exp(t)
data2 = np.sin(2 * np.pi * t)

fig, ax1 = plt.subplots()

color = 'tab:red'
ax1.set_xlabel('time (s)')
ax1.set_ylabel('exp', color=color)
ax1.plot(t, data1, color=color)
ax1.tick_params(axis='y', labelcolor=color)

ax2 = ax1.twinx()  # instantiate a second axes that shares the same x-axis

color = 'tab:blue'
ax2.set_ylabel('sin', color=color)  # we already handled the x-label with ax1
ax2.plot(t, data2, color=color)
ax2.tick_params(axis='y', labelcolor=color)

fig.tight_layout()  # otherwise the right y-label is slightly clipped
plt.show()
twinx axis

So, in the above example, we can see that we have plotted graphs for two functions, and then to define a twinx axis we created another axis as ax2 = ax1.twinx() method.

Example 2

It goes similar to the y-axis we just need to write new_axis = old_axis.twiny(). In this way, we can define a new twinning y-axis. Let’s see an example of it.

import numpy as np
import matplotlib.pyplot as plt

# Create some mock data
t = np.arange(0.01, 10.0, 0.01)
data1 = np.exp(t)
data2 = np.sin(0.8 * np.pi * t)

fig, ax1 = plt.subplots()

color = 'tab:red'
ax1.set_xlabel('time (s)')
ax1.set_ylabel('exp', color=color)
ax1.plot( data1,t, color=color)
ax1.tick_params(axis='y', labelcolor=color)

ax2 = ax1.twiny()  # instantiate a second axes that shares the same x-axis

color = 'tab:blue'
ax2.set_ylabel('sin', color=color)  # we already handled the x-label with ax1
ax2.plot( data2,t, color=color)
ax2.tick_params(axis='y', labelcolor=color)

fig.tight_layout()  # otherwise the right y-label is slightly clipped
plt.show()
new_axis = old_axis.twiny()

Example 3: Twinx().Twiny()

Now, once you understood how can we use twinx() and twiny() separately, now the question arises that how can we define them simultaneously like twinxy(). So, to do that we have to stack our function as new_axis = old_axis.twinx().twiny() instead of using twinxy() as there is no function available of that name. Let’s see an example, how can we do it.

import numpy as np
import matplotlib.pyplot as plt

# Create some mock data
t = np.arange(0.01, 10.0, 0.01)
data1 = np.exp(t)
data2 = np.sin(2 * np.pi * t)

fig, ax1 = plt.subplots()

color = 'tab:red'
ax1.set_xlabel('time (s)')
ax1.set_ylabel('exp', color=color)
ax1.plot(t, data1, color=color)
ax1.tick_params(axis='y', labelcolor=color)

ax2 = ax1.twinx().twiny()  # instantiate a second axes that shares the same x-axis

color = 'tab:blue'
ax2.set_ylabel('sin', color=color)  # we already handled the x-label with ax1
ax2.plot(t, data2, color=color)
ax2.tick_params(axis='y', labelcolor=color)

fig.tight_layout()  # otherwise the right y-label is slightly clipped
plt.show()
Twinx().Twiny()

Conclusion

So, today in this article we have seen what is twinx and twiny and how can we implement the functionality of twinning the x-axis and y-axis simultaneously in our graph. We have seen the demonstration of how can we plot a graph with twinx().twiny() method.

I hope this article helped you. Thank You.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments