2017-05-02 1 views
0

いくつかのコードを整理してみると、非常に面倒です。目標は、1つのプロットに多数の線をプロットすることですが、データの構造を変更することができます。Python:Matplotlibを使って一度に多数の行リストをプロットしてください。

データは、いくつかの異なる方法で配置することができ... XS、及びYSの

lats[set][point] 
lons[set][point] 

または

lat2[collection][set][point] 
lon2[collection][set][point] 

あるいは単に通常のリスト、

私は希望pls.plot(x、y)にそれぞれの 'set'をxsまたはysのリストとしてプラグインします。私は、プロットしているデータに応じて、必要なだけ多くのリストを深く持ちたいと思っています。現在、タイプに基づいてソートするいくつかの複雑なチェックがありますが、コードのベースは以下のとおりです。

def plotter(xs,ys): 
    for x,y in zip(xs,ys): 
     if type(x[0]) in (list, tuple): 
      plotter(x,y) 
     else: 
      plt.plot(x,y) 

複雑になるのは、さまざまな深さのリストにスタイルを組み込むことです。

私はmatplotlib.collectionsを使用しようとしていましたが、正しく使用する方法がわかりませんでした。どんな助けもありがたいです

答えて

0

1つの解決策は、実際のデータを含むデータ配列のすべての可能な組み合わせのインデックスを生成することです。以下は、複雑なように見えるかもしれないが、ほとんどの場合データを生成してプロットしているコード例です。

形態を有する3個のデータセット(あなたが示唆したように)、があります

LAT1 - ポイント

LAT3のセットの>コレクション - ポイント

LAT2のセット>は - ここ>点

コードである:

import matplotlib.pyplot as plt 
import numpy as np 
import itertools 

# First data type 
lat1 = np.array([np.linspace(0,180,100) for i in range(5)]) 
lon1 = np.array([10+5*np.random.random(100) for i in range(5)]) 
# lat1.shape = (5,100) 
# [set][point] 


# Second data type 
lat2 = np.array([np.linspace(0,180,100) for i in range(5*3)]).reshape((3,5,100)) 
lon2 = np.array([30+10*np.random.random(100) for i in range(5*3)]).reshape((3,5,100)) 
# lat2.shape = (3,5,100) 
# [collection][set][point] 


# Third data type 
lat3 = np.linspace(0,180,100) 
lon3 = 50+5*np.random.random(100) 
# lat3.shape = (100,) 
# [point] 


def plotter(xs,ys,ax,**kwargs): 
    # Get number of dimensions 
    ndim = xs.ndim 

    # Iterate over each dimension, generating all indices 
    if ndim>1: 
     indices = [np.arange(0,i,1) for i in xs.shape[:-1]] 
     comb = list(itertools.product(*indices)) 
    else: 
     # This is to deal with one dimensional data (i.e. just a list) 
     comb = [slice(0, xs.shape[0])] 

    for c in comb: 
     xx = xs[c] 
     yy = ys[c] 
     ax.plot(xx, yy, **kwargs) 

    return ax 

fig = plt.figure() 
ax = fig.add_subplot(111) 
plotter(lat1, lon1, ax, c='r', label='Type 1') 
plotter(lat2, lon2, ax, c='b', label='Type 2') 
plotter(lat3, lon3, ax, c='g', label='Type 3') 

box = ax.get_position() 
ax.set_position([box.x0, box.y0, box.width * 0.8, box.height]) 
ax.legend(fontsize=8,bbox_to_anchor=(1,1)) 

ax.set_ylim(0,60) 
ax.set_xlim(0,180) 

fig.show() 

これは次の図を示します。enter image description here

関連する問題