Mathematical modeling has many reused basic codes. Here is a brief summary of the code in Python and Matlab. The summary will be updated in real time.
-
python (pandas)
File Vocal (Extended Name) is not necessary. The main role is to prompt the system to open, and on the other hand, it prompts the file content format. Such as
.txt
,.csv
,.tsv
Files are pure text files, just.csv
,.tsv
explain the division method of data is,
与#\t
. Since they are all text files, it is availablepandas.read_csv
orpandas.read_table
and wait for reading, here it is usedpandas.read_csv
。.txt
fileimport pandas as pd tsvfile = pd.read_csv('filename.txt') tsvfile = pd.read_csv('filename.txt',skiprows=1)# 11 11 11
.csv
fileimport pandas as pd tsvfile = pd.read_csv('filename.csv') tsvfile = pd.read_csv('filename.csv',skiprows=1)# 1 1 1
.tsv
fileimport pandas as pd tsvfile = pd.read_csv('filename.tsv', sep='\t')
.json
fileimport pandas as pd jsonfile = pd.read_json('filename.json', orient = 'records')
.csv
File Turn.json
fileimport csv import json csvfile = open('filename.tsv',r) jsonfile = open('filename.json',w) fieldnames = ("key1","key2","key3") reader = csv.DictReader(csvfile,fieldnames) for row in reader: json.dump(row,jsonfile) jsonfile.write('\n')
.xlsx
fileExcel is a binary file. It preserves the information of all worksheets in the workbook and can also operate the data.
import pandas as pd # Read Excel data, select Sheet1 worksheet sheet_1 = pd.read_excel('demo.xlsx', sheet_name='Sheet1', na_values='n/a') # Print sheet table name print(pd.ExcelFile('listings.xlsx').sheet_names) # Print the data head print(sheet_1.head())
.xlsx
file transfer.csv
import pandas as pd def xlsx_to_csv_pd(): data_xls = pd.read_excel('demo.xlsx', index_col=0) data_xls.to_csv('demo.csv', encoding='utf-8')
.csv
File Turn.xlsx
import pandas as pd def csv_to_xlsx_pd(): csv = pd.read_csv('1.csv', encoding='utf-8') csv.to_excel('1.xlsx', sheet_name='data')
-
MATLAB
The same reason, Matlab reads text files available
textscan
。.txt
fileclc;clear; filename = 'filename.txt'; file = fopen(filename);%Open the file columns= 's%s%s%s%s%s%';%Read a few columns, there are a few columns's%' data = textscan(filename,columns,'delimiter', ' ');%Separate with table makingfclose(file);
.csv
fileclc;clear; filename = 'filename.csv'; file = fopen(filename);%Open the file columns= 's%s%s%s%s%s%';%Read a few columns, there are a few columns's%' data=textscan(filename,columns,'delimiter', ',');%to,fclose(file);
.tsv
fileclc;clear; filename = 'filename.tsv'; file = fopen(filename);%Open the file columns= 's%s%s%s%s%s%';%Read a few columns, there are a few columns's%' data=textscan(filename,columns,'delimiter', ' ');%Divided by watchmakingfclose(file);
.json
filematlab read
.json
files need to download the jsonlab package.clc;clear; addpath('E:\PIR\PIR_V3.0\jsonlab-1.5'); %Add JSONLAB package storage path filename= 'filename.json'; %file name jsondata= loadjson(filename);%jsondata is the Struct structure Data= jsonData.u';
-
MATLAB
plot(xi,yi,'>','Color',[x/255 x/255 x/255]);%Right triangle,color is(x,x,x) %symbol can be'o','.','+','>','<'et al.xlabel('x/x') ylabel('y/y') title('Title') set(gcf,'unit','normalized','position',[0.2,0.2,0.8,0.6]);%fixed size
Folding line diagram
xi= 1: 0.25:76; yi = interp1(X,Y,xi,'spline');%Insert, the step is1to become0.25 plot(xi,yi,'Color',[x/255 x/255 x/255],'LineWidth',1);%color is(x,x,x),line thickness is1 xlabel('x/x') ylabel('y/y') title('Title') set(gcf,'unit','normalized','position',[0.2,0.2,0.8,0.6]);%fixed size
bar chart
y=[1 2 3,1 2 3];%packet bar charttiledlayout(2,1)%Specify the vertical and horizontal ratiobar(y); bar(x,y); bar(y,'stacked');%and y=[1 2 3,1 2 3]Combination, the same pillar shape layered displaybar(x,y,0.6);%relative width control intervalbar(y,'FaceColor',[0 .5 .5],'EdgeColor',[0 .9 .9],'LineWidth',1.5);%Multi -parameter Y= [10 15 20; 30 35 40; 50 55 62]; b = bar(y); b(3).FaceColor = [.2 .6 .5];%The third column of each group is set to green
Other features
%Draw multiple photosfigure(i); %plot hold on; figure(i+1); %plot %Multi -threading%plot hold on; %plot
-
python
import matplotlib.pyplot as plt plt.rcParams['figure.figsize'] = (48.0, 30.0) # Set Figure_size size plt.plot(X,Y,'.') plt.xlabel("x-label",fontproperties=zhfont,fontsize='32') plt.ylabel("y-label",fontproperties=zhfont,fontsize='32') plt.title("title",fontproperties=zhfont,fontsize='32')
folding line map
import matplotlib.pyplot as plt plt.rcParams['figure.figsize'] = (48.0, 30.0) # Set Figure_size size plt.plot(X,Y) plt.xlabel("x-label",fontproperties=zhfont,fontsize='32') plt.ylabel("y-label",fontproperties=zhfont,fontsize='32') plt.title("title",fontproperties=zhfont,fontsize='32')
Other common functions
# Draw multiple photos plt.subplot(221) # The first one in the two lines and two columns plt.plot(X1,Y1,'.') plt.subplot(222) # The second one of the two lines and two columns plt.plot(X2,Y2,'.') plt.subplot(223) # The third of the two lines and two columns plt.plot(X3,Y3,'.') plt.subplot(224) # The fourth line of two lines and two columns plt.plot(X4,Y4,'.') # Multi -picture plt.plot(X1,Y1,'.') plt.plot(X2,Y2,'.') # Settings resolution ## Drawing resolution plt.rcParams['figure.figsize'] = (24.0, 20.0) # The default pixel is [6.0,4.0], the resolution is 100, and the picture size is 600 & 400 plt.rcParams['figure.dpi'] = 300 # Directly set the resolution, generally use one of these two methods ## Save resolution plt.rcParams['savefig.dpi'] = 300 # Pre -set to save image pixels plt.savefig(‘demo.jpg', dpi=200) # Specify the resolution when saving, one of these two methods is generally used # title format ## 8 `` `` `Code Cow" blog plt.title('Interesting Graph',fontsize='large',fontweight='bold')Set the font size and format PLT.title('Interesting Graph',color='blue')Set font color PLT.title('Interesting Graph',loc ='left')Set font position PLT.title('Interesting Graph',verticalalignment='bottom')Set vertical alignment method PLT.title('Interesting Graph',rotation=45)Set the font rotation angle PLT.title('Interesting',bbox=dict(facecolor='g', edgecolor='blue', alpha=0.65 ))Title Border
-
Common color matching
#5d7a9a | #ec554a | #ffad60 | #8bc24c | #2d2d2d |
#bc8420 | #593e1a | #ffeb28 | #996699 | #0fff95 |