matplotlib散点图中的对数色条

时间:2022-09-10 22:26:53

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]?

我想使散点图上的点的颜色对应于空隙分数的值,但是在对数标度上以放大差异。我这样做了,但是现在当我执行plt.colorbar()时,它会显示空白分数的对数,当我真正想要实际的空隙分数时。如何使用属于[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.

这是我现在拥有的图的图像,但是空隙分数颜色条没有被适当地标记为对应于真实的空隙分数,而不是它的对数。

matplotlib散点图中的对数色条

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.

谢谢你的帮助。

1 个解决方案

#1


55  

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]. The second step maps values in [0,1] -> RGBA space.

matplotlib进行颜色映射的方式分为两步,首先是Normalize函数(由matplotlib.colors.Normalize的子类包装),它将您输入的数据映射到[0,1]。第二步映射[0,1] - > RGBA空间中的值。

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

您只需要使用Logorm规范化类,并使用规范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进行转换而不是自己进行转换。

  • Normalize doc
  • 规范化文档
  • LogNorm doc
  • LogNorm doc
  • matplotlib.color doc
  • matplotlib.color doc

#1


55  

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]. The second step maps values in [0,1] -> RGBA space.

matplotlib进行颜色映射的方式分为两步,首先是Normalize函数(由matplotlib.colors.Normalize的子类包装),它将您输入的数据映射到[0,1]。第二步映射[0,1] - > RGBA空间中的值。

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

您只需要使用Logorm规范化类,并使用规范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进行转换而不是自己进行转换。

  • Normalize doc
  • 规范化文档
  • LogNorm doc
  • LogNorm doc
  • matplotlib.color doc
  • matplotlib.color doc