Create Multiple Buttons in Matplotlib
We all very well know that Python offers us a very beautiful library named Matplotlib that has the support of numerous methods using which we can plot graphs and visualize our data. Many a time, we all want our plots to look appealing and become interactive at the same time. For that purpose, buttons can be added in our matplotlib plots that can be used to trigger some on-click events and make our plots interactive.
In this article, we will be reading about creating multiple buttons in Matplotlib using the Buttons widget. We will use these buttons to create certain events (here plotting different graphs) when they get clicked. Initially, we will be plotting a graph for y=x and then make three different buttons named Prev, Next, and Home.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
- When the Home button will be clicked, the initial page where a graph for y = x will be shown.
- When the Prev button will be clicked, a graph for y= will be shown.
- When the Next button will be clicked, a graph for y = 2x will be shown.
As stated earlier, initially a graph will be plotted for y=x, for that, a list of x-values in the range of 0 to 1 using np.arange() function will be generated and then assigned the same values to y-variable as well and then made a subplot to plot this graph. To note, plt.subplots_adjust() function is used to position the plot, the left and bottom parameter set the padding from the left side and the bottom of the graph. The initial graph will be displayed as:
Now that we have plotted our initial graph, let’s start adding our buttons and proceed to the methodology to add the Home button and in the same manner other two can be added.
The plt.axes() function accepts a list as an argument denoting the attributes of the button related to its position and size. The four elements inside the list corresponding to the left, bottom, width, and height values respectively. Now, we will be using the Button module from matplotlib.widgets library to make our button. The first argument it takes is the attributes of the button, apart from that the label parameter takes the name to be shown on the button, the color parameter decides the color of the button and the hovercolor parameter shows the changes in the color of the button when we hover our mouse pointer over the button (here it changes from the pink color to color of a tomato that is red).
We have now made the button, but to make some events happen when they get clicked, we need to create some on-click events. It can be done using the on_clicked() method of the button. The on_clicked() method accepts a custom function as a parameter that describes what action will be performed when the button gets clicked. For the Home button, plot1 is made as to the custom function which displays the graph of y=x which will be shown as:
Similarly, the Prev button can also be made to display the graph for y = when they get clicked, and it will be shown as
Similarly, the Next button is used to display the graph for y = 2x when they get clicked, and it will be shown as