Matlab中的条形图开关颜色

时间:2019-01-07 09:21:36

标签: matlab plot colors matlab-figure

我在Matlab中有以下代码,它为我生成了一个有用的图。现在,我想使该图与另一图在颜色方面具有可比性。因此,应该切换一些颜色:...的颜色

“拆分前总EON”和“拆分后总EON”

“前拆分纯EON”和“后拆分纯EON”

“拆分前总RWE”和“拆分后总RWE”

“ Pre split pure RWE”和“ Post split pure RWE”。

仅此而已,但我不知道该怎么做,因为颜色是自动分配的...

clear all
close all

values = [4 1 11 2 3; 4 1 5 2 -10];
names = {'Pre split total EON' 'Post split total EON'...
    'Pre split pure EON' 'Post split pure EON' 'Post split Uniper';...
    'Pre split total RWE' 'Post split total RWE'...
    'Pre split pure RWE' 'Post split pure RWE' 'PostSplitInnogy'};
categories = {'EON','RWE'};
figure;
b = bar(values,'FaceColor','flat');
ticksList = b(1).XData+arrayfun(@(x)x.XOffset, b)';
xticks(ticksList(:))
xticklabels([names(1,:)';names(2,:)'])
xtickangle(90)
ax1 = gca;
ax2 = axes('Position', get(ax1, 'Position'),'Color', 'none');
set(ax2, 'XAxisLocation', 'top','YAxisLocation','Right');
set(ax2, 'XLim', get(ax1, 'XLim'),'YLim', get(ax1, 'YLim'));
set(ax2, 'YTick', []);
xticks(b(1).XData)
xticklabels(categories)
for k = 1:size(values,2) % for fancier colors.
    b(k).CData = k;
end

enter image description here

2 个答案:

答案 0 :(得分:3)

我觉得您是在以下位置手动分配颜色的:

const asmData = [{
  "id": "111",
  "policies": [{
      "name": "JOHN DOE 1",
      "sum_covered": "555000",
      "date": "2018-05-16 12:02:32"
    },
    {
      "name": "JOHN DOE 2",
      "sum_covered": "404000",
      "date": "2018-02-20 17:33:25"
    }
  ]
}, {
  "id": "222",
  "policies": [{
      "name": "JOHN DOE 3",
      "sum_covered": "555000",
      "date": "2018-05-16 12:02:32"
    },
    {
      "name": "JOHN DOE 4",
      "sum_covered": "404000",
      "date": "2018-02-20 17:33:25"
    }
  ]
}]



$(document).on("mouseenter", "a", function() {
  var $tb = $('#summary-table tbody');
  //Make sure table is empty

  $tb.empty()

  var asmId = $(this).attr('data-id'),
    asmPolicies = "";
  for (var i = 0; i < asmData.length; i++) {
    if (asmId == asmData[i]['id']) {
      for (var j = 0; j < asmData[i]['policies'].length; j++) {
        var pol = asmData[i]['policies'][j];
        asmPolicies += '<tr><td>' + pol.name + '</td><td>' + pol.sum_covered + '</td><td>' + pol.date + '</td><td>' + '</td></tr>';
      }
    }
  }
  //append asmPolicies Html to the table
  $tb.append(asmPolicies);

});

如果您只想更改顺序,可以通过

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href="#" data-id="111">111</a> | <a href="#" data-id="222">222</a>

<div class="col-md-8">
  <table class="table table-hover mg-b-0 tx-11" id="summary-table">
    <thead>
      <tr>
        <th>NAME</th>
        <th>SUM</th>
        <th>DATE</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <!-- Add policies dynamically via JS under respective thead columns-->
      </tr>
    </tbody>
  </table>
</div>

,依此类推。或者,您可以使用

进行全部更改
Sub user()
Dim sqlColumnNames As String, strSQL As String

sqlColumnNames = "name,address"
strSQL = fetchUserInfo(sqlColumnNames)
MsgBox strSQL

End Sub

Public Function fetchUserInfo(columnnames As String) As String
    fetchUserInfo = "SELECT  " & columnnames & " FROM userInfo.csv"
End Function

答案 1 :(得分:1)

我知道@Finn已经回答了这个问题,但是当我测试他的解决方案时,我无法使其工作。奇怪的是,我意识到即使原始脚本也给我的系统带来了错误。可能是我有一个非常古老的MATLAB版本。但话又说回来,我想知道是否还有其他人也有问题。

由于某些原因,我无法为CData设置bar成员。

无论如何,经过测试,我得到了这个为我工作。

clc
clear all
close all

values = [4 1 11 2 3; 4 1 5 2 -10];
names = {'Pre split total EON' 'Post split total EON'...
    'Pre split pure EON' 'Post split pure EON' 'Post split Uniper';...
    'Pre split total RWE' 'Post split total RWE'...
    'Pre split pure RWE' 'Post split pure RWE' 'PostSplitInnogy'};
categories = {'EON','RWE'};
figure;
b = bar(values,'FaceColor','flat');

% only significant changes here
% ***********************************
col = ["r", "b", "y", "g", "cyan"];
for k = 1:size(values,2) % for fancier colors.
    set(b(k), "FaceColor", col(k));
end
% ***********************************

ticksList = b(1).XData + arrayfun(@(x)x.XOffset, b)';
xticks(ticksList(:))
xticklabels([names(1,:)';names(2,:)'])
xtickangle(90)
ax1 = gca;
ax2 = axes('Position', get(ax1, 'Position'),'Color', 'none');
set(ax2, 'XAxisLocation', 'top','YAxisLocation','Right');
set(ax2, 'XLim', get(ax1, 'XLim'),'YLim', get(ax1, 'YLim'));
set(ax2, 'YTick', []);
xticks(b(1).XData)
xticklabels(categories)

如果您想进一步了解在MATLAB中如何为颜色分配名称,请选中here

您可以玩的另一件事是按照here的说明设置colormap