0

This is my code.I mentioned here 50 intervals,when i drag the slider then only i got 6 or 7 intervals,but i want to display the all my intervals in my colorbar. So can any one please guide me.Thank you in advance.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button
import matplotlib.colors
ax = plt.subplot(111)
plt.subplots_adjust(left=0.25, bottom=0.25)
img_data = np.random.rand(50,50)

c_max = 2
img = ax.imshow(img_data, interpolation='nearest')
cb = plt.colorbar(img)
axcolor = 'lightgoldenrodyellow'
ax_cmax  = plt.axes([0.25, 0.15, 0.65, 0.03])
s_cmax = Slider(ax_cmax, 'max', 0, 50, valfmt=c_max)
def update(val, s=None):
    # _cmin = s_cmin.val
    _cmax = s_cmax.val
    img.set_clim(_cmax)
    plt.draw()
s_cmax.on_changed(update)

plt.show()

enter image description here

1 Answer 1

2

The argument to Slider called valfmt should be a string which is used to format the slider value.

So if you wanted to display 2 decimal places to the float you would need to make c_max = "%1.2f". Note that if you want to keep the minimum value at 0 you need to set that too in img.set_clim(0, _cmax)

c_max = "%1.2f"
img = ax.imshow(img_data, interpolation='nearest')
cb = plt.colorbar(img)
axcolor = 'lightgoldenrodyellow'
ax_cmax  = plt.axes([0.25, 0.15, 0.65, 0.03])
s_cmax = Slider(ax_cmax, 'max', 0, 50, valfmt=c_max)
def update(val, s=None):
    # _cmin = s_cmin.val
    _cmax = s_cmax.val
    img.set_clim(0, _cmax) 
    plt.draw()
s_cmax.on_changed(update)

plt.show()

enter image description here

Sign up to request clarification or add additional context in comments.

9 Comments

thank you somuch for you answer,but sir i want to showed to all intervals here i mentioned 50
I don't understand what you mean by all intervals. Can you exlain in more detail?
when i drag the slider then only my remaning intervals are coming but i want to display the whole intervals in colorbar i.e the problem
yes here s_cmax = Slider(ax_cmax, 'max', 0, 50, valfmt=c_max) here i took 0 to 50 values those values i want to display in colorbar sir
What should the slider do then, if not to adjust the values on the colorbar (and hence the image)?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.