Create Multiple Buttons in Matplotlib
Last Updated :
15 Mar, 2021
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.
- 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=x^2 will be shown.
- When the Next button will be clicked, a graph for y = 2x will be shown.
Python
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Button
# Generating x and y-values
x = np.arange(0, 1, 0.02)
y = x
fig, ax = plt.subplots()
plt.subplots_adjust(left=0.1, bottom=0.3)
p, = plt.plot(x, y, color='red')
ax.title.set_text('Graph for y = x')
# Home button
axButn1 = plt.axes([0.1, 0.1, 0.1, 0.1])
btn1 = Button(
axButn1, label="Home", color='pink', hovercolor='tomato')
# To plot a graph for y = x
def plot1(event):
p.set_xdata(x)
p.set_ydata(x)
ax.title.set_text('Graph for y = x')
plt.draw()
btn1.on_clicked(plot1)
# Previous button
axButn2 = plt.axes([0.3, 0.1, 0.1, 0.1])
btn2 = Button(
axButn2, label="Prev", color='pink', hovercolor='tomato')
# To plot a graph for y = x**2
def plot2(event):
p.set_xdata(x)
p.set_ydata(x**2)
ax.title.set_text('Graph for y = x**2')
plt.draw()
btn2.on_clicked(plot2)
# Next button
axButn3 = plt.axes([0.5, 0.1, 0.1, 0.1])
btn3 = Button(
axButn3, label="Next", color='pink', hovercolor='tomato')
# To plot a graph for y = 2x
def plot3(event):
p.set_xdata(x)
p.set_ydata(2*x)
ax.title.set_text('Graph for y = 2x')
plt.draw()
btn3.on_clicked(plot3)
plt.show()
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 = x^2 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
Output:
Similar Reads
Matplotlib - Button Widget In Matplotlib a button is one of the important widgets by which we can perform various operations. They are mostly used for making a good functional graph having different properties. There are three types of buttons ButtonRadio ButtonsCheck Buttons In this article, we will learn how to use differen
4 min read
Matplotlib - Radio Buttons Radio buttons let the user choose only one option between multiple options. These buttons are arranged in groups of two or more with a list of circular dots. Â For the radio buttons to remain responsive you must keep a reference to this object. Â We connect the RadioButtons with the on_clicked method
3 min read
Customizing Styles in Matplotlib Here, we'll delve into the fundamentals of Matplotlib, exploring its various classes and functionalities to help you unleash the full potential of your data visualization projects. From basic plotting techniques to advanced customization options, this guide will equip you with the knowledge needed t
12 min read
How to create buttons in Jupyter? In this article, we will learn How to create an interactive button in Jupyter using Python Program. An Interactive Button is a button which when clicked by a user performs a specific function. For the following task, we need the ipywidgets library module. ipywidgets, also known as jupyter-widgets or
1 min read
How to make Custom Buttons in Plotly? A Plotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization libra
2 min read
Create Scatter Charts in Matplotlib using Flask In this article, we will see how to create charts in Matplotlib with Flask. We will discuss two different ways how we can create Matplotlib charts in Flask and present it on an HTML webpage with or without saving the plot using Python. File structure  Create and Save the Plot in the Static Directory
4 min read
Matplotlib.pyplot.connect() in Python Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. matplotlib.pyplot.connect() Function This method is used to connect an event with string s to a function.
3 min read
Matplotlib.axes.Axes.plot() in Python Axes.plot() method in Matplotlib is used to plot data on a set of axes. It is primarily used for creating line plots but can be extended for other types of plots, including scatter plots, bar plots, and more. When using this function, you typically provide the x and y coordinates of the data points
3 min read
Matplotlib.pyplot.get_plot_commands() in Python Matplotlib is a library in Python and it is numerical â mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. There are various plots which can be used in Pyplot are Line Plot, Contour, Histogram, Scatter, 3D Plot,
2 min read
Introduction to Matplotlib Matplotlib is a powerful and versatile open-source plotting library for Python, designed to help users visualize data in a variety of formats. Developed by John D. Hunter in 2003, it enables users to graphically represent data, facilitating easier analysis and understanding. If you want to convert y
4 min read