Python matplotlib 散点图中的对数颜色条
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17201172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
A logarithmic colorbar in matplotlib scatter plot
提问by Cokes
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.
这是我现在拥有的图的图像,但空隙率颜色条没有适当标记以对应于真实空隙率,而不是它的对数。


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.
谢谢你的帮助。
采纳答案by tacaswell
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 matplotlibdoes color mapping is in two steps, first a Normalizefunction (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 LogNormnormalization class, passed in with the normkwarg.
您只需要使用LogNorm与normkwarg一起传入的规范化类。
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 matplotlibdo the transformations than to do it your self.
当您想缩放/调整数据以进行绘图时,最好让matplotlib转换而不是自己做。

