Python Forum
How to use rangesliders feature from matplotlib in tkinter?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to use rangesliders feature from matplotlib in tkinter?
#1
I have written a code to use the RangeSldider widget to have control over my axis range.
from tkinter import *
import numpy as np
import matplotlib
matplotlib.use('TkAgg')
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib import cm
from matplotlib.colors import ListedColormap
from RangeSlider.RangeSlider import RangeSliderH, RangeSliderV

root = Tk()
root.geometry("600x600")

def plot():
    x, y = np.mgrid[slice(0, 6, 1), slice(0, 6, 1)]
    z = np.arange(1,26).reshape(5,5)
    figure = Figure(figsize=(4, 4))
    ax = figure.add_subplot(111)

    col_type = cm.get_cmap('rainbow', 256)
    newcolors = col_type(np.linspace(0, 1, 1000))
    white = np.array([1, 1, 1, 1])
    newcolors[:1, :] = white
    newcmp = ListedColormap(newcolors)

    c = ax.pcolormesh(x, y, z, cmap=newcmp, edgecolor='lightgrey', linewidth=0.003)
    ax.figure.colorbar(c)

    ax.set_title('mY Title', fontweight="bold")
    ax.set_xlabel("X", fontsize=14)
    ax.set_ylabel("Y", fontsize=14)

    canvas = FigureCanvasTkAgg(figure, root)
    canvas.get_tk_widget().place(x=100, y=25)
    figure.patch.set_facecolor('#f0f0f0')

    ax.set_xlim(rs1.getValues())
    ax.set_ylim(rs2.getValues())

hVar1 = IntVar() # left handle variable
hVar2 = IntVar()  # right handle variable
rs1 = RangeSliderH(root, [hVar1, hVar2], Width=230, Height=55, padX=17, min_val=0, max_val=5, font_size=12,\
                   show_value=True, digit_precision='.0f', bgColor='#f0f0f0', line_s_color='black',\
                   line_color='black', bar_color_inner='black', bar_color_outer='#f0f0f0')
rs1.place(x=150, y=420)

vVar1 = IntVar()  # top handle variable
vVar2 = IntVar()  # down handle variable
rs2 = RangeSliderV(root, [vVar1, vVar2], Width=81, Height=180, padY=11, min_val=0, max_val=5, font_size=12,\
                   show_value=True, digit_precision='.0f', bgColor='#f0f0f0', line_s_color='black',\
                   line_color='black', bar_color_inner='black', bar_color_outer='#f0f0f0')
rs2.place(x=0, y=150)

button = Button(root, text="Plot", command=plot)
button.pack()

root.mainloop()
I got a suggestion to use the range slider feature provided by matplotlib. I searched in the libraries, and used the FloatRangeSlider. But I don't know how to add the range sliders to show on the canvas and make it work and update the figure instantly.

from tkinter import *
import numpy as np
import matplotlib
matplotlib.use('TkAgg')
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.colors import ListedColormap

import ipywidgets as widgets

root = Tk()
root.geometry("600x600")

def plot():
    x, y = np.mgrid[slice(0, 6, 1), slice(0, 6, 1)]
    z = np.arange(1,26).reshape(5,5)
    figure = Figure(figsize=(8, 8))
    ax = figure.add_subplot(111)

    col_type = cm.get_cmap('rainbow', 256)
    newcolors = col_type(np.linspace(0, 1, 1000))
    white = np.array([1, 1, 1, 1])
    newcolors[:1, :] = white
    newcmp = ListedColormap(newcolors)

    c = ax.pcolormesh(x, y, z, cmap=newcmp, edgecolor='lightgrey', linewidth=0.003)
    ax.figure.colorbar(c)

    ax.set_title('mY Title', fontweight="bold")
    ax.set_xlabel("X", fontsize=14)
    ax.set_ylabel("Y", fontsize=14)

    sliderH = widgets.FloatRangeSlider(value=(0, 5), min=0, max=5, step=1, orientation='horizontal')
    sliderV = widgets.FloatRangeSlider(value=(0, 5), min=0, max=5, step=1, orientation='vertical')

    canvas = FigureCanvasTkAgg(figure, root)
    canvas.get_tk_widget().pack()

    def update():
        x, y = np.mgrid[slice(sliderH.value[0], sliderH.value[1], 1), slice(sliderV.value[0], sliderV.value[1], 1)]
        figure.canvas.draw()

    sliderH.observe(update, names='value')
    sliderV.observe(update, names='value')
    display(sliderH)
    display(sliderV)

button = Button(root, text="Plot", command=plot)
button.pack()

root.mainloop()
Reply
#2
there is an example here: https://matplotlib.org/stable/gallery/wi...angeslider
Reply
#3
(Feb-28-2022, 11:47 AM)Larz60+ Wrote: there is an example here: https://matplotlib.org/stable/gallery/wi...angeslider

I think my problem is that this ipywidgets library is for jupyter notebook. I am not using jupyter.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Interaction between Matplotlib window, Python prompt and TKinter window NorbertMoussy 3 540 Mar-17-2024, 09:37 AM
Last Post: deanhystad
  [Tkinter] Tkinter Matplotlib Animation Graph not rendering dimidgen 3 636 Mar-12-2024, 02:09 PM
Last Post: deanhystad
  Tkinter Matplotlib Nepo 1 2,478 Sep-27-2020, 10:20 AM
Last Post: Gribouillis
  Tkinter & matplotlib PillyChepper 9 5,734 Nov-23-2019, 10:36 AM
Last Post: PillyChepper
  Making a Matplotlib in Tkinter using a slider Jemeronimo 1 5,695 Dec-05-2018, 08:06 AM
Last Post: Gribouillis
  how to install numpy and matplotlib to Tkinter elwolv 4 4,791 Dec-02-2017, 06:17 PM
Last Post: Barrowman

Forum Jump:

User Panel Messages

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