# How MATLAB draws a line chart and beautifies the line chart

When writing papers, the images we draw directly with MATLAB are always far behind those of the big guys. Now let's see how to beautify the pictures of scientific research papers.

## Draw the base image

The data we use, the saved file name is " d a t a A 1. c s v " "dataA1.csv" "dataA1.csv", for simplicity, I selected 10 rows of data, x = 1 : 10 x=1:10 x=1:10 is the same, y y The two columns of data with different y are as follows: Let's plot the next two columns of data today. First we import the data in MATLAB

```data=csvread('dataA1.csv');  %The data imported here can only contain numbers
```

There are other ways to import data here. If it is data in .xlsx format, use the following code

```data=xlsread("dataA1.xlsx")
```

Now we use the following code to draw a line chart,

```clc;clear; % to clear
%% Import data
% data=dataA1
%% Plot
figure
hold on  %Keep the interface from being refreshed
for i=1:size(data,2)-1
plot(data(:,1),data(:,i+1))
end
legend('GA','GB','Location','eastoutside')
```

The output results are saved in .png format. The picture is as follows: Next, let's see how to beautify the above picture.

The first method is to adjust through the GUI interface of MATLAB, that is, for the drawn picture, we click "Edit", and then click "Figure Properties". You can see the Property Inspector below. Here you can make various settings for the properties of the picture, and I won’t explain it in detail here. However, this is for the case where there are few pictures, but for when many pictures are to be drawn, how can you adjust the parameters one by one? It's not very troublesome, let's look at another method next.

The second way is to make adjustments by writing code. First of all, we first design the parameters of the image, as follows

```fontSize=28; %font text
lineWidthBox=1.5; %Box line width
lineWidth=2.5; %line width
symbol=['o','s']; %mark
markerSize=7; % marked size
pictureSize=[200,200,950,650]; % The size of the drawn picture, 200,200，The position of the lower left corner, representing the pixel value.  950,650 The length and height of the picture also represent the pixel value.
fileName='F2.png'; % Save the picture name
pictureResolution='-r600'; % The greater the precision, the clearer the picture.
color=[[0 0 139]/255;[0 139 139]/255];%2 A matrix with 3 rows and 3 columns
coordinateRange=[[0 10];[0 2500]];%coordinate range
xLabel='Time [t]'; %x Label
yLabel='Frequency [m/t]';% y Label
```

Here we set parameters such as the font of the picture, the line width of the Box, and the mark of the point. For the color, we can also choose the color we like. You can check the website https://tool.oschina.net/commons ?type=3. After adjusting the parameters, we draw

```%% Plot
figure
hold on  %Keep the interface from being refreshed
box on % box call out directly
set(gca,'LineWidth',lineWidthBox) %set up box line width
for i=1:size(data,2)-1
plot(data(:,1),data(:,i+1),'-','Color',color(i,:),'LineWidth',lineWidth,'Marker',symbol(1,i),'MarkerSize',markerSize,'MarkerFaceColor',color(i,:))
end  %draw pictures
legend(' GA',' GB','Location','eastoutside') %label, outside
legend('boxoff') %remove legend border
set(gca,'XLim',coordinateRange(1,:),'YLim',coordinateRange(2,:)); % coordinate range
xlabel(xLabel)
ylabel(yLabel )
set(gca,'FontName','Time New Roman','FontSize',fontSize) %Set all font sizes
```

set (set coordinate attributes), gca: indicates the current coordinate area or graph, FontName sets the font, commonly used ‘Times New Roman’; finally we save the picture, here gcf: Current figure handle (current image handle)

```%% Output  save
set(gcf,'Position',pictureSize) %The size of the output image size
print('-djpeg',fileName,pictureResolution); % Name, precision, output the picture
```

The complete code is as follows:

```clc;clear; % to clear
%% Import data
%% Parameter setting
fontSize=28; %font text
lineWidthBox=1.5; %Box line width
lineWidth=2.5; %line width
symbol=['o','s']; %mark
markerSize=7; % marked size
pictureSize=[200,200,950,650]; % The size of the drawn picture, 200,200，The position of the lower left corner, representing the pixel value.  950,650 The length and height of the picture also represent the pixel value.
fileName='F2.png'; % Save the picture name
pictureResolution='-r600'; % The greater the precision, the clearer the picture.
color=[[0 0 139]/255;[0 139 139]/255];%2 A matrix with 3 rows and 3 columns  https://tool.oschina.net/commons?type=3
coordinateRange=[[0 10];[0 2500]];%coordinate range
xLabel='Time [t]'; %x Label
yLabel='Frequency [m/t]';% y Label
%% Plot
figure
hold on  %Keep the interface from being refreshed
box on % box call out directly
set(gca,'LineWidth',lineWidthBox) %set up box line width
for i=1:size(data,2)-1
plot(data(:,1),data(:,i+1),'-','Color',color(i,:),'LineWidth',lineWidth,'Marker',symbol(1,i),'MarkerSize',markerSize,'MarkerFaceColor',color(i,:))
end  %draw pictures
legend(' GA',' GB','Location','eastoutside') %label, outside
legend('boxoff') %remove legend border
set(gca,'XLim',coordinateRange(1,:),'YLim',coordinateRange(2,:)); % coordinate range
xlabel(xLabel)
ylabel(yLabel )
set(gca,'FontName','Time New Roman','FontSize',fontSize) %Set all font sizes
%% Output  save
set(gcf,'Position',pictureSize) %The size of the output image size
print('-djpeg',fileName,pictureResolution); % Name, precision, output the picture
``` 