Plot matrix containing negatives in confusionchart() style

16 次查看(过去 30 天)
Hello,
I have a matrix of numbers which includes negitives. I would like to plot this in the same graphical style as confusionchart() so all figures look consistent. I have had a look inside the function with open confusionchart() however I do not beleive the specific code is visable.
Is there an easy way to replicate this? If not I can code it manually, however I don't want to spend lots of time on this if there is a pre-existing solotion.
Kind regards,
Christopher

采纳的回答

Shubham
Shubham 2023-7-26
Hi Christopher,
If you want to plot a matrix of numbers with negative values in the same graphical style as `confusionchart()`, you can use the `imagesc()` function in MATLAB. `imagesc()` is commonly used to visualize matrices as images, with a color scale representing the values.
Here's an example of how you can use `imagesc()` to plot a matrix with negative values:
% Example matrix with negative values
matrix = [1 2 3; -1 -2 -3; 4 5 6];
% Plot the matrix using imagesc()
figure;
imagesc(matrix);
% Set the color map to match confusionchart()
colormap(parula); % You can choose a different colormap if desired
% Add colorbar for reference
colorbar;
In this example, `imagesc()` is used to plot the `matrix` with negative values. The `colormap()` function is then used to set the color map to match the style of `confusionchart()`. You can choose a different colormap if you prefer.
By using `imagesc()` and setting the appropriate colormap, you can achieve a consistent graphical style similar to `confusionchart()` for plotting matrices with negative values.
You can refer to these documentation for more info:
  4 个评论
