Skip to content

NavigationToolbar2Kivy doesnt load the buttons only the toolbar #7

@Shubhamm33

Description

@Shubhamm33

Describe the bug
NavigationToolbar2Kivy not working with matplotlib versions
3.7.2, 3.7.3, 3.7.4, 3.7.5, 3.8.0rc, 3.8.1, .....3.8.4, 3.9.0rc2
tried this with all

To Reproduce
Code example showing the issue:
tried it with this code from the example itself
examples/backend.py

import matplotlib

matplotlib.use("module://kivy_garden.matplotlib.backend_kivy")
from matplotlib.figure import Figure
from numpy import arange, sin, pi
from kivy.app import App

import numpy as np
from kivy_garden.matplotlib.backend_kivyagg import (
    FigureCanvas,
    NavigationToolbar2Kivy,
)
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.boxlayout import BoxLayout
from matplotlib.transforms import Bbox
from kivy.uix.button import Button
from kivy.graphics import Color, Line, Rectangle

import matplotlib.pyplot as plt


def press(event):
    print("press released from test", event.x, event.y, event.button)


def release(event):
    print("release released from test", event.x, event.y, event.button)


def keypress(event):
    print("key down", event.key)


def keyup(event):
    print("key up", event.key)


def motionnotify(event):
    print("mouse move to ", event.x, event.y)


def resize(event):
    print("resize from mpl ", event.width, event.height)


def scroll(event):
    print("scroll event from mpl ", event.x, event.y, event.step)


def figure_enter(event):
    print("figure enter mpl")


def figure_leave(event):
    print("figure leaving mpl")


def close(event):
    print("closing figure")


N = 5
menMeans = (20, 35, 30, 35, 27)
menStd = (2, 3, 4, 1, 2)

ind = np.arange(N)  # the x locations for the groups
width = 0.35  # the width of the bars

fig, ax = plt.subplots()
fig2, ax2 = plt.subplots()
rects1 = ax.bar(ind, menMeans, width, color="r", yerr=menStd)

womenMeans = (25, 32, 34, 20, 25)
womenStd = (3, 5, 2, 3, 3)
rects2 = ax.bar(ind + width, womenMeans, width, color="y", yerr=womenStd)

# add some text for labels, title and axes ticks
ax.set_ylabel("Scores")
ax.set_title("Scores by group and gender")
ax.set_xticks(ind + width)
ax.set_xticklabels(("G1", "G2", "G3", "G4", "G5"))
ax.legend((rects1[0], rects2[0]), ("Men", "Women"))

rects2 = ax2.bar(ind + width, womenMeans, width, color="y", yerr=womenStd)


def autolabel(rects):
    # attach some text labels
    for rect in rects:
        height = rect.get_height()
        ax.text(
            rect.get_x() + rect.get_width() / 2.0,
            1.05 * height,
            "%d" % int(height),
            ha="center",
            va="bottom",
        )


fig.canvas.mpl_connect("button_press_event", press)
fig.canvas.mpl_connect("button_release_event", release)
fig.canvas.mpl_connect("key_press_event", keypress)
fig.canvas.mpl_connect("key_release_event", keyup)
fig.canvas.mpl_connect("motion_notify_event", motionnotify)
fig.canvas.mpl_connect("resize_event", resize)
fig.canvas.mpl_connect("scroll_event", scroll)
fig.canvas.mpl_connect("figure_enter_event", figure_enter)
fig.canvas.mpl_connect("figure_leave_event", figure_leave)
fig.canvas.mpl_connect("close_event", close)

fig2.canvas.mpl_connect("button_press_event", press)
fig2.canvas.mpl_connect("button_release_event", release)
fig2.canvas.mpl_connect("key_press_event", keypress)
fig2.canvas.mpl_connect("key_release_event", keyup)
fig2.canvas.mpl_connect("motion_notify_event", motionnotify)
fig2.canvas.mpl_connect("resize_event", resize)
fig2.canvas.mpl_connect("scroll_event", scroll)
fig2.canvas.mpl_connect("figure_enter_event", figure_enter)
fig2.canvas.mpl_connect("figure_leave_event", figure_leave)
fig2.canvas.mpl_connect("close_event", close)

canvas = fig.canvas
# canvas.blit(Bbox(np.array([[0, 0], [400, 400]], np.int32)))
canvas2 = fig2.canvas


def callback(instance):
    autolabel(rects1)
    autolabel(rects2)
    canvas.draw()


class MatplotlibTest(App):
    title = "Matplotlib Test"

    def build(self):
        fl = BoxLayout(orientation="vertical")
        a = Button(text="press me", height=40, size_hint_y=None)
        a.bind(on_press=callback)
        nav1 = NavigationToolbar2Kivy(canvas)
        nav2 = NavigationToolbar2Kivy(canvas2)
        fl.add_widget(nav1.actionbar)
        fl.add_widget(canvas)
        fl.add_widget(nav2.actionbar)
        fl.add_widget(canvas2)
        fl.add_widget(a)
        return fl


if __name__ == "__main__":
    MatplotlibTest().run()

Expected behavior
to have buttons for toolbar in matplotlib, zoom, pan, etc.

Logs/output
image

Platform (please complete the following information):

  • OS: windows 11
  • Python version 3.10.8

Additional context
my project: "Audio waveform generator with other audio editing functionalities for a client"
been stuck since 4 days, tried with
kivy.garden.matplotlib then later found out not using that anymore and shifted to this
then read some docs and comments
found out about kivy_matplotlib_widget (thats malfunctioning as well in my project)
toolbar buttons not properly binded
and now i'm trying out this part.. need help if anyone has solved this?
saw that someone pushed to pypi
do i need to update my python version ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions