2017-12-04 15 views
-1

このコードを使用すると、「3d」配列[X、Y、Z]のカラーマップをプロットすることができます(3つの単純なnp要素の配列です)。しかし、カラーバーの凡例の右側に縦書きのラベルを追加することはできません。matplotlibに垂直ラベルを追加するカラーマップの凡例

import numpy as np 
import matplotlib.pyplot as plt 

fig = plt.figure("Color MAP 2D+") 

contour = plt.tricontourf(X, Y, Z, 100, cmap="bwr") 

plt.xlabel("X") 
plt.ylabel("Y") 
plt.title("Color MAP 2D+") 

#Legend 
def fmt(x, pos): 
    a, b = '{:.2e}'.format(x).split('e') 
    b = int(b) 
    return r'${} \times 10^{{{}}}$'.format(a, b) 
import matplotlib.ticker as ticker 
plt.colorbar(contour, format=ticker.FuncFormatter(fmt)) 

plt.show() 

enter image description here

それはGoogleからの簡単な答えが得られないためにanoyingだ...誰かが私を助けることができますか?

+0

あなたのコードは、matplotlibの2.1.0を使用して私のために動作します。 –

+0

問題の説明を忘れてしまった。あなたが表示するコードの問題は何ですか?結果は何ですか?あなたが何を期待しているかは、どれくらいの距離であるのでしょうか。 [ask]と[mcve]を参照してください。 – ImportanceOfBeingErnest

+0

私は画像 –

答えて

2

colorbarオブジェクトにlabelを追加するには、この例を参照してください。ありがたいことに、colorbarにはset_labelという機能があります。要するに

:最小限のスクリプトで

cbar = plt.colorbar(contour, format=ticker.FuncFormatter(fmt)) 
cbar.set_label('your label here') 

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

X = np.random.uniform(-2, 2, 200) 
Y = np.random.uniform(-2, 2, 200) 
Z = X*np.exp(-X**2 - Y**2) 

contour = plt.tricontourf(X, Y, Z, 100, cmap="bwr") 

def fmt(x, pos): 
    a, b = '{:.2e}'.format(x).split('e') 
    b = int(b) 
    return r'${} \times 10^{{{}}}$'.format(a, b) 

cbar = plt.colorbar(contour, format=ticker.FuncFormatter(fmt)) 
cbar.set_label('your label here') 

plt.show() 

enter image description here

1

あなたのコードは機能していると思います。

import numpy as np 
import matplotlib.pyplot as plt 
from sklearn import datasets 

iris = datasets.load_iris().data 
X = iris[:,0] 
Y = iris[:,1] 
Z = iris[:,2] 

fig = plt.figure("Color MAP 2D+") 

contour = plt.tricontourf(X, Y, Z, 100, cmap="bwr") 

plt.xlabel("X") 
plt.ylabel("Y") 
plt.title("Color MAP 2D+") 

#Legend 
def fmt(x, pos): 
    a, b = '{:.2e}'.format(x).split('e') 
    b = int(b) 
    return r'${} \times 10^{{{}}}$'.format(a, b) 

import matplotlib.ticker as ticker 
plt.colorbar(contour, format=ticker.FuncFormatter(fmt)) 

plt.show() 

出力::あなたが探している

enter image description here

+0

で明確にしましたが、あなたのものと同じようにOPコードが動作しますが、あなたのものもカラーマップの右側に凡例が追加されていません(OPが最近の編集で投稿した画像を見てください)。 – gboffi

+0

ありがとうございますが、私は横に伝説を追加したいと思います。 –

+0

伝説に何を伝えたいですか?カラーバーはグラフのZ値の凡例です。 –

関連する問題