Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
MatplotLib Sliders
#1
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()
Reply
#2
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/1106...tlib-chart

D
Reply
#3
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
Reply
#4
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
Reply
#5
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Matplotlib: How do I convert Dates from Excel to use in Matplotlib JaneTan 1 3,163 Mar-11-2021, 10:52 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020