在matplotlib中水平显示单选按钮

阿南德·辛格

我正在使用matplotlib.widgets来在我的小部件中创建单选按钮,即将出现的按钮是垂直堆叠的,我希望它们是水平堆叠的。

MVCE:

import matplotlib.pyplot as plt
from matplotlib.widgets import RadioButtons
plt.subplots_adjust(left=0.2)
rax = plt.axes([0.5,0.05,0.1,0.1])
radio =  RadioButtons(rax ,['1','2','3'], active=0, activecolor='blue' )
plt.show()

如您在本示例中看到的,您可以获取如下单选按钮单选按钮垂直堆叠的图

我想知道是否有一种方法可以水平堆叠这些单选按钮。

认真的重要性

当前尝试PR#13374中引入一个orientation论点尚未完成。RadioButtons

正如我在此PR中评论的那样,另一种选择是对按钮使用散点图。下面显示了我如何想象此实现。与常规按钮相比,有两个主要增强功能:

  • 单选按钮始终是圆形的,与轴的大小无关。
  • 它们可以任意对准,尤其是水平对准。

这是通过在内部创建图例来实现的,该图例具有所有可用的必需选项。任何有效的参数Legend也可以用于Button。

import matplotlib.pyplot as plt
from matplotlib.widgets import AxesWidget, RadioButtons

class MyRadioButtons(RadioButtons):

    def __init__(self, ax, labels, active=0, activecolor='blue', size=49,
                 orientation="vertical", **kwargs):
        """
        Add radio buttons to an `~.axes.Axes`.
        Parameters
        ----------
        ax : `~matplotlib.axes.Axes`
            The axes to add the buttons to.
        labels : list of str
            The button labels.
        active : int
            The index of the initially selected button.
        activecolor : color
            The color of the selected button.
        size : float
            Size of the radio buttons
        orientation : str
            The orientation of the buttons: 'vertical' (default), or 'horizontal'.
        Further parameters are passed on to `Legend`.
        """
        AxesWidget.__init__(self, ax)
        self.activecolor = activecolor
        axcolor = ax.get_facecolor()
        self.value_selected = None

        ax.set_xticks([])
        ax.set_yticks([])
        ax.set_navigate(False)

        circles = []
        for i, label in enumerate(labels):
            if i == active:
                self.value_selected = label
                facecolor = activecolor
            else:
                facecolor = axcolor
            p = ax.scatter([],[], s=size, marker="o", edgecolor='black',
                           facecolor=facecolor)
            circles.append(p)
        if orientation == "horizontal":
            kwargs.update(ncol=len(labels), mode="expand")
        kwargs.setdefault("frameon", False)    
        self.box = ax.legend(circles, labels, loc="center", **kwargs)
        self.labels = self.box.texts
        self.circles = self.box.legendHandles
        for c in self.circles:
            c.set_picker(5)
        self.cnt = 0
        self.observers = {}

        self.connect_event('pick_event', self._clicked)


    def _clicked(self, event):
        if (self.ignore(event) or event.mouseevent.button != 1 or
            event.mouseevent.inaxes != self.ax):
            return
        if event.artist in self.circles:
            self.set_active(self.circles.index(event.artist))

用作

plt.subplots_adjust(left=0.2)
rax = plt.axes([0.5,0.05,0.4,0.07])
radio =  MyRadioButtons(rax ,['1','2','3'], active=0, activecolor='crimson',
                        orientation="horizontal")

plt.show()

在此处输入图片说明

要么

rax = plt.axes([0.2,0.5,0.25,0.1])
radio =  MyRadioButtons(rax ,["AA", "BB", "CC", "DD"], ncol=2)

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用ipywidgets水平显示单选按钮

来自分类Dev

表格中的水平中心单选按钮

来自分类Dev

单选按钮水平对齐

来自分类Dev

在Prolog Xpce中显示单选按钮

来自分类Dev

单选按钮在组合框中显示值

来自分类Dev

更改单选按钮列中的显示值

来自分类Dev

如何在python中水平放置单选按钮

来自分类Dev

单选按钮布局以使水平

来自分类Dev

单选按钮显示div

来自分类Dev

单选按钮不显示

来自分类Dev

单选按钮显示div

来自分类Dev

根据字典中显示的属性显示单选按钮

来自分类Dev

获取Matplotlib单选按钮的状态

来自分类Dev

如何显示水平按钮

来自分类Dev

wxPython中的单选按钮显示和选择问题

来自分类Dev

iTunes Connect中未显示Internal Tester单选按钮

来自分类Dev

在android中单击单选按钮时如何显示edittext

来自分类Dev

在IE和firefox中,单选按钮以额外的轮廓显示

来自分类Dev

根据Shiny R中的单选按钮显示滑块输入

来自分类Dev

使用单选按钮显示或隐藏Vue 3中的div

来自分类Dev

显示来自Angularjs中JSON输入的选定单选按钮

来自分类Dev

文本不会显示在文本框中(单选按钮)

来自分类Dev

在android中单击单选按钮时如何显示edittext

来自分类Dev

wxPython中的单选按钮显示和选择问题

来自分类Dev

在tianium中显示单选按钮标签有问题吗?

来自分类Dev

根据单选按钮的选择在HTML表或DataTable中显示数据

来自分类Dev

根据单选按钮显示不同的<tr>(在Knockout中)

来自分类Dev

为什么单选按钮的size()在硒中显示为0?

来自分类Dev

在angularJS中显示来自JSON的选定单选按钮