2017-08-15 25 views
0

我通过一些数据试图循环,这是我的代码:matplotlib:循环虽然数据产生多个地块

fig = plt.figure(figsize=(8, 6), dpi=120) 
ax = fig.add_subplot(111) 
e_key_list = [0.14, 0.23, 0.41, 0.77, 1.26, 1.3, 1.7, 2.2, 3.0, 4.1, 5.8] 
e_width_ls = [0.09, 0.18, 0.36, 0.49, 66.74, 0.4, 0.5, 0.8, 1.1, 1.7, 7.0] 

for d in range(7): 
    for i in range(len(tme[0])): 
     bin_data = [] 
     if i == 4: 
      continue 
     #print i 
     for j in range(len(e_eng[i+1])): 
      if 4.2 <= x[j] <= 5 and int(day[0])+d == int(day[j]): 
       bin_data.append(e_eng[i+1][j] - e_bc[i+1][j]) 

     bin_data = np.array(bin_data) 
     ax.bar(e_key_list[i], np.mean(bin_data), color ='g', width = e_width_ls[i], edgecolor = 'k', align ='edge') 
    plt.show() 

我想d(这是指代表天)每次迭代产生一个图,但我只得到d = 0的图。另一方面,如果不包含plt.show(),我想要创建的所有7个图都显示在同一个直方图上。任何帮助将非常感激!

+0

你想每天有一个新图形(即新窗口),还是同一张图中并排多个图形? – pingul

+0

我正在考虑每天绘制一张新图。 – vanKoekje

回答

0

据我所知,你想有7个数字,每个都有一个barplot。要创建一个图形,请使用plt.figure。但是,不是只创建一个数字,而是需要7个数字。因此将图形创建放入循环中。

import matplotlib.pyplot as plt 
import numpy as np 

e_key_list = [0.14, 0.23, 0.41, 0.77, 1.26, 1.3, 1.7, 2.2, 3.0, 4.1, 5.8] 
e_width_ls = [0.09, 0.18, 0.36, 0.49, 66.74, 0.4, 0.5, 0.8, 1.1, 1.7, 7.0] 

for d in range(7): 
    fig = plt.figure(figsize=(8, 6), dpi=120) 
    ax = fig.add_subplot(111) 
    for i in range(5): 
     bin_data = [] 
     if i == 4: 
      continue 
     #print i 
     for j in range(5): 
      bin_data.append(j) 

     bin_data = np.array(bin_data) 
     ax.bar(e_key_list[i], np.mean(bin_data), color ='g', width = e_width_ls[i], edgecolor = 'k', align ='edge') 
plt.show() 

如果你把plt.show()外循环如上所有7个数字将立即显示出来。

+0

非常感谢!我不相信我错过了!这是一个漫长而令人沮丧的日子......再次感谢! – vanKoekje