Hướng dẫn dùng matlab heatmap python
Show
SyntaxDescriptionexample
example
example
example
Examplescollapse all Create Heatmap from Tabular DataCreate a heatmap from a table of data for medical patients. Load the load patients tbl = table(LastName,Age,Gender,SelfAssessedHealthStatus,... Smoker,Weight,Location); h = heatmap(tbl,'Smoker','SelfAssessedHealthStatus'); Reorder Heatmap LabelsCreate a heatmap and reorder the labels along the y-axis. Load the load patients tbl = table(LastName,Age,Gender,SelfAssessedHealthStatus,... Smoker,Weight,Location); h = heatmap(tbl,'Smoker','SelfAssessedHealthStatus'); Reorder the labels along the y-axis. h.YDisplayData = {'Excellent','Good','Fair','Poor'}; Alternatively, you can reorder the labels by changing the data to categorical data and then reordering the categories using the Specify Table Variable for Heatmap ColorsCreate a heatmap and specify the table variable to use when determining the heatmap cell colors. Load the load patients tbl = table(LastName,Age,Gender,SelfAssessedHealthStatus,... Smoker,Weight,Location); h = heatmap(tbl,'Smoker','SelfAssessedHealthStatus',... 'ColorVariable','Age'); Specify Calculation Method for Color DataCreate a heatmap and specify the table variable and calculation method to use when determining the heatmap cell colors. Load the patients data set and create a heatmap from the data. Color each cell using the median age of patients with a particular pair of load patients tbl = table(LastName,Age,Gender,SelfAssessedHealthStatus,... Smoker,Weight,Location); h = heatmap(tbl,'Smoker','SelfAssessedHealthStatus',... 'ColorVariable','Age','ColorMethod','median'); Create Heatmap from Matrix DataCreate a matrix of data. Then create a heatmap of the matrix values. The default labels along the x-axis and y-axis appear as 1, 2, 3, and so on. cdata = [45 60 32; 43 54 76; 32 94 68; 23 95 58]; h = heatmap(cdata); Create Heatmap from Matrix Data Using Custom Axis LabelsCreate a matrix of data. Then create a heatmap of the matrix values. Use custom labels along the x-axis and y-axis by specifying the first two input arguments as the labels you want. Specify the title and axis labels by setting properties of the cdata = [45 60 32; 43 54 76; 32 94 68; 23 95 58]; xvalues = {'Small','Medium','Large'}; yvalues = {'Green','Red','Blue','Gray'}; h = heatmap(xvalues,yvalues,cdata); h.Title = 'T-Shirt Orders'; h.XLabel = 'Sizes'; h.YLabel = 'Colors'; Normalize Colors Along Each Row or ColumnCreate a heatmap and normalize the colors along each column or row by setting the Read the sample file T = readtable('outages.csv');
T(1:5,:) ans=5×6 table
Region OutageTime Loss Customers RestorationTime Cause
_____________ ________________ ______ __________ ________________ ___________________
{'SouthWest'} 2002-02-01 12:18 458.98 1.8202e+06 2002-02-07 16:50 {'winter storm' }
{'SouthEast'} 2003-01-23 00:49 530.14 2.1204e+05 NaT {'winter storm' }
{'SouthEast'} 2003-02-07 21:15 289.4 1.4294e+05 2003-02-17 08:14 {'winter storm' }
{'West' } 2004-04-06 05:44 434.81 3.4037e+05 2004-04-06 06:10 {'equipment fault'}
{'MidWest' } 2002-03-16 06:18 186.44 2.1275e+05 2002-03-18 23:23 {'severe storm' }
Create a heatmap that shows the different regions along the x-axis and the different outage causes along the y-axis. In each cell, show how many times each region experienced a power outage due to a particular cause. h = heatmap(T,'Region','Cause'); Normalize the colors along each column. The smallest value in each column maps to the first color in the colormap and the largest value maps to the last color. The last color indicates the cause that caused the most power outages for each region. h.ColorScaling = 'scaledcolumns'; Normalize the colors along each row instead. The smallest value in each row maps to the first color in the colormap and the largest value maps to the last color. The last color indicates the region that experienced the most power outages due to each cause. h.ColorScaling = 'scaledrows'; Compute Color Data Using Custom Aggregation MethodCreate a heatmap and color the cells using data computed with a custom aggregation method. Use the Read the sample file tbl = readtable('TemperatureData.csv'); Create categorical arrays from the months = categorical(tbl.Month); years = categorical(tbl.Year); xlabels = categories(months); ylabels = categories(years); Determine the final size of the resulting color data based on the number of unique months and years. nummonths = numel(xlabels); numyears = numel(ylabels); Convert the categorical x = double(months); y = double(years); temps = tbl.TemperatureF; cdata = accumarray([y,x],temps,[numyears,nummonths],@max,NaN); Create the heatmap. Label the x-axis and y-axis with the months and years, respectively. Color the heatmap cells using the computed matrix data. h = heatmap(xlabels,ylabels,cdata); Note: You can use the Input Argumentscollapse all tbl — Source table table | timetableSource table, specified as a table or timetable. You can create a table from workspace variables using the The xvar — Table variable for x-axis character vector | string scalar | numeric scalar | logical vectorTable variable for x-axis, specified in one of these forms:
The values associated with your table variable must be grouped into a finite set of discrete categories that the The labels that appear along the x-axis are in alphabetical order. You can customize the labels using categorical arrays. For an example, see Create Heatmap from Tabular Data. The yvar — Table variable for y-axis character vector | string scalar | numeric scalar | logical vectorTable variable for y-axis, specified in one of these forms:
The values associated with your table variable must be grouped into a finite set of discrete categories that the The labels that appear along the y-axis are in alphabetical order. You can customize the labels using categorical arrays. For an example, see Create Heatmap from Tabular Data. The cvar — Table variable for color data character vector | string scalar | numeric scalar | logical vectorTable variable for color data, specified in one of these forms:
The values associated with your table variable must be of a numeric type or The property value is empty By default, cdata — Color data matrixColor data for the heatmap cells, specified as a matrix. The Example: xvalues — Values appearing along x-axis categorical array | string array | numeric array | cell array of character vectorsValues appearing along the x-axis, specified as a categorical array, string array, numeric array, or cell array of character vectors. Example: Example: yvalues — Values appearing along y-axis categorical array | string array | numeric array | cell array of character vectorsValues appearing along y-axis, specified as a categorical array, string array, numeric array, or cell array of character vectors. Example: Example: parent — Parent container Figure object | Panel object | Tab object | TiledChartLayout object | GridLayout objectParent container, specified as a Name-Value ArgumentsSpecify optional
pairs of arguments as Before R2021a, use commas to separate each name and value, and enclose Example: Chart title, specified as a character vector, cell array of character vectors, string array, or categorical array. Example: Example:
To create a multiline title, specify a cell array of character vectors or a string array. Each element in the array corresponds to a line of text. Example: If you specify the title as a categorical array, MATLAB® uses the values in the array, not the categories. If you create the chart using tabular data, the default chart has an autogenerated title. If you do not want a title, specify By default, heatmaps support a subset of TeX markup for the text you specify. Use TeX markup to add superscripts and subscripts, modify the font type and color, and include special characters in the text. If you want a TeX markup character in regular text, such as an underscore (_), then insert a backslash (\) before the character you want to include. The backslash is the TeX escape character. For more information, see the Interpreter property of the text object. Method to calculate the color data values (stored in If you do not want to use a third variable from the table for the color data, then specify the method as If you want to use a third variable from the table for the color data, then set the
If you want to compute your own matrix of aggregated data, use the Example: Example: Output Argumentscollapse all h — Heatmap chart object HeatmapChart object
More Aboutcollapse all Standalone VisualizationA standalone visualization is a chart designed for a special purpose that works independently from other charts. Unlike other charts such as
Tips
Version HistoryIntroduced in R2017a expand all R2019a: Heatmaps interpret text using TeX markupBehavior changed in R2019a Starting in R2019a, heatmaps interpret text using TeX markup instead of displaying the literal characters. If you want to use a TeX markup character in regular text, such as an underscore (_), then insert a backslash (\) before the character you want to include. The backslash is the TeX escape character. For more information on using TeX markup, see the Interpreter property of the text object. |