Use MATLAB to draw SCI papers

Starting from gcf and gca

Whether it is Python drawing or Matlab drawing, these two words will be used if you want to get a better-looking picture.

gcf: get current figure, is the graphics handle object of the target image
gca: get current axes, is the coordinate axis handle object of the target image

Matlab also supports object-oriented programming. After opening a drawing window or drawing a picture in Matlab, input gcfand gcawill get the properties of the current image object or the current coordinate axis object.

gcf
gca
In fact, when MATLAB creates a plot, it creates a series of graphics objects, objects such as figures, axes, lines, fills, text, and legends.
The following example has three graphics objects -- an axes, a line, and a text object, using the optional output parameter to store the created graphics objects.

x = -pi:pi/20:pi;
y = sin(x);

f = figure;
p = plot(x,y);
txt1 = text(0.2,0,'sin(x)');

sin(x)
All graphics objects have properties that can be viewed and modified, and which have default values.

To access individual properties of a graphics object, use the dot notation syntax object.PropertyName. For example, returns the LineWidth property of a line object.

pcol = p.LineWidth

Change the color of the line to red by setting its Color property.

p.Color = 'red';

Generic functions get and set

When viewing and modifying many properties of an object, the above method is not applicable. There are getand setfunctions in Matlab for obtaining and setting properties of an object.

Instructions:

v = get(h): Return all properties and property values ​​of the graphic object identified by h, v is a structure;
v = get(h,propertyName1,propertyName2,···): Return the value of specific properties propertyName1, propertyName2, ..., the property names must be enclosed in single quotation marks when using; when
using getand setfunctions , MATLAB recognizes an attribute case-insensitively. Also, just use enough characters to uniquely identify an attribute name. For example, the position property in an axes object can be called with 'Position', 'position', or even 'pos'.
set(H,propertyName1,Value1,propertyName2,Value2,···): Specifies the value of the attribute for the object identified by H
s = set(H): Returns the user-settable attribute and its possible value for the object identified by H.

parent class and child class

MATLAB arranges graphics objects in a hierarchy. At the top of the hierarchy is a special object called the graph root. To access the graph root, use the groot function.
groot defines the maximum szie that a figure can use. Below groot is the figure. Figue is the new dialog window that pops up when you draw a figure. If the figure changes, groot will not change accordingly. But if the groot changes, the figure will change accordingly.

Below the figure are axes, line, text, legend and so on. axes is what you want to draw in the figure window. The size and position of the axes depends on the figure. If the size of the figure is enlarged or reduced, the image inside will also change accordingly.

line, text, legend, etc. are what you add to the graph with instructions.

All graphics objects (except the root) have a parent. For example, the parent of the axes is a figure.

h.Parentand h.Childrento get the parent and child of the graphics object. For example the graph below has eight children - 5 axes objects, 2 legend objects, 1 colorbar object.

complex graph

>> f.Children

ans = 

    8×1 graphics 数组:

    Axes
    Axes
    Legend      (data1)
    Axes
    Axes
    ColorBar
    Legend      (data1, data2)
    Axes        (aaaa)

Because the figure has multiple children, the value of the Children property is an array of figure objects. To access individual children of figure, index into the array. You can then set properties on child objects.

t = f.Children(8);       % get the 'aaaa' Axes object
t.FontWeight = 'bold';    % set the font to bold
t.Children;              % view 'aaaa' Axes object's Children

ans = 

    2×1 Line 数组:

    Line    (data2)
    Line    (data1)

common attributes

After understanding the above knowledge, in theory, as long as you know the various properties of the graphic object, you can create the desired effect. In fact, when these attributes are known, it is also possible to add these parameters to plot, text, legend...
The common attribute table and optional parameters of common objects are given below

Figure common attributes

