10076

Logarithmic slider with matplotlib

<h3>Question</h3>

I just implemented a slider in my plot which works great. (I used this example: http://matplotlib.org/examples/widgets/slider_demo.html) Now my question is if it is possible to make the slider logarithmic. My values range between 0 and 1 and I want to make changes from 0.01 to 0.02 and so on but also from 0.01 to 0.5. That is why I think a logarithmic scale would be nice. Also if this isn't doable with a slider do you then have other ideas how to implement this?


<h3>Answer1:</h3>

You can simply np.log() the value of the slider. However then the label next to it would be incorrect. You need to manually set the text of valtext of the slider to the log-value:

def update(val): amp = np.log(slider.val) slider.valtext.set_text(amp)
<h3>Answer2:</h3>

I know it's been few years but I think it still is useful. The previous answer is simple and straightforward but can be a problem with the inital values not correctly displayed for example. You can directly create a log slider by creating a new class inheriting from the matplotlib slider and edit the function that set the value like so :

from matplotlib.widgets import Slider class Sliderlog(Slider): """Logarithmic slider. Takes in every method and function of the matplotlib's slider. Set slider to *val* visually so the slider still is lineat but display 10**val next to the slider. Return 10**val to the update function (func)""" def set_val(self, val): xy = self.poly.xy if self.orientation == 'vertical': xy[1] = 0, val xy[2] = 1, val else: xy[2] = val, 1 xy[3] = val, 0 self.poly.xy = xy self.valtext.set_text(self.valfmt % 10**val) # Modified to display 10**val instead of val if self.drawon: self.ax.figure.canvas.draw_idle() self.val = val if not self.eventson: return for cid, func in self.observers.items(): func(10**val)

You use it the same way you use the slider but instead of call :

Slider(ax, label, valmin, valmax, valinit=0.5, valfmt='%1.2f', closedmin=True, closedmax=True, slidermin=None, slidermax=None, dragging=True, valstep=None, orientation='horizontal')

Just call :

Sliderlog(ax, label, valmin, valmax, valinit=0.5, valfmt='%1.2f', closedmin=True, closedmax=True, slidermin=None, slidermax=None, dragging=True, valstep=None, orientation='horizontal',

Be careful, if you want to have 10^3 as initial value you have to pass in valinit=3 not 10**3. Same for valmax and valmin. You can use log10(desired_value) if you can not easily type it.

来源:https://stackoverflow.com/questions/39960791/logarithmic-slider-with-matplotlib

Recommend

  • Logarithmic slider with matplotlib
  • Cordova build windows on Windows 10 and VisualStudio 2017
  • Flip a Hex String
  • Internet Explorer Addon Toolbar Button Error
  • Starting a javascript prompt after a button is clicked
  • Convert Hex to Decimal when no datatype can hold the full number
  • Post-receive hook to pull from bare repo
  • ASIO UDP: class std::allocator has no member named ‘construct’
  • SFINAE for class member function (one compiles the other not)
  • How to pass a parameter to an included page with JSF 1.2
  • How to echo values of an associative array returned by function
  • Button On Click event not firing
  • How I can specify how rainbow color scheme should be converted to grayscale
  • Pass multiple lines of stdin input to interactive Java command line program, non-interactively
  • Django non-rel createsuperuser fails due to non-ascii characters
  • Python List of Tuples (Find value with key + check if exist)
  • Python tk scrollbar becomes inactive once text is outside the screen
  • React/Redux. Access state from other component
  • date changes on export kendoGrid
  • connect.cookieParser and connect.session
  • Force fading edge on TextView
  • Align microsoft access queries
  • Calling a flash ExternalInterface in swiffyobject
  • Validating my form with Jquery
  • Add checkbox dynamically using angular 2
  • Query takes almost two seconds but matches only two rows - why isn't the index helping?
  • Excel File upload in asp.net using SqlBulkCopy
  • Year over Year Stats from a Crossfilter Dataset
  • how to run a different select statement based on condition in Hive SQL
  • Ruby regex for matching simpliest Ruby's regexes
  • Problems to understand DXGI DirectX 11 Desktop Duplication to get a Buffer or Array
  • C++ STL stack pop operation giving segmentation fault
  • Why is ordered choice in pyparsing failing for my use case?
  • VSTS work items list through REST API
  • Google App Engine Datastore: Dealing with eventual consistency
  • Bitrate JWplayer
  • Can someone explain this Java code (formatting the output using System.out.format) to me?
  • Access to a Matlab gui from the web