3

y=f(x) と z=f(x) などの 3 列のデータ セットから単純な 2D プロットを作成しようとしています。xy をプロットし、色を使用して z を表示したいと考えています。たとえば、[x1,x2, min(y), max(y)] ... の間の長方形の領域は、z の値に応じて背景色で塗りつぶされます。fill_ between を使用しようとしましたが、カラーマップを関連付けることができませんでした。私はmatplotlibとpythonが初めてです。コメント/提案をお待ちしております。

編集:正確なプロットはありませんが、次の図の サンプルプロットを使用してクエリを説明しようとします

x=0.5 から x=1、z=1 x=1.0、から x=1.5、z=2 ....

したがって、x=0.5 から x=1 (最小(y) から最大(y))] を、z=1 に対応する色で、x=1、x=1.5、z=2 などの間でカバーしたいと思います。 .. カラーマップを使用してこのバリエーションを表示し、このカラーバーを右側に表示したいと考えています。

4

2 に答える 2

3

これは、contourf を使用できない、または他の理由で fill_between が必要な場合 (この場合は不規則なグリッド データの場合) の解決策です。

import numpy as np
import matplotlib.pyplot as plt
from random import randint, sample
import matplotlib.colorbar as cbar

# from Numeric import asarray
%matplotlib inline

# The edges of 2d grid
# Some x column has varying rows of y (but always the same number of rows) 
# z array that corresponds a value in each xy cell

xedges = np.sort(sample(range(1, 9), 6))
yedges = np.array([np.sort(sample(range(1, 9), 6)) for i in range(5)])
z = np.random.random((5,5))

f, ax = plt.subplots(1, sharex=True, figsize=(8,8))
f.subplots_adjust(hspace=0)
ax.set_ylabel(r'y')
ax.set_xlabel(r'x')
ax.set_ylim(0,10)
ax.set_xlim(0,10)

c = ['r','g','b','y','m']

normal = plt.Normalize(z.min(), z.max())
cmap = plt.cm.jet(normal(z))

# plot showing bins, coloured arbitrarily.
# I want each cell coloured according to z.
for i in range(len(xedges)-1):
    for j in range(len(yedges)):
        ax.vlines(xedges[i],yedges[i][j],yedges[i][j+1],linestyle='-')
        ax.hlines(yedges[i][j],xedges[i],xedges[i+1],linestyle='-')
        ax.vlines(xedges[i+1],yedges[i][j],yedges[i][j+1],linestyle='-')
        ax.hlines(yedges[i][j+1],xedges[i],xedges[i+1],linestyle='-')

        ax.fill_between([xedges[i],xedges[i+1]],yedges[i][j],yedges[i][j+1],facecolor=cmap[i][j][:])


cax, _ = cbar.make_axes(ax) 
cb2 = cbar.ColorbarBase(cax, cmap=plt.cm.jet,norm=normal) 

これは与える

z の関数として色付けされた xy グリッド

于 2016-06-28T03:53:24.883 に答える