具有 3d 条和不同条颜色的双变量直方图图

Bivariate histogram plot with 3d bars AND different bar colors

histogram2 function (added in R2015b) has the DisplayStyle可选参数,控制输出显示使用"bars"(统一颜色但不同高度)或"tiles"(相同高度为0,但有不同的颜色),如下所示:

rng(1337); X = rand(100,1)-0.5; Y = randn(100,1); rng('default');
figure(); 
subplot(1,2,1); hH(1) = histogram2(X, Y, 'DisplayStyle', 'bar3');
subplot(1,2,2); hH(2) = histogram2(X, Y, 'DisplayStyle', 'tile');

我想结合这两种模式,得到不同高度的条,有不同的颜色。如前所述,我尝试使用 'DisplayStyle' 选项,但它同时更改了太多视觉元素。任何人都可以建议一种方法来获得我想要的东西吗?

我非常希望解决方案成为一个 histogram2 对象(而不是例如 bar3),因为以后使用它们会更方便。

长话短说:
histogram2(X, Y, 'FaceColor', 'flat');

那些更喜欢“蛮力”方法而不是彻底阅读文档的人可能会通过深入比较两个结果对象(hH(1)hH(2))来解决这个问题,找到不同的属性,然后尝试从其他样式分配所需的值。的确,这些差异出现了:

  • FaceColor'auto''flat',分别在“蓝色”和“扁平”图表中。
  • FaceLighting'lit''none',分别在“蓝色”和“扁平”图表中。

然后,幸运的是,下面一行确实有效:

hH(1).FaceColor = 'flat';

documentation of FaceColor:

中找到了这个有效的原因

Histogram bar color, specified as one of these values:

'none' — ....

'flat'Bar colors vary with height. Bars with different height have different colors. The colors are selected from the figure or axes colormap.

'auto' — ....

...早点阅读可以节省一些时间。


这里要吸取的教训是:

当有大量文档可用时1,我们不应该只停留在与问题相关的第一件事上,因为可能会出现明显的解决方案再往下一点。

1 大多数 MATLAB 函数的情况,尤其是 MATLAB 的图形对象。