matplotlib 散点图中的对数颜色条 [英] A logarithmic colorbar in matplotlib scatter plot
问题描述
我想让散点图上的点的颜色对应于空隙率的值,但在对数刻度上以放大差异.我这样做了,但是现在当我执行 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')
感谢您的帮助.
推荐答案
现在有一部分文档描述了 颜色映射和标准化的工作原理
There is now a section of the documentation describing how color mapping and normalization works
matplotlib
进行颜色映射的方式分为两步,首先是一个 Normalize
函数(由 matplotlib.colors.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.
您只需要使用 LogNorm
规范化类,通过 norm
kwarg 传入.
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.
这篇关于matplotlib 散点图中的对数颜色条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!