如何在python代码中使用LaTEX获取频率分布表?

时间:2015-04-13 04:28:43

标签: python pandas latex

for i in range(25):  
    str(i)+'-'+ str(i+1) - wind speed bins   
    for j in range(12):   
      fdh[i][j]=fd[i][j]/6.   
      print np.round(fdh[i][j],2) - frequency distribution values for 12 sectors  

我希望frequency distribution table有两列Wind Speed和Wind Direction扇区(0 30 60 90 120 150 180 210 240 270 300 330)。对于每个风速箱12,应将值打印为一行。下面是我在python中的输出。

0-1 m/s  3.83 6.0 6.33 3.83 4.0 3.5 4.33 2.5 2.67 4.5 8.5 6.33 

1-2 m/s  23.0 20.67 35.83 31.33 24.83 21.67 21.5 21.83 24.0 27.67 20.83 31.33 

2-3 m/s  39.67 37.5 68.17 66.0 54.0 35.0 41.83 54.33 54.5 49.5 49.0 50.17 

3-4 m/s  58.83 53.0 96.0 91.83 95.67 45.83 56.33 84.5 83.0 69.67 85.5 89.83 

4-5 m/s  60.0 69.67 82.83 109.83 125.83 58.67 50.33 147.67 136.33 80.17 92.33 86.67 

5-6 m/s  65.0 62.67 63.17 97.67 142.83 74.33 78.0 173.33 131.17 80.0 105.33 80.0 

6-7 m/s  64.17 56.83 45.0 79.33 137.83 80.0 84.83 204.17 131.33 87.67 92.17 67.5 

7-8 m/s  54.5 57.17 52.0 76.33 145.17 92.33 88.83 230.33 146.83 114.5 69.67 61.0 

8-9 m/s  52.83 42.0 40.0 83.5 130.83 71.67 98.0 246.83 170.0 152.5 46.83 56.83 

9-10 m/s  37.17 37.67 21.17 65.5 125.17 61.33 92.83 223.83 177.5 168.33 49.67 45.5 

10-11 m/s  28.67 26.17 12.83 60.0 100.83 48.17 77.33 204.0 193.33 144.5 51.67 46.5 

11-12 m/s  14.5 13.5 10.0 37.83 80.0 30.0 52.67 173.0 162.83 117.83 54.0 40.67 

12-13 m/s  8.5 8.17 6.0 25.33 42.5 18.17 33.0 144.5 149.67 99.83 34.0 30.33 

13-14 m/s  8.83 6.83 2.33 16.33 33.83 9.17 17.83 119.5 134.0 81.67 20.0 19.67 

14-15 m/s  10.67 4.0 2.67 11.67 18.33 5.33 10.5 91.83 112.5 66.0 13.0 17.5 

15-16 m/s  7.83 3.0 2.5 4.83 5.67 3.67 4.67 70.17 102.33 50.33 10.5 12.0 

16-17 m/s  5.33 0.5 2.17 1.0 1.67 0.83 2.33 50.0 76.83 47.17 12.33 5.67 

17-18 m/s  4.0 0.5 0.83 0.0 1.0 0.33 0.67 38.5 55.67 34.17 12.33 3.5 

18-19 m/s  1.83 0.83 1.5 0.0 0.17 0.0 0.33 23.67 44.0 37.0 6.33 3.67 

19-20 m/s  1.33 0.0 0.5 0.0 0.0 0.0 0.17 17.83 28.83 26.67 6.0 3.0 

20-21 m/s  1.0 0.33 0.5 0.0 0.0 0.0 0.0 11.17 22.17 26.0 2.83 1.17 

21-22 m/s  0.0 0.5 0.0 0.0 0.0 0.0 0.33 8.0 11.83 17.5 2.17 0.33 

22-23 m/s  0.17 0.17 0.67 0.0 0.0 0.0 0.0 4.67 11.0 16.17 1.33 0.0 

23-24 m/s  0.0 0.17 0.0 0.0 0.0 0.0 0.0 4.67 5.67 12.0 0.33 0.0 

24-25 m/s  0.0 0.0 0.0 0.0 0.0 0.0 0.0 5.33 4.67 5.83 0.0 0.0   

如何用python中的LaTEX代码编写频率表?这是我在python for pdf report中的乳胶代码。

outfile.write('\\begin{tabular}{|>{\centering}p{3.5cm}|>{\centering}p{3.5cm}|}'+'\n')  
outfile.write('\\hline'+'\n')  
outfile.write('\\small\\textbf{\\color{black!50!black}Wind Speed(m/s)} & \\small\\textbf{\\color{black!50!black}Wind Direction(deg)}\\tabularnewline'+'\n')
outfile.write('\\hline'+'\n')  
for i in range(25):  
     for j in range(12):  
         outfile.write('\\small + str(i)+'-'+ str(i+1)+' & '+ '%.2f'%(np.round(fdh[i][j],2))+' \\tabularnewline'+'\n')
     outfile.write('\\hline'+'\n')  
outfile.write('\\end{tabular}'+'\n')

以下是If进入最终报告的pdf频率分发表。

Wind Speed(m/s) Wind Direction(deg)  
0-1                   3.83  
0-1                   6.00  
0-1                   6.33  
0-1                   3.83  
0-1                   4.00  
0-1                   3.50  
0-1                   4.33  
0-1                   2.50  
0-1                   2.67  
0-1                   4.50  
0-1                   8.50  
0-1                   6.33  
1-2                   23.00    
1-2                   20.67  
and so on                 

请帮帮我..!

1 个答案:

答案 0 :(得分:0)

我认为你是否试图让LaTeX简单地让你回到原来的桌子?我在这个答案中假设了这一点。

您应该首先了解r'string'的工作原理。如果在字符串前放置r,则将其解释为原始字符串。你不需要逃避反斜杠。

outfile.write(r'\begin{tabular}{|>{\centering}p{3.5cm}|>{\centering}p{3.5cm}|}'+'\n')  
outfile.write(r'\hline'+'\n')  
outfile.write(r'\small\textbf{\color{black!50!black}Wind Speed(m/s)} & \small\textbf{\color{black!50!black}Wind Direction(deg)}\tabularnewline'+'\n')
outfile.write(r'\hline'+'\n')  
for i in range(25):  
     outfile.write(r'\small '+str(i)+ '-' + str(i+1) + ' & ')
     for j in range(12):  
         outfile.write('%.2f'%(np.round(fdh[i][j],2))+ ' & ')
     outfile.write(r'\hline'+r'\n')  
outfile.write(r'\end{tabular}'+r'\n')

如果我正确理解了您的问题以及您想要看到的内容,那么问题在于您每行只需要执行一些您想要完成的操作,并且每行需要多次执行一些操作,但它们全部出现在同一行中嵌套for循环。