matlab使用教程(52)—绘制数据热图和文字云图

1.基于表格数据创建热图

        热图是一种使用颜色实现数据可视化的方式。此示例说明如何将文件作为表导入 MATLAB® 并根据表列创建热图。它还说明如何修改热图的外观,例如设置标题和轴标签。

1.1 以表的形式导入文件

        加载示例文件 TemperatureData.csv,此文件包含 2015 年 1 月至 2016 年 7 月间的日均温度。将此文件读取到一个表中并显示前五行。
tbl = readtable('TemperatureData.csv');
head(tbl,5)
Year Month Day TemperatureF
____ ___________ ___ ____________
2015 {'January'} 1 23
2015 {'January'} 2 31
2015 {'January'} 3 25
2015 {'January'} 4 39
2015 {'January'} 5 29

1.2 创建基本热图

        创建一个热图,其中 x 轴表示月份,y 轴表示年份。通过设置 ColorVariable 属性,使用温度数据为热图单元格着色。将 HeatmapChart 对象赋给变量 h 。在创建图后,可使用 h 对其进行修改。
h = heatmap(tbl,'Month','Year','ColorVariable','TemperatureF');

        默认情况下,MATLAB 会将颜色数据作为每月的平均温度进行计算。但是,可通过设置 ColorMethod 属性来更改计算方法。

1.3 对轴值重新排序

        轴值按字母顺序显示。对月份重新排序,以使其按年月顺序显示。可以使用分类数组或通过设置HeatmapChart 属性来自定义标签。要使用分类数组,首先将该表的 Month 列中的数据从元胞数组更改为分类数组。然后使用 reordercats 函数对类别重新排序。可以将这些函数应用于工作区中的表 ( tbl ) 或 HeatmapChart 对象的SourceTable 属性中存储的表 ( h.SourceTable )。将这些函数应用于 HeatmapChart 对象中存储的表可避免影响原始数据。
h.SourceTable.Month = categorical(h.SourceTable.Month);
neworder = {'January','February','March','April','May','June','July',...
'August','September','October','November','December'};
h.SourceTable.Month = reordercats(h.SourceTable.Month,neworder);

        对于分类数组,同样可以使用 addcats removecats renamecats 函数添加、删除或重命名热图标签。
        也可以使用 HeatmapChart 对象的 XDisplayData YDisplayData 属性对轴值重新排序。
h.XDisplayData = {'January','February','March','April','May','June', ...
'July','August','September','October','November','December'};

1.4 修改标题和轴标签

        使用表格数据创建热图时,会自动生成热图的标题和轴标签。通过设置 HeatmapChart 对象的 TitleXLabel YLabel 属性,自定义标题和轴标签。例如更改标题和删除 x 轴标签。另外还可以更改字体大小。
h.Title = 'Average Temperatures';
h.XLabel = '';
h.FontSize = 12;

1.5 修改缺失数据元胞的外观

        由于缺少 2016 年 8 月至 2016 年 12 月的数据,因此这些元胞显示为缺失数据。使用
MissingDataColor MissingDataLabel 属性修改缺失数据元胞的外观。
h.MissingDataColor = [0.8 0.8 0.8];
h.MissingDataLabel = 'No Data';

1.6 删除颜色栏

        通过设置 ColorbarVisible 属性删除颜色栏。
h.ColorbarVisible = 'off';

1.7 设置元胞文本格式

        通过设置 CellLabelFormat 属性自定义每个元胞中显示的文本的格式。例如,显示不带小数点的值文本。
h.CellLabelFormat = '%.0f';

1.8 添加或删除轴值

        通过设置 XDisplayData 属性,仅显示每个季度的第一个月。通过设置 YDisplayData 属性,沿 y 轴添加年份 2017。将这些属性分别设置为 XData YData 中的值的子集、超集或置换。
h.XDisplayData = {'January','April','July','October'};
h.YDisplayData = {'2015','2016','2017'};

        由于没有与 2017 年关联的数据,因此热图单元格使用缺失数据颜色。

2.使用字符串数组创建文字云

        此示例说明如何通过将纯文本读入字符串数组、进行预处理并传递给 wordcloud 函数,使用纯文本创建文字云。如果您安装了 Text Analytics Toolbox™,则可以直接使用字符串数组创建文字云。有关详细信息,请参阅 wordcloud (Text Analytics Toolbox) (Text Analytics Toolbox)。
        使用 fileread 函数从莎士比亚的十四行诗中读取文本。
sonnets = fileread('sonnets.txt');
sonnets(1:135)
ans =
'THE SONNETS
by William Shakespeare
I
From fairest creatures we desire increase,
That thereby beauty's rose might never die,'
        使用 string 函数将文本转换为字符串。然后,使用 splitlines 函数按换行符对其进行拆分。
sonnets = string(sonnets);
sonnets = splitlines(sonnets);
sonnets(10:14)
ans = 5x1 string
" From fairest creatures we desire increase,"
" That thereby beauty's rose might never die,"
" But as the riper should by time decease,"
" His tender heir might bear his memory:"
" But thou, contracted to thine own bright eyes,"
        用空格替换一些标点字符。
p = ["." "?" "!" "," ";" ":"];
sonnets = replace(sonnets,p," ");
sonnets(10:14)
ans = 5x1 string
" From fairest creatures we desire increase "
" That thereby beauty's rose might never die "
" But as the riper should by time decease "
" His tender heir might bear his memory "
" But thou contracted to thine own bright eyes "
        将 sonnets 拆分为其元素包含单个单词的字符串数组。要完成此操作,需要将所有字符串元素合并成一个1×1 字符串,然后在空白字符处进行拆分。
sonnets = join(sonnets);
sonnets = split(sonnets);
sonnets(7:12)
ans = 6x1 string
"From"
"fairest"
"creatures"
"we"
"desire"
"increase"
        删除少于五个字符的单词。
sonnets(strlength(sonnets)<5) = [];
        将 sonnets 转换为分类数组,然后使用 wordcloud 进行绘图。此函数绘制 C 的唯一元素,大小与这些元素的频率计数对应。
C = categorical(sonnets);
figure
wordcloud(C);
title("Sonnets Word Cloud")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

配电网和matlab

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值