Shubham
Shubham 2023-7-27
编辑:Shubham 2023-7-27
Can you please explain in detail about your question on colour map? I did not get it.
Christopher McCausland
编辑:Christopher McCausland 2023-8-4
Hi Shubham.
What I had meant was to exactly mirror how a plot from confusionchat() looks like. Including the red-white-blue colour scheme and black/white contrast writting. For a differance graph of two matricies. I may change the colourgraph in the future as I am not convinced that this is clear, given that a reduction in confusion is marked in red but this is beside the point. For anyone in the future, here is the code to do so. I don't have the time to wrap it into a function so please forgive the untidy code.
% Sample data
data = [1350, -943, -440, 536, -503;
319, 1439, -875, 138, -1021;
-64, 70, 791, -113, -684;
-168, -341, -373, 1832, -950;
-206, -1026, -337, -352, 1921];
% Calculate the maximum and minimum values for setting the colormap range
cmax = max(data(:));
cmin = min(data(:));
% Create the heatmap
figure;
imagesc(data);
offset = 400; % Change the contrast point for black/white text
% Given colormap values
cmMap = [ 0 0.4500 0.7400
0.0079 0.4543 0.7420
0.0157 0.4587 0.7441
0.0236 0.4630 0.7461
0.0315 0.4673 0.7482
0.0394 0.4717 0.7502
0.0472 0.4760 0.7523
0.0551 0.4803 0.7543
0.0630 0.4846 0.7564
0.0709 0.4890 0.7584
0.0787 0.4933 0.7605
0.0866 0.4976 0.7625
0.0945 0.5020 0.7646
0.1024 0.5063 0.7666
0.1102 0.5106 0.7687
0.1181 0.5150 0.7707
0.1260 0.5193 0.7728
0.1339 0.5236 0.7748
0.1417 0.5280 0.7769
0.1496 0.5323 0.7789
0.1575 0.5366 0.7809
0.1654 0.5409 0.7830
0.1732 0.5453 0.7850
0.1811 0.5496 0.7871
0.1890 0.5539 0.7891
0.1969 0.5583 0.7912
0.2047 0.5626 0.7932
0.2126 0.5669 0.7953
0.2205 0.5713 0.7973
0.2283 0.5756 0.7994
0.2362 0.5799 0.8014
0.2441 0.5843 0.8035
0.2520 0.5886 0.8055
0.2598 0.5929 0.8076
0.2677 0.5972 0.8096
0.2756 0.6016 0.8117
0.2835 0.6059 0.8137
0.2913 0.6102 0.8157
0.2992 0.6146 0.8178
0.3071 0.6189 0.8198
0.3150 0.6232 0.8219
0.3228 0.6276 0.8239
0.3307 0.6319 0.8260
0.3386 0.6362 0.8280
0.3465 0.6406 0.8301
0.3543 0.6449 0.8321
0.3622 0.6492 0.8342
0.3701 0.6535 0.8362
0.3780 0.6579 0.8383
0.3858 0.6622 0.8403
0.3937 0.6665 0.8424
0.4016 0.6709 0.8444
0.4094 0.6752 0.8465
0.4173 0.6795 0.8485
0.4252 0.6839 0.8506
0.4331 0.6882 0.8526
0.4409 0.6925 0.8546
0.4488 0.6969 0.8567
0.4567 0.7012 0.8587
0.4646 0.7055 0.8608
0.4724 0.7098 0.8628
0.4803 0.7142 0.8649
0.4882 0.7185 0.8669
0.4961 0.7228 0.8690
0.5039 0.7272 0.8710
0.5118 0.7315 0.8731
0.5197 0.7358 0.8751
0.5276 0.7402 0.8772
0.5354 0.7445 0.8792
0.5433 0.7488 0.8813
0.5512 0.7531 0.8833
0.5591 0.7575 0.8854
0.5669 0.7618 0.8874
0.5748 0.7661 0.8894
0.5827 0.7705 0.8915
0.5906 0.7748 0.8935
0.5984 0.7791 0.8956
0.6063 0.7835 0.8976
0.6142 0.7878 0.8997
0.6220 0.7921 0.9017
0.6299 0.7965 0.9038
0.6378 0.8008 0.9058
0.6457 0.8051 0.9079
0.6535 0.8094 0.9099
0.6614 0.8138 0.9120
0.6693 0.8181 0.9140
0.6772 0.8224 0.9161
0.6850 0.8268 0.9181
0.6929 0.8311 0.9202
0.7008 0.8354 0.9222
0.7087 0.8398 0.9243
0.7165 0.8441 0.9263
0.7244 0.8484 0.9283
0.7323 0.8528 0.9304
0.7402 0.8571 0.9324
0.7480 0.8614 0.9345
0.7559 0.8657 0.9365
0.7638 0.8701 0.9386
0.7717 0.8744 0.9406
0.7795 0.8787 0.9427
0.7874 0.8831 0.9447
0.7953 0.8874 0.9468
0.8031 0.8917 0.9488
0.8110 0.8961 0.9509
0.8189 0.9004 0.9529
0.8268 0.9047 0.9550
0.8346 0.9091 0.9570
0.8425 0.9134 0.9591
0.8504 0.9177 0.9611
0.8583 0.9220 0.9631
0.8661 0.9264 0.9652
0.8740 0.9307 0.9672
0.8819 0.9350 0.9693
0.8898 0.9394 0.9713
0.8976 0.9437 0.9734
0.9055 0.9480 0.9754
0.9134 0.9524 0.9775
0.9213 0.9567 0.9795
0.9291 0.9610 0.9816
0.9370 0.9654 0.9836
0.9449 0.9697 0.9857
0.9528 0.9740 0.9877
0.9606 0.9783 0.9898
0.9685 0.9827 0.9918
0.9764 0.9870 0.9939
0.9843 0.9913 0.9959
0.9921 0.9957 0.9980
1.0000 1.0000 1.0000
1.0000 1.0000 1.0000
0.9988 0.9947 0.9929
0.9976 0.9894 0.9858
0.9965 0.9842 0.9787
0.9953 0.9789 0.9717
0.9941 0.9736 0.9646
0.9929 0.9683 0.9575
0.9917 0.9631 0.9504
0.9906 0.9578 0.9433
0.9894 0.9525 0.9362
0.9882 0.9472 0.9291
0.9870 0.9420 0.9220
0.9858 0.9367 0.9150
0.9846 0.9314 0.9079
0.9835 0.9261 0.9008
0.9823 0.9209 0.8937
0.9811 0.9156 0.8866
0.9799 0.9103 0.8795
0.9787 0.9050 0.8724
0.9776 0.8998 0.8654
0.9764 0.8945 0.8583
0.9752 0.8892 0.8512
0.9740 0.8839 0.8441
0.9728 0.8787 0.8370
0.9717 0.8734 0.8299
0.9705 0.8681 0.8228
0.9693 0.8628 0.8157
0.9681 0.8576 0.8087
0.9669 0.8523 0.8016
0.9657 0.8470 0.7945
0.9646 0.8417 0.7874
0.9634 0.8365 0.7803
0.9622 0.8312 0.7732
0.9610 0.8259 0.7661
0.9598 0.8206 0.7591
0.9587 0.8154 0.7520
0.9575 0.8101 0.7449
0.9563 0.8048 0.7378
0.9551 0.7995 0.7307
0.9539 0.7943 0.7236
0.9528 0.7890 0.7165
0.9516 0.7837 0.7094
0.9504 0.7784 0.7024
0.9492 0.7731 0.6953
0.9480 0.7679 0.6882
0.9469 0.7626 0.6811
0.9457 0.7573 0.6740
0.9445 0.7520 0.6669
0.9433 0.7468 0.6598
0.9421 0.7415 0.6528
0.9409 0.7362 0.6457
0.9398 0.7309 0.6386
0.9386 0.7257 0.6315
0.9374 0.7204 0.6244
0.9362 0.7151 0.6173
0.9350 0.7098 0.6102
0.9339 0.7046 0.6031
0.9327 0.6993 0.5961
0.9315 0.6940 0.5890
0.9303 0.6887 0.5819
0.9291 0.6835 0.5748
0.9280 0.6782 0.5677
0.9268 0.6729 0.5606
0.9256 0.6676 0.5535
0.9244 0.6624 0.5465
0.9232 0.6571 0.5394
0.9220 0.6518 0.5323
0.9209 0.6465 0.5252
0.9197 0.6413 0.5181
0.9185 0.6360 0.5110
0.9173 0.6307 0.5039
0.9161 0.6254 0.4969
0.9150 0.6202 0.4898
0.9138 0.6149 0.4827
0.9126 0.6096 0.4756
0.9114 0.6043 0.4685
0.9102 0.5991 0.4614
0.9091 0.5938 0.4543
0.9079 0.5885 0.4472
0.9067 0.5832 0.4402
0.9055 0.5780 0.4331
0.9043 0.5727 0.4260
0.9031 0.5674 0.4189
0.9020 0.5621 0.4118
0.9008 0.5569 0.4047
0.8996 0.5516 0.3976
0.8984 0.5463 0.3906
0.8972 0.5410 0.3835
0.8961 0.5357 0.3764
0.8949 0.5305 0.3693
0.8937 0.5252 0.3622
0.8925 0.5199 0.3551
0.8913 0.5146 0.3480
0.8902 0.5094 0.3409
0.8890 0.5041 0.3339
0.8878 0.4988 0.3268
0.8866 0.4935 0.3197
0.8854 0.4883 0.3126
0.8843 0.4830 0.3055
0.8831 0.4777 0.2984
0.8819 0.4724 0.2913
0.8807 0.4672 0.2843
0.8795 0.4619 0.2772
0.8783 0.4566 0.2701
0.8772 0.4513 0.2630
0.8760 0.4461 0.2559
0.8748 0.4408 0.2488
0.8736 0.4355 0.2417
0.8724 0.4302 0.2346
0.8713 0.4250 0.2276
0.8701 0.4197 0.2205
0.8689 0.4144 0.2134
0.8677 0.4091 0.2063
0.8665 0.4039 0.1992
0.8654 0.3986 0.1921
0.8642 0.3933 0.1850
0.8630 0.3880 0.1780
0.8618 0.3828 0.1709
0.8606 0.3775 0.1638
0.8594 0.3722 0.1567
0.8583 0.3669 0.1496
0.8571 0.3617 0.1425
0.8559 0.3564 0.1354
0.8547 0.3511 0.1283
0.8535 0.3458 0.1213
0.8524 0.3406 0.1142
0.8512 0.3353 0.1071
0.8500 0.3300 0.1000];
% Set the colormap for the current figure
colormap(flip(cmMap));
% Add text values to each segment
textStrings = num2str(data(:), '%d'); % Convert elements to string
textStrings = strtrim(cellstr(textStrings)); % Remove any space padding
[x, y] = meshgrid(1:size(data, 2), 1:size(data, 1)); % start the text grid
hStrings = text(x(:), y(:), textStrings(:), 'HorizontalAlignment', 'center');
midValue = offset + mean([cmin, cmax]);
textColors = repmat(data(:) > midValue, 1, 3);
set(hStrings, {'Color'}, num2cell(textColors, 2));
% Set custom labels on both axes
xticks(1:size(data, 2));
yticks(1:size(data, 1));
xticklabels({'N1', 'N2', 'N3', 'W', 'R'});
yticklabels({'N1', 'N2', 'N3', 'W', 'R'});
% Set axis labels and title
xlabel('Predicted Class');
ylabel('True Class');
title('Confusion Matrix');
% Set colorbar
colorbar;
% Remove tick marks on both axes
ax = gca;
ax.TickLength = [0, 0];

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Orange 的更多信息

产品


版本

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by