<bdo id='N1bLk'></bdo><ul id='N1bLk'></ul>

  1. <small id='N1bLk'></small><noframes id='N1bLk'>

  2. <i id='N1bLk'><tr id='N1bLk'><dt id='N1bLk'><q id='N1bLk'><span id='N1bLk'><b id='N1bLk'><form id='N1bLk'><ins id='N1bLk'></ins><ul id='N1bLk'></ul><sub id='N1bLk'></sub></form><legend id='N1bLk'></legend><bdo id='N1bLk'><pre id='N1bLk'><center id='N1bLk'></center></pre></bdo></b><th id='N1bLk'></th></span></q></dt></tr></i><div id='N1bLk'><tfoot id='N1bLk'></tfoot><dl id='N1bLk'><fieldset id='N1bLk'></fieldset></dl></div>

      <legend id='N1bLk'><style id='N1bLk'><dir id='N1bLk'><q id='N1bLk'></q></dir></style></legend>
      <tfoot id='N1bLk'></tfoot>

    1. 我可以让 matplotlib 滑块更加离散吗?

      Can I make matplotlib sliders more discrete?(我可以让 matplotlib 滑块更加离散吗?)
        • <bdo id='gZtdx'></bdo><ul id='gZtdx'></ul>

          <i id='gZtdx'><tr id='gZtdx'><dt id='gZtdx'><q id='gZtdx'><span id='gZtdx'><b id='gZtdx'><form id='gZtdx'><ins id='gZtdx'></ins><ul id='gZtdx'></ul><sub id='gZtdx'></sub></form><legend id='gZtdx'></legend><bdo id='gZtdx'><pre id='gZtdx'><center id='gZtdx'></center></pre></bdo></b><th id='gZtdx'></th></span></q></dt></tr></i><div id='gZtdx'><tfoot id='gZtdx'></tfoot><dl id='gZtdx'><fieldset id='gZtdx'></fieldset></dl></div>

            <small id='gZtdx'></small><noframes id='gZtdx'>

            <tfoot id='gZtdx'></tfoot>
                <legend id='gZtdx'><style id='gZtdx'><dir id='gZtdx'><q id='gZtdx'></q></dir></style></legend>
                  <tbody id='gZtdx'></tbody>
              1. 本文介绍了我可以让 matplotlib 滑块更加离散吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在使用 matplotlib 滑块,类似于 这个演示.滑块目前使用 2 个小数位,并且感觉"非常连续(尽管它们在某种程度上必须是离散的).我可以决定它们是离散的吗?整数步数?0.1 步长?0.5?我的 google-fu 让我失望了.

                I'm using matplotlib sliders, similar to this demo. The sliders currently use 2 decimal places and 'feel' quite continuous (though they have to be discrete on some level). Can I decide on what level they are discrete? Integer steps? 0.1-sized steps? 0.5? My google-fu failed me.

                推荐答案

                如果您只需要整数值,只需在创建滑块时传入适当的 valfmt(例如 valfmt='%0.0f')

                If you just want integer values, just pass in an approriate valfmt when you create the slider (e.g. valfmt='%0.0f')

                但是,如果您想要非整数 invervals,则每次都需要手动设置文本值.但是,即使您这样做了,滑块仍会平稳前进,并且不会感觉"像离散间隔.

                However, if you want non-integer invervals, you'll need to manually set the text value each time. Even if you do this, though, the slider will still progress smoothly, and it won't "feel" like discrete intervals.

                这是一个例子:

                import matplotlib.pyplot as plt
                import numpy as np
                from matplotlib.widgets import Slider
                
                class ChangingPlot(object):
                    def __init__(self):
                        self.inc = 0.5
                
                        self.fig, self.ax = plt.subplots()
                        self.sliderax = self.fig.add_axes([0.2, 0.02, 0.6, 0.03],
                                                          axisbg='yellow')
                
                        self.slider = Slider(self.sliderax, 'Value', 0, 10, valinit=self.inc)
                        self.slider.on_changed(self.update)
                        self.slider.drawon = False
                
                        x = np.arange(0, 10.5, self.inc)
                        self.ax.plot(x, x, 'ro')
                        self.dot, = self.ax.plot(self.inc, self.inc, 'bo', markersize=18)
                
                    def update(self, value):
                        value = int(value / self.inc) * self.inc
                        self.dot.set_data([[value],[value]])
                        self.slider.valtext.set_text('{}'.format(value))
                        self.fig.canvas.draw()
                
                    def show(self):
                        plt.show()
                
                p = ChangingPlot()
                p.show()
                

                如果你想让滑块感觉"完全像离散值,你可以继承 matplotlib.widgets.Slider.按键效果由 Slider.set_val

                If you wanted to make the slider "feel" completely like discrete values, you could subclass matplotlib.widgets.Slider. The key effect is controlled by Slider.set_val

                在这种情况下,你会这样做:

                In that case, you'd do something like this:

                class DiscreteSlider(Slider):
                    """A matplotlib slider widget with discrete steps."""
                    def __init__(self, *args, **kwargs):
                        """Identical to Slider.__init__, except for the "increment" kwarg.
                        "increment" specifies the step size that the slider will be discritized
                        to."""
                        self.inc = kwargs.pop('increment', 0.5)
                        Slider.__init__(self, *args, **kwargs)
                
                    def set_val(self, val):
                        discrete_val = int(val / self.inc) * self.inc
                        # We can't just call Slider.set_val(self, discrete_val), because this 
                        # will prevent the slider from updating properly (it will get stuck at
                        # the first step and not "slide"). Instead, we'll keep track of the
                        # the continuous value as self.val and pass in the discrete value to
                        # everything else.
                        xy = self.poly.xy
                        xy[2] = discrete_val, 1
                        xy[3] = discrete_val, 0
                        self.poly.xy = xy
                        self.valtext.set_text(self.valfmt % discrete_val)
                        if self.drawon: 
                            self.ax.figure.canvas.draw()
                        self.val = val
                        if not self.eventson: 
                            return
                        for cid, func in self.observers.iteritems():
                            func(discrete_val)
                

                作为使用它的完整示例:

                And as a full example of using it:

                import matplotlib.pyplot as plt
                import numpy as np
                from matplotlib.widgets import Slider
                
                class ChangingPlot(object):
                    def __init__(self):
                        self.inc = 0.5
                
                        self.fig, self.ax = plt.subplots()
                        self.sliderax = self.fig.add_axes([0.2, 0.02, 0.6, 0.03],
                                                          facecolor='yellow')
                
                        self.slider = DiscreteSlider(self.sliderax, 'Value', 0, 10, 
                                                     increment=self.inc, valinit=self.inc)
                        self.slider.on_changed(self.update)
                
                        x = np.arange(0, 10.5, self.inc)
                        self.ax.plot(x, x, 'ro')
                        self.dot, = self.ax.plot(self.inc, self.inc, 'bo', markersize=18)
                
                    def update(self, value):
                        self.dot.set_data([[value],[value]])
                        self.fig.canvas.draw()
                
                    def show(self):
                        plt.show()
                
                class DiscreteSlider(Slider):
                    """A matplotlib slider widget with discrete steps."""
                    def __init__(self, *args, **kwargs):
                        """Identical to Slider.__init__, except for the "increment" kwarg.
                        "increment" specifies the step size that the slider will be discritized
                        to."""
                        self.inc = kwargs.pop('increment', 0.5)
                        Slider.__init__(self, *args, **kwargs)
                        self.val = 1
                
                    def set_val(self, val):
                        discrete_val = int(val / self.inc) * self.inc
                        # We can't just call Slider.set_val(self, discrete_val), because this 
                        # will prevent the slider from updating properly (it will get stuck at
                        # the first step and not "slide"). Instead, we'll keep track of the
                        # the continuous value as self.val and pass in the discrete value to
                        # everything else.
                        xy = self.poly.xy
                        xy[2] = discrete_val, 1
                        xy[3] = discrete_val, 0
                        self.poly.xy = xy
                        self.valtext.set_text(self.valfmt % discrete_val)
                        if self.drawon: 
                            self.ax.figure.canvas.draw()
                        self.val = val
                        if not self.eventson: 
                            return
                        for cid, func in self.observers.items():
                            func(discrete_val)
                
                
                p = ChangingPlot()
                p.show()
                

                这篇关于我可以让 matplotlib 滑块更加离散吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Running .jl file from R or Python(从 R 或 Python 运行 .jl 文件)
                Running Julia .jl file in python(在 python 中运行 Julia .jl 文件)
                Using PIP in a Azure WebApp(在 Azure WebApp 中使用 PIP)
                How to run python3.7 based flask web api on azure(如何在 azure 上运行基于 python3.7 的烧瓶 web api)
                Azure Python Web App Internal Server Error(Azure Python Web 应用程序内部服务器错误)
                Run python dlib library on azure app service(在 azure app 服务上运行 python dlib 库)

                <small id='sBGNK'></small><noframes id='sBGNK'>

                1. <i id='sBGNK'><tr id='sBGNK'><dt id='sBGNK'><q id='sBGNK'><span id='sBGNK'><b id='sBGNK'><form id='sBGNK'><ins id='sBGNK'></ins><ul id='sBGNK'></ul><sub id='sBGNK'></sub></form><legend id='sBGNK'></legend><bdo id='sBGNK'><pre id='sBGNK'><center id='sBGNK'></center></pre></bdo></b><th id='sBGNK'></th></span></q></dt></tr></i><div id='sBGNK'><tfoot id='sBGNK'></tfoot><dl id='sBGNK'><fieldset id='sBGNK'></fieldset></dl></div>

                        <bdo id='sBGNK'></bdo><ul id='sBGNK'></ul>
                        <tfoot id='sBGNK'></tfoot>
                          <tbody id='sBGNK'></tbody>

                        <legend id='sBGNK'><style id='sBGNK'><dir id='sBGNK'><q id='sBGNK'></q></dir></style></legend>