'Number': figure number
'Name': figure name
'Color': background color, specified as an RGB triplet, hexadecimal color code, color name, or short name
'Position': position of the drawable area and size, [left bottom width height]
'Units': unit of measurement, optional 'pixels' (default) | 'normalized' | 'inches' | 'centimeters' | 'points' | 'characters' 'MenuBar': Figure menu
bar Display mode, you can choose 'figure' (default) | 'none'
'ToolBar': Figure toolbar display, you can choose 'auto' (default) | 'figure' | 'none'
'Resize': Whether the figure can be resized , 'on' (default) | 'off'

Axes common attributes

'FontName': font name, optional supported font name | 'FixedWidth'
'FontWeight': character weight, 'normal' (default) | 'bold'
'FontSize': font size, numeric scalar
'FontAngle': character slant, 'normal' (default) | 'italic'
'XTick, YTick, ZTick': tick values, [] (default) | vector of increasing values
​​'XTickLabel, YTickLabel, ZTickLabel': tick labels, '' (default)| cell array of character vectors | string array | categorical array
The tick values ​​are where the tick marks appear on the axis, and the tick labels are the labels you see next to each tick mark.
'XLim, YLim, ZLim': minimum and maximum axis limits, [0 1] (default) | two-element vector of the form [min max] 'XAxisLocation': x-axis location, 'bottom' (default) | 'top
' | 'origin'
'YAxisLocation': y-axis location, 'left' (default) | 'right' | 'origin'
'XGrid, YGrid, ZGrid': grid lines, 'off' (default) | on
'GridLineStyle': style of gridlines, '-' (default) | '–' | ':' | '-.' | 'none' 'GridColor
': color of gridlines, [0.15 0.15 0.15] (default) | RGB triplet | hex color code | 'r' | 'g' | 'b' | ...
'Title': text object for the title
'Color': background color, [1 1 1] (default) | RGB triplet | hex color code | 'r' | 'g' | 'b' | ... 'LineWidth': line width, 0.5
( default) | positive value
'Box': box outline, 'off' (default) | 'on'
'Position': size and position, excluding label margins, [0.1300 0.1100 0.7750 0.8150] (default) | [left bottom width height] a four-element vector of the form

Legend common attributes

'Location': position relative to the axes, 'north' | 'south' | 'east' | 'west' | 'northeast' | ... 'Position'
: custom position and size, of the form [left bottom width height] 4-element vector
'Orientation': orientation, 'vertical' (default) | 'horizontal'
'NumColumns': number of columns, 1 (default) | positive integer
'FontName': font name, supported font names | 'FixedWidth'
'FontSize ': font size, scalar value greater than 0
'Color': background color, [1 1 1] (default) | RGB triplet | hex color code | 'r' | 'g' | 'b' | ...
'EdgeColor': Box outline color, [0.15 0.15 0.15] (default) | RGB triplet | Hex color code | 'r' | 'g' | 'b' | ... 'Box': Box
outline display, 'on' (default) | 'off'
'LineWidth': Width of the box outline, 0.5 (default) | positive value

line attribute

'Color': line color, [0 0 0] (default) | RGB triplet | hex color code | 'r' | 'g' | 'b' | ... 'LineStyle': line style, '
- ' (default) | '–' | ':' | '-.' | 'none'
'LineWidth': line width, 0.5 (default) | positive value
'Marker': marker symbol, 'none' (default) | ' o' | '+' | '*' | '.' | ...
'MarkerIndices': indices of data points to display markers, 1:length(YData) (default) | vector of positive integers | scalar of positive integers '
MarkerSize': markersize, 6 (default) | positive value

In fact, figure, axes, and legend all have attributes such as Position, FontName, Fontsize, and Color. Their meanings are the same, and the parameter forms are also the same. They just represent the attributes of this object and the attributes of that object.

Simulink Oscilloscope

The Simulink oscilloscope is also an object. Use the following command to open the drawing menu of the oscilloscope for drawing operations

