2016-01-23 99 views
0

我有几个使用matplotlib创建的子图。一旦我绘制数据,我需要返回并在for循环中的数据点之间画线。我的数据文件很大,这需要蟒蛇很长一段时间...matplotlib加快循环中的绘图线

有没有办法加快速度?这里是我的代码:

def generateHistogram(x, y, ax): 
    x = np.log10(x) 
    xerror = [] 

    numData = len(x) 

    plt.rcParams['lines.solid_capstyle'] = 'butt' 

    for dataIndex in range(0, numData-1): 
     xerror.append(np.divide(np.subtract(x[dataIndex+1], x[dataIndex]), 2.0)) 

    for errorIndex in range(0, len(x)): 
     if (errorIndex == 0): 
      ax.semilogx((np.power(10, (x[errorIndex]-xerror[errorIndex])), np.power(10, x[errorIndex])), 
         (y[errorIndex], y[errorIndex]), linewidth=2, color='k') 

     if (errorIndex == len(xerror)): 
      ax.semilogx((np.power(10, x[errorIndex]), np.power(10, (x[errorIndex]+xerror[errorIndex-1]))), 
         (y[errorIndex], y[errorIndex]), linewidth=2, color='k') 

     if (errorIndex < len(xerror)): 
      ax.semilogx((np.power(10, x[errorIndex]), np.power(10, (x[errorIndex]+xerror[errorIndex]))), 
         (y[errorIndex], y[errorIndex]), linewidth=2, color='k') 
      ax.semilogx((np.power(10, (x[errorIndex+1]-xerror[errorIndex])), np.power(10, x[errorIndex+1])), 
         (y[errorIndex+1], y[errorIndex+1]), linewidth=2, color='k') 

      verticleLineXPos = np.power(10, (x[errorIndex]+xerror[errorIndex])) 
      ax.semilogx((verticleLineXPos, verticleLineXPos), (y[errorIndex], y[errorIndex+1]), 
         linewidth=2, color='k') 


    return xerror; 

这基本上借鉴了各自在我需要的位置次要情节(其中x轴是在semilogx规模)的线。你有任何改善表现的建议吗?

+0

你能否提供一个_minimal_例子,说明在这种情况下x和y是什么样的数据结构?如果这些是一维数组,你的第一个循环是'xerror = np.diff(x)/ 2'。你能发表你想要做什么的照片吗?你可能也想看看一个'LineCollection'艺术家。 – tacaswell

+0

x,y和xerror数据结构都只是一个浮点列表。这些循环用于计算我需要绘制的线的长度和位置。这个细节对这个问题并不重要。 我在问如何在matplotlib的情节中高效地绘制多条线。 LineCollection实际上在绘制每个表现的过程中是否会提高性能?有没有办法让速度更快? – grover

+0

这可能会更好地放在http://codereview.stackexchange.com – tom

回答