0

I am trying to create a plot with vertical bars equal to score length, AND with a radiobutton which enables the user to choose which level scores he/she wants to see, as such: Button 1: Shows the pointscores of John and Daniel in level 1. Button 2: Shows the pointscores of John and Daniel in level 2. Button 3: Shows the pointscores of John and Daniel in level 3. ... And so on. If you run the code the idea of the design should be clear.

My general problem is how to connect the radiobutton to the values of the items, in this example called "level1", "level2" and "level3" (lines 19, 20 & 21). All I have as a scabelon is a color-changer (disabled), from Matplotlib.

I have tried a lot of googeling, but are only able to come up with answers of how to DESIGN the radiobutton itself, instead of HOW TO CONNECT it to my input.

I hope that the question is clear. Thank you

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button, RadioButtons

john_level1 = 10
john_level2 = 2
john_level3 = 18

daniel_level1 = 11
daniel_level2 = 0
daniel_level3 = 6

fig, ax = plt.subplots()
plt.subplots_adjust(left=0.35, bottom=0.25)

people = ("John", "Daniel")
y_pos = np.arange(len(people))

level1 = john_level1, daniel_level1
level2 = john_level2, daniel_level2
level3 = john_level3, daniel_level3


plt.axis([0, 12, -1, 2])

axcolor = 'lightgoldenrodyellow'

plt.barh(y_pos, level1, align='center', alpha=0.5)
plt.yticks(y_pos, people)
plt.title('Individuel Points', fontsize=14, fontweight='bold')

#################
# RADIO BUTTONS # Originally a color-changer from Matplotlib
#################
rax = plt.axes([0.015, 0.45, 0.25, 0.25], axisbg=axcolor)
radio = RadioButtons(rax, ('Score at Level 1', 'Score at Level 2', 'Score at       Level 3'), active=0)
def raidfunc(label):
    #    l.set_color(label)
    fig.canvas.draw_idle()
radio.on_clicked(raidfunc)

plt.show()
ToFo
  • 1,643
  • 1
  • 19
  • 34

1 Answers1

0

Alright, so I think I got it to work. Try this:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button, RadioButtons

john_level1 = 10
john_level2 = 2
john_level3 = 18

daniel_level1 = 11
daniel_level2 = 0
daniel_level3 = 6

# plt.subplots_adjust(left=0.35, bottom=0.25)

people = ("John", "Daniel")
y_pos = np.arange(len(people))

level1 = john_level1, daniel_level1
level2 = john_level2, daniel_level2
level3 = john_level3, daniel_level3

fig = plt.figure()
ax = fig.add_subplot(111)


ax.axis([0, 12, -1, 2])

axcolor = 'lightgoldenrodyellow'


ax.barh(y_pos, level1, align='center', alpha=0.5)
ax.set_yticks(y_pos, people)
ax.set_title('Individual Points', fontsize=14, fontweight='bold')

#################
# RADIO BUTTONS # Originally a color-changer from Matplotlib
#################
rax = fig.add_axes([0.015, 0.75, 0.25, 0.25], axisbg=axcolor)
radio = RadioButtons(rax, ('Score at Level 1', 'Score at Level 2', 'Score at Level 3'), active=0)
def raidfunc(label):

    if (label == 'Score at Level 1'):
        ax.clear()
        ax.axis([0, 12, -1, 2])
        ax.barh(y_pos, level1, align='center', alpha=0.5)
        fig.canvas.draw()

    if (label == 'Score at Level 2'):
        ax.clear()
        ax.axis([0, 12, -1, 2])
        ax.barh(y_pos, level2, align='center', alpha=0.5)
        fig.canvas.draw()

    if (label == 'Score at Level 3'):
        ax.clear()
        ax.axis([0, 12, -1, 2])
        ax.barh(y_pos, level3, align='center', alpha=0.5)
        fig.canvas.draw()

radio.on_clicked(raidfunc)

plt.show()

I changed two things, basically:

  1. Used the fig, ax and add_subplot syntax to make for easier plot manipulation
  2. With this, I can clear the subplot containing your data (without ruining the entire plot), replot the axes (otherwise it gets all messed up and fills the plot with your data. I guess you don't want that) and replot the data

There should be a more elegant solution to this involving a function similar to set_ydata, but that particular one doesn't seem to work for barh. The gist is that you should be able to do it without replotting, which is faster, but this seems to work. At least for me.

I hope this helps!

Also, inspiration : How to update a plot in matplotlib?

Community
  • 1
  • 1
Gijs
  • 84
  • 10