我有一个颜色条,有几个颜色块。如何找到每个颜色段的宽度,以便我可以将其打印到其他地方。 我已经尝试过mouseevent.ydata等,但只输出你点击过的地方。有没有一种方法可以获得颜色分区的宽度? 提前致谢
答案 0 :(得分: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>
然后我只是查看了documentation for 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)
我不知道这些数字在哪个坐标空间。遗憾。