如何使用matplotlib在颜色栏中显示所有间隔 [英] how to display the all intervals in colorbar using matplotlib

查看:62
本文介绍了如何使用matplotlib在颜色栏中显示所有间隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码.我在这里提到了50个间隔,当我拖动滑块时,只有6个或7个间隔,但是我想在我的颜色栏中显示所有间隔.所以任何人都可以请我指导.谢谢.

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()

推荐答案

Slider 的参数称为 valfmt 应该是用于格式化滑块值的字符串.

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

因此,如果要在浮点数上显示2个小数位,则需要使 c_max =%1.2f" .请注意,如果要将最小值保持为0,也需要在 img.set_clim(0,_cmax)

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()

这篇关于如何使用matplotlib在颜色栏中显示所有间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