自定义颜色矩阵

参考:Matlab作图Plot自动设置线条颜色

  1. %% 设置颜色数组,循环使用自定义的不同颜色
  2. data = [1495.8289,1482.7761,1481.7087,1540.0725,1634.9596,1701.2878,1719.0398,1787.5005,2116.4097,2917.4580,4250.5449,5920.3862,7500.7246,8493.1563,8546.8984,7624.3667,6022.2988,4236.0674,2740.3240,1801.7512,1415.3453,1380.8688,1457.1257;1524.9509,1516.9895,1519.9365,1577.3025,1666.5717,1726.8987,1742.6063,1813.1699,2142.8354,2934.8203,4243.1709,5874.3999,7412.7407,8374.8643,8422.3633,7520.9248,5959.5869,4218.8716,2758.7649,1838.1851,1453.7550,1413.4885,1485.1687;1553.1062,1549.8462,1556.4934,1612.8953,1696.9139,1751.6591,1765.4696,1837.9187,2168.0295,2951.0476,4235.5801,5829.9458,7328.3735,8261.8066,8303.5127,7422.2192,5899.6431,4202.2422,2776.1221,1872.8434,1490.5303,1444.9550,1512.3591;1599.9282,1548.8813,1549.4675,1629.3633,1741.3156,1807.1475,1809.6577,1858.7754,2169.2627,2944.3589,4226.6289,5809.4082,7276.0063,8162.2710,8163.8608,7278.4805,5805.8232,4205.8447,2893.2632,2077.4297,1724.5004,1644.6768,1637.0161;1584.1958,1551.4672,1590.8636,1695.3685,1800.1534,1835.7272,1810.3317,1855.3517,2185.7446,2982.1006,4257.1895,5790.1514,7180.0098,8003.4883,7999.9321,7186.6255,5843.9697,4381.0010,3155.8286,2345.2886,1923.4561,1742.4698,1652.5088;1591.5916,1579.2158,1638.7156,1745.4473,1832.2404,1843.7811,1807.8594,1865.0564,2219.9731,3027.6284,4275.9995,5738.0234,7033.0327,7776.6450,7745.6436,6972.6304,5722.1924,4364.5547,3216.6343,2432.7402,1992.5060,1774.7474,1659.2755];
  3. [N,~]=size(data);
  4. % 如定义颜色矩阵,Nx3维,每一行一个颜色。
  5. color=[0 0 0
  6. 0 0 1
  7. 0 1 0
  8. 0 1 1
  9. 1 0 0
  10. 1 0 1
  11. 1 1 0
  12. 1 1 1 ...
  13. ] ; %可以任意定义0-1之间的数值
  14. figure
  15. hold on; %在同一张图上绘制
  16. for i=1:N
  17. plot(data(i,:),'color',color(i,:)); % data(i,:) 为所需要绘制的线条数据
  18. end
  19. legend;

image.png

MATLAB中的标准颜色映像

参考:

示例程序

  1. ts = 1:16:353;
  2. y = [7936,8256,6883,3424,1625,7982,7876,4758,386,8232,439,7735,5780,3517,8816,395,4199,8473,4072,8497,8347,8319,8230;7677.6196,6503.1904,5625.3569,5254.3774,5275.8921,5378.3721,5286.2163,4944.0322,4536.2485,4337.7559,4502.8936,4939.7412,5359.0415,5469.8594,5193.6260,4750.4912,4550.0142,4943.4893,5987.8877,7371.1890,8553.9658,9050.3828,8685.3574;7972.0337,6713.5854,5600.8018,5036.9482,5058.4629,5353.8164,5496.6113,5238.4458,4672.6768,4166.3081,4107.9941,4638.9233,5544.1563,6378.1138,6745.9102,6559.6953,6102.2993,5851.7451,6173.0020,7070.3711,8159.0679,8878.9355,8821.7852;8271.1650,6854.4824,5431.1392,4641.1973,4755.3979,5535.4277,6397.8872,6776.7910,6459.2549,5690.2778,4990.5918,4812.0093,5253.5132,6017.1011,6624.9082,6749.9297,6440.2466,6087.0469,6154.5234,6846.8843,7932.6777,8849.4541,9033.2695;8283.5664,7097.4771,5718.6187,4722.0249,4518.6709,5144.9956,6253.1104,7305.0371,7856.3208,7766.5337,7224.3960,6590.4248,6167.8853,6047.1685,6111.0562,6178.4810,6174.3667,6200.6997,6455.8564,7057.0742,7892.9810,8620.4609,8828.9824;8057.0186,7200.1714,6328.0454,5787.5762,5762.2520,6191.2227,6816.7939,7328.5664,7519.5166,7368.0142,7009.2192,6627.3301,6344.2720,6174.3735,6064.6938,5980.9058,5968.5947,6138.8545,6582.1890,7268.8369,8009.9331,8519.8252,8555.6807;8054.7671,7085.9258,6220.8921,5809.6099,5936.7603,6410.1353,6906.5020,7176.0508,7176.3896,7058.6055,7026.3662,7170.7026,7395.6870,7486.7168,7275.4414,6787.7012,6267.9326,6051.9854,6360.2646,7140.0879,8062,8682.8818,8685.5234;8227.0732,7040.9985,5952.6270,5522.3613,5959.3203,7013.5635,8132.2075,8780.6631,8733.4063,8163.6523,7490.5288,7093.3350,7080.2412,7255.8418,7298.0361,7017.0342,6516.6924,6144.3384,6256.7974,6955.4419,7969.9116,8779.5674,8910.1582;8427.4805,7250.5640,5982.6152,5336.6123,5702.4390,6906.5933,8309.7598,9183.6211,9135.6016,8322.1230,7324.6108,6773.2949,6958.8018,7673.9341,8377.7656,8567.9053,8115.9282,7348.1943,6821.1675,6939.4258,7667.8701,8530.6387,8905.0928;8203.2637,7221.0093,6142.0825,5543.2554,5801.4658,6845.2690,8168.9458,9109.7422,9224.8350,8540.8711,7525.8525,6803.1592,6784.4697,7444.6025,8369.0234,9033.1377,9130.0664,8739.3838,8236.7471,8013.3037,8195.6426,8554.1152,8662.2070;8166.2080,7760.2734,7294.4658,7030.4116,7153.7871,7640.7949,8249.1250,8646.7803,8604.6846,8136.2344,7496.8628,7041.2148,7022.4580,7453.3145,8109.8647,8671.3564,8907.0449,8795.4922,8507.8262,8273.8857,8223.2930,8306.0820,8342.6104];
  3. len = 11;
  4. color_type = hsv(len);
  5. for i = 1:len
  6. hold on;
  7. plot(ts,y(i,:)/10000,'-o','color',color_type(i,:),'LineWidth',2,'MarkerSize',5);
  8. end
  9. legend_cell = {'original series','1st iteration','2nd iteration','3rd iteration',...
  10. '4th iteration','5th iteration','6th iteration','7th iteration',...
  11. '8th iteration','9th iteration','10th iteration'};
  12. leg = legend(legend_cell,'Location','best');
  13. set(leg,'FontName','Times New Roman','FontSize',20,'FontWeight','normal');

image.png

颜色映象

  • MATLAB 有一个叫颜色映象的数据结构来代表颜色值。
    • 颜色映象定义为一个有三列和若干行的矩阵
    • 利用 0 到 1 之间的数,矩阵的每一行都代表了一种色彩。
    • 任一行的数字都指定了一个 RGB 值,即红、黄、蓝三种颜色的强度,形成一种特定的颜色。
  • 一些有代表性的 RGB 值在下表中给出。 | Red(红) | Green(绿) | Blue(蓝) | 颜色 | | :—-: | :—-: | :—-: | :—-: | | 0 | 0 | 0 | | | 1 | 1 | 1 | | | 1 | 0 | 0 | | | 0 | 1 | 0 | 绿 | | 0 | 0 | 1 | | | 1 | 1 | 0 | | | 1 | 0 | 1 | 洋红 | | 0 | 1 | 1 | 青蓝 | | 0.667 | 0.667 | 1 | 天蓝 | | 1 | 0.5 | 0 | 橘黄 | | 0.5 | 0 | 0 | 深红 | | 0.5 | 0.5 | 0.5 | 灰色 |

十个 MATLAB 函数产生预定的颜色映象:

标准颜色映象
hsv 色彩饱和值(以红色开始和结束) 【MATLAB】随机绘制颜色 - 图3
hot 从黑到红到黄到白
cool 青蓝和洋红的色度
pink 粉红的彩色度
bone 带一点蓝色的灰度
jet hsv的一种变形(以蓝色开始和结束)
copper 线性铜色度
prim 三棱镜。交替为红,橘黄,黄,绿,天蓝
flag 交替为红,白,蓝,黑
gray 线性灰度
  • 缺省,上面所列的各个颜色映象产生一个64×3的矩阵,指定了64种颜色RGB的描述。
  • 这些函数都接受一个参量来指定所产生矩阵的行数。
    • 比如hot(m)产生一个m×3的矩阵,它包含的RGB颜色值的范围从黑经过红、橘红和黄,到白
  • image.png

    colormap函数:获取当前色图

    t = 0:pi/6:pi*2;
    [x,y,z] = cylinder(0.5*sin(t));
    surf(x,y,z);
    colormap(pink);
    pink;
    

    image.png

    pcolor 函数:显示一个颜色映象

    n=32;
    colormap(hsv(n));
    pcolor([1:n+1;1:n+1]);
    title('Using Pcolor to Display a Color Map')
    

    image.png

    colorbar函数:显示颜色条

  • 在当前的图形窗口中增加水平或垂直的颜色标尺以显示当前坐标轴的颜色映象

    [x,y,z]=peaks;
    mesh(x,y,z);
    colormap(hsv)
    axis([-3  3  -3  3  -6  8]);
    colorbar;
    title('使用颜色条')
    

    image.png
    image.png
    image.png