matplotlib散点图中的对数颜色条 [英] A logarithmic colorbar in matplotlib scatter plot

查看:160
本文介绍了matplotlib散点图中的对数颜色条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使散点图上的点的颜色与空隙分数的值相对应,但以对数标度放大差异.我这样做了,但是现在当我执行plt.colorbar()时,当我真的想要实际的空隙率时,它会显示空隙率的对数.如何在色条上用相应的[0.00001,1]空隙分数标签进行对数刻度?

I would like to make the colors of the points on the scatter plot correspond to the value of the void fraction, but on a logarithmic scale to amplify differences. I did this, but now when I do plt.colorbar(), it displays the log of the void fraction, when I really want the actual void fraction. How can I make a log scale on the colorbar with the appropriate labels of the void fraction, which belongs to [0.00001,1]?

这是我现在所拥有的情节的图像,但是空隙分数颜色条没有正确地标记为与真实的空隙分数相对应,而不是对应于它的对数.

Here is an image of the plot I have now, but the void fraction colorbar is not appropriately labeled to correspond to the true void fraction, instead of the log of it.

fig = plt.figure()
plt.scatter(x,y,edgecolors='none',s=marker_size,c=np.log(void_fraction))
plt.colorbar()
plt.title('Colorbar: void fraction')

感谢您的帮助.

推荐答案

文档的一部分现在描述颜色映射和规范化的工作方式(这是开发文档的链接,但适用于mpl的所有版本.它将在主文档"soon"中)

There is now a section of the documentation describing how color mapping and normalization works (that is a link to the development documentation, but applies to all versions of mpl. It will be in the mainline documentation 'soon')

matplotlib进行颜色映射的方法分两个步骤,首先是Normalize函数(由的子类包装),该函数将您提交的数据映射到[0, 1].第二步映射[0,1]-> RGBA空间中的值.

The way that matplotlib does color mapping is in two steps, first a Normalize function (wrapped up by the sub-classes of matplotlib.colors.Normalize) which maps the data you hand in to [0, 1]. The second step maps values in [0,1] -> RGBA space.

您只需要使用随norm kwarg传入的LogNorm规范化类.

You just need to use the LogNorm normalization class, passed in with the norm kwarg.

plt.scatter(x,y,edgecolors='none',s=marker_size,c=void_fraction,
                norm=matplotlib.colors.LogNorm())

当您想要缩放/调整数据以进行绘图时,最好让matplotlib进行转换,而不是自己进行转换.

When you want to scale/tweak data for plotting, it is better to let matplotlib do the transformations than to do it your self.

  • Normalize doc
  • LogNorm doc
  • matplotlib.color doc

这篇关于matplotlib散点图中的对数颜色条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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