How can I add a slider to my plot to adjust the contrast?

567 views Asked by At

I have a plot that is a representation of a sine wave. I was able to add a slider to manipulate the frequency in real time, and I'd like to add another slider to adjust the contrast of the image. The slider for frequency is on the bottom of the plot - I'd like to add the contrast slider beneath it. So far, the code I've found to manipulate images is for actual pictures. This is a black/white plot that needs to change.

My code is as follows:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm      
from matplotlib.widgets import Slider

vals = np.linspace(-np.pi,np.pi,100)
xgrid, ygrid = np.meshgrid(vals,vals)       

def f(x, y, b):
    return np.sin(x * b)
b = 1

ax = plt.subplot(111)
plt.subplots_adjust(left=0.1, bottom=0.25)
fig = plt.imshow(f(xgrid, ygrid, b), cm.gray)
plt.axis('off')
fig.axes.get_xaxis().set_visible(False)
fig.axes.get_yaxis().set_visible(False)

axb = plt.axes([0.15, 0.1, 0.65, 0.03])
sb = Slider(axb, 'b', 1.0, 65.0, valinit=b)
def update(val):
    fig.set_data(f(xgrid, ygrid, val))
sb.on_changed(update)

plt.show()      

Any ideas?

0

There are 0 answers