2

matplotlib を使用して異なる行の長さでヒート マップをプロットする方法はありますか?

このような:

plt.imshow( [ [1,2,3], [1,2], [1,2,3,4,5,6,7], [1,2,3,4]])
plt.jet()
plt.colorbar()
plt.show()

希望の画像

4

1 に答える 1

2

欲しいイメージを考えると、plt.pcolormeshむしろ欲しいと思ってもらえると思いますがimshow、間違っているかもしれません。いずれにせよ、私は個人的に配列をパディングする関数を作成し、マスクを使用してそれらのポイントをプロットしたりプロットしimshowたりしpcolormeshません。例えば

import matplotlib.pylab as plt
import numpy as np

def regularise_array(arr, val=-1):
    """ Takes irregular array and returns regularised masked array

    This first pads the irregular awway *arr* with values *val* to make 
    it of rectangular. It then applies a mask so that the padded values
    are not displayed by pcolormesh. For this reason val should not
    be in *arr* as you will loose these points.
    """

    lengths = [len(d) for d in data]
    max_length = max(lengths)
    reg_array = np.zeros(shape=(arr.size, max_length))

    for i in np.arange(arr.size):
        reg_array[i] = np.append(arr[i], np.zeros(max_length-lengths[i])+val)

    reg_array = np.ma.masked_array(reg_array, reg_array == val)

    return reg_array

data = np.array([[1,2,3], [1,2], [1,2,3,4,5,6,7], [1,2,3,4]])

reg_data = regularise_array(data, val=-1)

plt.pcolormesh(reg_data)
plt.jet()
plt.colorbar()
plt.show()

ここに画像の説明を入力

これに関する問題は、それらが配列にないことに注意する必要があることですval。これに簡単なチェックを追加するか、使用しているデータに基づいてチェックすることができます。for ループはおそらくベクトル化できますが、その方法はわかりません。

于 2014-07-17T13:43:35.663 に答える