shh = get(0,'ShowHiddenHandles');
set(0,'ShowHiddenHandles','On');
set(gcf,'menubar','figure');
set(gcf,'CloseRequestFcn','closereq');
set(gcf,'DefaultLineClipping','Off');
set(0,'ShowHiddenHandles',shh);

drawing example

The following is an example of the image code that draws a single column

clear all
figure(2)
set(gcf,'unit','centimeters','position',[10 5 8.4 10]); % 设置窗口位置[10cm,5cm]和大小8.4cm*10cm
set(gcf,'ToolBar','none','ReSize','off');   % 移除工具栏,不可调整大小
set(gcf,'color','w'); % 背景设为白色
% 以上也可在 编辑->图窗属性->窗口外观和位置 设置
 
t=0:0.1:1;
y1=t;
y2=t.^2;
y3=t+0.1;
y4=t.^4+0.1;
 
subplot(2,1,1) 
 
p1 = plot(t,y1,'b--','LineWidth',1.5);
hold on
p2 = plot(t,y2,'color',[0 0.81 0.82],'LineWidth',1.5);
set(gca,'Position',[0.14 0.52 0.83 0.33]);%第(1)个图的位置
g = get(p1,'Parent');%对应p1所在的坐标轴
set(g,'Linewidth',1.5,'FontSize',10,'FontName','Arial','FontWeight','bold');
ylabel('Error [m]','FontSize',10,'FontName','Arial','FontWeight','bold');
ylim([0,2])
%xlabel('t [s]','FontSize',10,'FontName','Arial','FontWeight','bold');
set(g,'XTick',[]); %隐藏X坐标轴

subplot(2,1,2) 

p3 = plot(t,y3,'r--','LineWidth',1.5);
hold on
p4 = plot(t,y4,'color',[0 0.3 0.82],'LineWidth',1.5);
set(gca,'Position',[0.14 0.16 0.83 0.33]);%第(3)个图的位置
g = get(p4,'Parent');%对应p1所在的坐标轴
set(g,'Linewidth',1.5,'FontSize',10,'FontName','Arial','FontWeight','bold');
ylim([0,2])
ylabel('Error [m]','FontSize',10,'FontName','Arial','FontWeight','bold');
xlabel({'t [s]','(a)'},'FontSize',10,'FontName','Arial','FontWeight','bold'); %{}作为换行给图标号
 
h1=legend([p1 p2 p3 p4],'\fontname{Arial}y_1','\fontname{Arial}y_2','\fontname{Arial}y_3',...
    '\fontname{Arial}y_4','Orientation','horizontal');
set(h1,'Linewidth',1.5,'FontSize',10,'FontWeight','bold');
set(h1,'position',[0.4,0.9,0.2,0.1]);%legend位置
set(h1,'Box','off');

When using Matlab to write the program code, if the number of lines is long, if the line is not changed, it will make the code difficult to read. At this time, the line can be changed. But the newline is not simply carried out with a carriage return, it needs to be added... to indicate the connection to the next line

Export graphs that can be used in SCI papers

First set the dpi at the rendering place. SCI papers require a dpi of more than 300, and then export to tiff format (this is recommended for SCI papers), so that the obtained pictures will not be blurred no matter how zoomed in. Writing papers with Latex can be exported as pdf or eps format.

dpi setting

tiff format

insert image description here
You can also export the command, print(figure_handle,fileformat,'-r300',filename), where the four parameters are:

a. figure_handle: figure handle

b. fileformat: single quote string, specify the storage format:
png format: '-dpng'
jpeg format: '-djpeg'
tiff format: '-dtiff'
pdf format: '-dpdf'
bmp format: '-dbitmap'
gif format : '-dgif'
emf lossless format: '-dmeta'

c. '-r300': expected dpi 300

d. filename: file name, which can include a path, if not set, it will be saved in the current folder.

Guess you like

Origin blog.csdn.net/a2360051431/article/details/131237419