- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to change the curve using radiobuttons in Matplotlib?
To change the color of a line using radiobuttons, we can take the following steps −
Create x, sin and cos data points using numpy.
Adjust the figure size and padding between and around the subplots.
Create a figure and a set of subplots using the subplots() method.
Plot curve with x and y data points using the plot() method.
Add an axes to the current figure and make it the current axes, using the axes() method.
Add a radio button to the current axes.
To change the curve with radionbutton, we can use the change_curve() method that can be passed in on_clicked() method.
To display the figure, use the show() method.
Example
import numpy as np from matplotlib import pyplot as plt from matplotlib.widgets import RadioButtons plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(-2, 2, 50) sin = np.sin(x) cos = np.cos(x) fig, ax = plt.subplots() l, = ax.plot(x, sin, lw=3, color='red') rb_axis = plt.axes([0.15, 0.75, 0.15, 0.15]) radio_button = RadioButtons(rb_axis, ('sin', 'cos')) def change_curve(c_label): d = {'sin': sin, 'cos': cos} data = d[c_label] l.set_ydata(data) plt.draw() radio_button.on_clicked(change_curve) plt.show()
Output
Now, click the radiobutton "cos", it will change the plot −
- Related Articles
- How to change the color of a line using radiobuttons in Matplotlib?
- How to plot sine curve on polar axes using Matplotlib?
- Draw parametrized curve using pyplot.plot() in Matplotlib
- How to fill the area under a step curve using pyplot? (Matplotlib)
- How to animate a sine curve in Matplotlib?
- Draw a parametrized curve using pyplot.plot() in Matplotlib
- How to fill color above the curve in Matplotlib Program?
- How to change the figuresize using Seaborn factorplot in Matplotlib?
- How to fill color below a curve in Matplotlib?
- How to add a cursor to a curve in Matplotlib?
- How to curve text in a polar plot in matplotlib?
- How to remove a specific line or curve in Matplotlib?
- How to change the figsize for matshow() in Jupyter notebook using Matplotlib?
- How to change the text color of font in the legend using Matplotlib?
- How to change the face color of a plot using Matplotlib?

Advertisements