Article directory
Part.I Introduction
This article summarizes several ways to draw graphs with matrices in Matlab.
Chapter.I Preliminary knowledge
关于 *.mat
文訳
*.mat
The file is the standard format for matlab data storage. It is a standard binary file and can also be saved and loaded in ASCII code. When opened in MATLAB, the display is similar to a single-line EXCEL table. The syntax for loading and storing mat files is:
// 加载 mat 文件
load('C:\Users\Lenovo 110\Desktop\Clustering-master\data\toy_clustering.mat')
// 保存 mat 矩阵
// 第一个参数是文件路径,后面的参数矩阵名字,类似于load
// 当只有第一个参数的时候,会把工作空间所有的矩阵都存在一个文件中
save('C:\Users\Lenovo 110\Desktop\Clustering-master\data\mydata.mat','r2','r3')
Chap.II Overview
There are several main ways to use matrices to draw pictures:
// 方式 1 -- 常用
imshow(SST); // imshow(SST,[]);
// 方式 2
F1=mapminmax(SST); %F1 ∈[-1,1]
F1=F1.*0.5+0.5; %F1 ∈[0,1]
// 方式 3 -- 常用
imagesc(SST);
// 方式 4
contourf(SST,'LineStyle','none');
colormap(jet); colorbar
// 方式 5
pcolor(SST);
Chapter III Miscellaneous Notes
Here are some miscellaneous notes
R = corrcoef(cov1); // 求协方差矩阵的相关系数矩阵
Part.II Drawing with Matrix
Chap.I The process of exploration
1. First load a matrix:
You can see that there are nan and normal data in the matrix. In order to get an overview, I put these data into Excel, and then zoomed out as shown below:
Then let’s introduce the characteristics of the matrix:
[a,b]=size(m);
num=a*b-numel(find(isnan(a))); %矩阵中除了nan外所有元素的个数
m1=reshape(m,a*b,1); %搞成一个列向量
max1=max(m1); %计算最大值
min1=min(m1); %计算最小值
mean1=nanmean(m1); %计算除nan之外的均值
mode1=mode(m1); %除了nan 返回值最小的那个众数
med1=nanmedian(m1); %求除了nan的中位数
// -----------------------------------------
217x217
max1=20.21
min1=1.22
med1=7.055
Now that I have a general understanding of what the data looks like, let's start trying to draw a graph.
code | image |
---|---|
– | |
imshow(SST); |
|
imshow(SST,[]); |
|
F1=mapminmax(SST); %F1 ∈[-1,1] F1=F1.*0.5+0.5; %F1 ∈[0,1] % [y,ps] = mapminmax(SST); % ps.ymin = 0; % [y,ps] = mapminmax(SST,ps); %这三行与上面两行异曲同工 |
|
imagesc(SST); |
|
contourf(SST,'LineStyle','none');colormap(jet);colorbar |
|
pcolor(SST); |
|
A=[0.8 0.1 0.5 0.2 0.4 0.7 0.5 0.3 0.6]; imagesc(A); colormap jet colorbar; |
|
A=[0.8 0.1 0.5 0.2 0.4 0.7 0.5 0.3 0.6]; imagesc(A); colorbar caxis([0 1]) [M, N] = size(A); set(gca, 'XTick', 1:M,'XTickLabel', {'0','1','2'})%X坐标轴刻度数据点位置、字符 set(gca, 'YTick', 1:N, 'YTickLabel', {'0','1','2'}) loc = get(xlabel(''), 'position'); text(loc(1), loc(2), loc(3), 'Predicted label', 'HorizontalAlignment','center'); set(gca, 'XAxisLocation', 'top') ylabel('True label') title('Confusion matrix') |
Currently, I only know these types, and the sixth one is the most useful;imshow()
can only display grayscales between [0,1]
, greater than 1 Displayed in white; imagesc()
displays the image in color, with no value restrictions; colormap()
displays the image in color. For colormap, please refer to the following article: a>
https://blog.csdn.net/weixin_42943114/article/details/81811556
Chap.II Draw professional drawings
Let’s create a more professional graph below. The data used is still the matrix above217x217
.
The code is as follows:
X=117:0.0416666666666:126;
Y=33:0.0416666666666:42;
x0=2*24:2*24:217; % 每隔两度标一下
y0=2*24:2*24:217;
x=119:2:126; %标注用的
y=35:2:42; %标注用的
x1=sprintfc('%g',x);%转成字符串数组
y1=sprintfc('%g',y);%转成字符串数组
n=size(x1,2);
for i=1:n
x1{
1,i}=[x1{
1,i},'°E'];
y1{
1,i}=[y1{
1,i},'°N'];
end
figure1 = figure;
ax2=axes('Parent',figure1);;%有子图时,ax2=subplot(2,3,2);
contourf(SST1,'LineStyle','none')
colormap(jet);
set(gca,'XTick',x0,'XTicklabel',x1); %设置x,y轴
set(gca,'YTick',y0,'YTicklabel',y1);
set(gca,'FontName','Times New Roman'); %更改字体为罗马
xlabel('(a) SST');
set(ax2,'XAxisLocation','top');%把x坐标轴搞到上面去
h=colorbar;
set(get(h,'Title'),'string','K'); %给colorbar加上单位
result:
Chapter III matrix to tiff
Still using the matrix in 2
GeoRef = georasterref('Rastersize',size(SST'),'Latlim',[double(min(Lat)),double(max(Lat))],'Lonlim',[double(min(Lon)),double(max(Lon))]);
%数据的写出
% data = flipud(SST');
SST_Tif = ['C:\Users\hp\Documents\MATLAB\','A20160922016121_01.tif'];
geotiffwrite(SST_Tif,flipud(SST'),GeoRef)
Pay attention to the front and back of the picture. The following picture is from the front: