Python Forum
MatplotLib Sliders - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: MatplotLib Sliders (/thread-18416.html)



MatplotLib Sliders - punksnotdead - May-16-2019

Wondered if anyone can help. The following code creates a 64x64 heatmap.
I'd like to add two horizontal sliders A and B each scaled from -90 to +90
Next to each slider I'd like to show the value selected on each slider in a label.
I've tried several times but can't seem to get it to work. Thanks

import matplotlib.pyplot as plt
import numpy as np

a = np.random.random((64, 64))

plt.figure('My First GUI')

plt.imshow(a, cmap='gray', interpolation='nearest')
plt.show()



RE: MatplotLib Sliders - DreamingInsanity - May-16-2019

I think this question fits more in the GUI forum.

D

Just to answer this anyway, here is what you wanted:
import matplotlib.pyplot as plt
from pylab import *
from matplotlib.widgets import Slider
import numpy as np

c = 0 
a = np.random.random((64, 64))
 
plt.figure('My First GUI')
plt.imshow(a, cmap='gray', interpolation='nearest')

axcolor = 'lightgoldenrodyellow'
axhte = axes([0.15, 0.04, 0.6, 0.03], facecolor=axcolor) #x,y,w,h
axhre = axes([0.15, 0.01, 0.65, 0.03], facecolor=axcolor)

sl1 = Slider(axhte, 'Slider1', -90, 90, valinit=1)
sl2 = Slider(axhre, 'Slider2', -90, 90, valinit=1)

def get_valS1(val):
    print("value of s1:",sl1.val)

def get_valS2(val):
    print("value of s2:",sl2.val)

sl1.on_changed(get_valS1)
sl2.on_changed(get_valS2)

plt.show()
Have a look here to see where I got it from: https://stackoverflow.com/questions/11069944/add-slider-to-matplotlib-chart

D


RE: MatplotLib Sliders - punksnotdead - May-17-2019

Thanks DreamingInsanity, That works great. Sorry for posting in the wrong forum. I'll just continue here to keep this thread going but keep that in mind for future posts. I have a few comments/questions

1. One of the sliders appears to be longer than the other and i'm not sure why
2. How can i insert some vertical space between the heatmap and the sliders and between the sliders themselves?
3. I'd like the grayscale value of each pixel to change according to the formula:

Grayscale (pix x,y) = x*sin(Slider1 Value) + y* sin(Slider2 Value)

I'm not sure how many grayscale values there are but i'd also like to be able to apply the MOD function to limit the grayscale values

Any help greatly appreciated, Thanks


RE: MatplotLib Sliders - DreamingInsanity - May-17-2019

As for the grayscale, I will work on it soon.

For the spacing if you look in my code on line 12, I have witten the comment '#x,y,w,h'. X is how far right the slider is from the left side of the screen. Y is how far the slider is from the bottom of the screen. To add a gap between two sliders, just change the Y value on them (the second value in the array). The length again is the same as X and Y. My comment mentions W and H meaning width and hight. Adusting these will change the width and height of each slider.

I have tried to experiment with the sliders chaning the grayscale, but I don't have enough experience with matplotlib, so I couldn't get it to work. Sorry!

D


RE: MatplotLib Sliders - punksnotdead - May-18-2019

Hi DreamingInsanity, Thanks for your help. I'll post the second part of the problem on the GUI forum.
I couldn't get the sliders to position neatly by adjusting y (one of them overlaps the x-axis). If anyone can suggest how I can either move the heatmap up or make the window bigger vertically so I can move the sliders down, that would be a really big help Wall Big Grin