我有一个颜色条,它有几个颜色块。我怎样才能找到每个颜色段的宽度,然后我可以在其他地方打印出来。我试过 mouseevent.ydata 等,但只输出你点击的地方。有没有办法可以得到色段的宽度?提前致谢
问问题
241 次
1 回答
0
这是 matplotlib 站点中一个演示的修改示例,它还打印出绘制颜色条的路径。
使用iPython
其方便的自动完成功能,您可以找出类的成员;
cb.[Press Tab]
cb.add_checker cb.extend cb.set_cmap
cb.add_lines cb.filled cb.set_colorbar
cb.alpha cb.formatter cb.set_label
cb.autoscale cb.get_array cb.set_norm
cb.autoscale_None cb.get_clim cb.set_ticklabels
cb.ax cb.get_cmap cb.set_ticks
cb.boundaries cb.lines cb.solids
cb.callbacksSM cb.locator cb.spacing
cb.changed cb.mappable cb.to_rgba
cb.check_update cb.norm cb.update_bruteforce
cb.cmap cb.orientation cb.update_dict
cb.colorbar cb.outline cb.update_normal
cb.config_axis cb.patch cb.update_ticks
cb.dividers cb.set_alpha cb.values
cb.draw_all cb.set_array cb.vmax
cb.drawedges cb.set_clim cb.vmin
所以我只是尝试了这些。solids
似乎最有希望。
In [53]: print cb.solids
<matplotlib.collections.QuadMesh object at 0xb1a620c>
然后我只是查看了Quadmesh 的文档,发现了一些看起来很有希望的东西。
#!/usr/bin/env python
"""
Use a pcolor or imshow with a custom colormap to make a contour plot.
Since this example was initially written, a proper contour routine was
added to matplotlib - see contour_demo.py and
http://matplotlib.sf.net/matplotlib.pylab.html#-contour.
"""
from pylab import *
delta = 0.01
x = arange(-3.0, 3.0, delta)
y = arange(-3.0, 3.0, delta)
X,Y = meshgrid(x, y)
Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
Z = Z2 - Z1 # difference of Gaussians
cmap = cm.get_cmap('PiYG', 11) # 11 discrete colors
im = imshow(Z, cmap=cmap, interpolation='bilinear',
vmax=abs(Z).max(), vmin=-abs(Z).max())
#axis('off')
cb = colorbar()
for path in cb.solids.get_paths():
print path
show()
然后是终端输出:
Path([[ 0. 0. ]
[ 1. 0. ]
[ 1. 0.09090909]
[ 0. 0.09090909]
[ 0. 0. ]], None)
Path([[ 0. 0.09090909]
[ 1. 0.09090909]
[ 1. 0.18181818]
[ 0. 0.18181818]
[ 0. 0.09090909]], None)
Path([[ 0. 0.18181818]
[ 1. 0.18181818]
[ 1. 0.27272727]
[ 0. 0.27272727]
[ 0. 0.18181818]], None)
Path([[ 0. 0.27272727]
[ 1. 0.27272727]
[ 1. 0.36363636]
[ 0. 0.36363636]
[ 0. 0.27272727]], None)
Path([[ 0. 0.36363636]
[ 1. 0.36363636]
[ 1. 0.45454545]
[ 0. 0.45454545]
[ 0. 0.36363636]], None)
Path([[ 0. 0.45454545]
[ 1. 0.45454545]
[ 1. 0.54545455]
[ 0. 0.54545455]
[ 0. 0.45454545]], None)
Path([[ 0. 0.54545455]
[ 1. 0.54545455]
[ 1. 0.63636364]
[ 0. 0.63636364]
[ 0. 0.54545455]], None)
Path([[ 0. 0.63636364]
[ 1. 0.63636364]
[ 1. 0.72727273]
[ 0. 0.72727273]
[ 0. 0.63636364]], None)
Path([[ 0. 0.72727273]
[ 1. 0.72727273]
[ 1. 0.81818182]
[ 0. 0.81818182]
[ 0. 0.72727273]], None)
Path([[ 0. 0.81818182]
[ 1. 0.81818182]
[ 1. 0.90909091]
[ 0. 0.90909091]
[ 0. 0.81818182]], None)
Path([[ 0. 0.90909091]
[ 1. 0.90909091]
[ 1. 1. ]
[ 0. 1. ]
[ 0. 0.90909091]], None)
我不知道这些数字在什么坐标空间上。对不起。
于 2013-02-20T12:12:53.283 回答