简体   繁体   English

matplotlib 散点图中的对数颜色条

[英]A logarithmic colorbar in matplotlib scatter plot

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.我这样做了,但是现在当我执行 plt.colorbar() 时,它会显示空分数的日志,而我真的想要实际的空分数。 How can I make a log scale on the colorbar with the appropriate labels of the void fraction, which belongs to [0.00001,1]?如何使用属于 [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')

Thanks for your help.谢谢你的帮助。

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')现在有一部分文档描述了颜色映射和规范化的工作原理(这是开发文档的链接,但适用于 mpl 的所有版本。它将在主线文档中“很快”)

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] . matplotlib颜色映射的方式分为两个步骤,首先是一个Normalize函数(由matplotlib.colors.Normalize的子类封装),它将您提交的数据映射到[0, 1] The second step maps values in [0,1] -> RGBA space.第二步映射[0,1] -> RGBA 空间中的值。

You just need to use the LogNorm normalization class, passed in with the norm kwarg.您只需要使用LogNorm规范化类,通过norm kwarg 传入。

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

When you want to scale/tweak data for plotting, it is better to let matplotlib do the transformations than to do it your self.当您想缩放/调整数据以进行绘图时,最好让matplotlib进行转换而不是您自己进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM