【发布时间】:2014-10-13 13:39:17
【问题描述】:
我正在为我的数据生成热图。
一切正常,但我有一个小问题。我的数据(数字)是从 0 到 10.000。
0 表示什么都没有(没有数据),目前 0 的字段只是采用我的色阶的最低颜色。我的问题是如何使 0 的数据具有完全不同的颜色(例如黑色或白色)
请看图片以更好地理解我的意思:
我的代码 (sn-p) 如下所示:
matplotlib.pyplot.imshow(results, interpolation='none')
matplotlib.pyplot.colorbar();
matplotlib.pyplot.xticks([0, 1, 2, 3, 4, 5, 6, 7, 8], [10, 15, 20, 25, 30, 35, 40, 45, 50]);
matplotlib.pyplot.xlabel('Population')
matplotlib.pyplot.yticks([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 10, 15, 20, 25, 30, 'serial']);
matplotlib.pyplot.ylabel('Communication Step');
axis.xaxis.tick_top();
matplotlib.pyplot.savefig('./results_' + optimisationProblem + '_dim' + str(numberOfDimensions) + '_' + statisticType + '.png');
matplotlib.pyplot.close();
【问题讨论】:
-
如果您只对等于
0的数据点有不同的颜色感兴趣,您可以使用for循环将0更改为nan。在imshow中,nan值将始终显示为white。但是,在这种情况下,不会有平滑的过渡,即值0.0001仍将是deep blue-ish。如果您确实想要这种转换,您可能需要创建您的own colorbar。 -
非常感谢。只需将
0更改为nan即可解决我的问题。如果您将此评论作为答案,我会将其标记为正确答案。
标签: python matplotlib colors plot