Log Scale Matplotlib PatchCollection Colors

こ雲淡風輕ζ 提交于 2019-12-06 07:40:03

Although I can't test your exact case as you've not provided an independently runnable example you should (if my understanding of your desired behaviour is correct) be able to accomplish what you want as follows.

Firstly edit this line to remove the manual setting of the colour and edge information:

patches = [mpl.patches.Rectangle(xy[i], dx[i], dy[i],         # I dont want
                                 ec=None, lw=0, fc=colors[i]) # visible edges.
            for i in range(mesh.shape[0])
          ]

It should look something like this:

patches = [mpl.patches.Rectangle(xy[i], dx[i], dy[i]) for i in range(mesh.shape[0])]

Then pass LogNorm, jet and your edge parameter to PatchCollection. This is because we want matplotlib to handle as much as possible by itself so it can sort out the colours for you.

patch_collection = mpl.collections.PatchCollection(patches,cmap=matplotlib.cm.jet, norm=matplotlib.colors.LogNorm(), lw=0)

Then use set_array to give the PatchCollection the z information:

patch_collection.set_array(z_plot)

Finally add the collection to the plot, create the colorbar and show the figure:

ax.add_collection(patch_collection)
plt.colorbar(patch_collection)

plt.show()

This answer is heavily based on the example given here which may be useful.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!