Image Plot Using MATLAB
Some simple functions to plot an image using MATLAB.
-
self-defined color mapping
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% self-defined colorbar
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function clc_map = get_color_map(colors, clc_slices)
n_clc = size(colors,1);
clc_map = zeros(clc_slices * (n_clc - 1), 3);
for j = 1:(n_clc - 1)
for i = 1:3
clc_map(1 + (j - 1) * clc_slices: j * clc_slices, i) = linspace(colors(j, i), colors(j+1, i), clc_slices);
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% test code
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clc_slices = 64;
% some common colors : [1 1 1] white / [0 0 1] blue / [1 0 0] red
start_color = [0 0 1];
mid_color = [0.8500 0.3250 0.0980]
end_color = [1 0 0];
% row index : RGB ; column index : color
colors = [start_color; mid_color; end_color];
clc_map = get_color_map(colors, clc_slices); -
plot lines with color gradient and switch on cases
show / hide1
2
3
4
5
6
7
8
9%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% a single line case
% x_list : x values ; y_list : y values ; c_list : color values in RGB
% N : # of line points
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for j = 1:(N - 1)
plot(x_list(j:j+1), y_list(j:j+1),'Color', 1 / 2 * (c_list(j) + c_list(j+1)), 'LineWidth', 2)
hold on;
end -
2D Interpolant using MATLAB
show / hide1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 第一种方式是用interp2, 要求是meshgrid
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 第二种方式是用scatterInterpolant, 只要是二维数据即可,但必须是列向量形式输入
% 也可以用于三维数据的插值
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% before pressure
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% line_profile1 = interp2(carrier_density_before_array - carrier_density_shift_before, d_before_array, log_data, linspace(-0.8, 0.8, 17), zeros(17, 1));
carrier_density_before_list = reshape(carrier_density_before_array - carrier_density_shift_before, [num_TG_before * num_BG_before, 1]);
d_field_before_list = reshape(d_before_array, [num_TG_before * num_BG_before, 1]);
log_data_before_list = reshape(log_data_before, [num_TG_before * num_BG_before, 1]);
F_before = scatteredInterpolant(carrier_density_before_list, d_field_before_list, log_data_before_list);
% n = 0处的Lineprofile
Nq = zeros(1, 121);
Dq = linspace(-0.30, 0.30, 121);
line_profile_before = F_before(Nq, Dq);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% after pressure
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
carrier_density_after_list = reshape(carrier_density_after_array - carrier_density_shift_after, [num_TG_after * num_BG_after, 1]);
d_field_after_list = reshape(d_after_array, [num_TG_after * num_BG_after, 1]);
log_data_after_list = reshape(log_data_after, [num_TG_after * num_BG_after, 1]);
F_after = scatteredInterpolant(carrier_density_after_list, d_field_after_list, log_data_after_list);
% n = 0处的Lineprofile
Nq = zeros(1, 121);
Dq = linspace(-0.30, 0.30, 121);
line_profile_after = F_after(Nq, Dq);下面是我第一次写文章作图时用MATLAB写的一些代码
-
plot lines switch on cases
Image Plot Using MATLAB
http://example.com/2023/04/26/image_plot_matlab/