Home > other >  Could you tell me when adding radiobutton in barh figure, choose a different button how to make the
Could you tell me when adding radiobutton in barh figure, choose a different button how to make the

Time:09-24

As title, I hope that the width of the bar kept 0.5, even if y data changes,
The code below is my combined with the example of official document, hope to be able to select different radiobutton in time, the width of the bar kept 0.5.
Thank you very much!

 
The import numpy as np
The import matplotlib. Pyplot as PLT
The from matplotlib. Widgets import RadioButtons


Category_names=[' Strongly disagree 'and' disagree ',
'Neither something nor disagree' and 'something', 'Strongly something']
Results={
'Question 1: [10, 15, 17, 32, 26],
'Question 2: [26, 22, 29, 10, 13],
'Question 3: [35, 37, 7, 2, 19],
'Question 4: [32, 11, 9, 15, 33],
'Question 5: [40] 21, 29, 5, 5,,
'Question 6: [8, 19, 5, 30, 38]
}



"" "
The Parameters
-- -- -- -- -- -- -- -- -- --
Results: the dict
A mapping from question labels to A list of answers per category.
It is assumed all lists contain the same number of entries and that
It matches the length of * category_names *.
Category_names: list of STR
The category labels.
"" "

Data=https://bbs.csdn.net/topics/np.array (the list (the values ()))

Category_colors=PLT. Get_cmap (' RdYlGn) (
Np. Linspace (0.15, 0.85, the data. The shape [1]))

FIG, ax=PLT. Subplots (figsize=9.2, (6))
Ax. Invert_yaxis ()
Ax. Xaxis. Set_visible (False)
Ax. Set_xlim (0, np. Sum (data, axis=1). The Max ())



Def survey (data, results, category_names) :

For I, (colname, color) in enumerate (zip (category_names category_colors)) :
Labels=list (the keys ())
Widths=data [, I]
Data_cum=data. Cumsum (axis=1)
Starts=data_cum [, I] - widths
Ax. Barh (labels, widths, left=starts, height=0.5,
Label=colname, color=color)
Ax. Set_title (' AACN)
Xcenters=starts + widths/2

R, g, b, _=color
Text_color='white' if r * g * b & lt; 0.5 the else 'darkgrey'
For y, (x, c) in enumerate (zip (xcenters, widths)) :
Ax. Text (x, y, STR (int) (c), ha='center', va='center',
Color=text_color)
Ax. Legend (ncol=len (category_names), bbox_to_anchor=(0, 1),
Loc='the lower left, fontsize=' small ')

Return the FIG, ax


Survey (data, results, category_names)

Axcolor='lightgoldenrodyellow'
Rax=PLT. Axes ([0.9, 0.7, 0.15, 0.15], facecolor=axcolor)
Radio=RadioButtons (rax, (' 2 Hz ', '4 Hz))

Data1={' Barton, LLC: [10, 15, 17, 0, 0],
'Frami, Hills and Schmidt: [8, 19, 5, 30, 38],
'Fritsch, Russell and Anderson: [8, 19, 5, 66, 2)}

Def hzfunc (label) :
Hzdict={' 2 Hz: results, '4 Hz: data1}
Ydata=HTTP://https://bbs.csdn.net/topics/hzdict/label
Data=https://bbs.csdn.net/topics/np.array (list (ydata values ()))
Ax. Cla ()
Survey (data, ydata category_names)
PLT. The draw ()
Radio. On_clicked (hzfunc)

PLT. The show ()
  • Related