2016-07-11 19 views
0

は、私が散布に色を設定するx**3を使用して、小さなスケールでの違いを強調したいが、私はx(ないx**3の値を表示したいと思います次のコードカラーバーの変更テキスト値matplotlibの

x = np.random.random((50))*2 -1 
y = np.random.random((50)) 
plt.scatter(x,y,c=x**3,cmap='viridis') 
cb = plt.colorbar() 

#I want a smarter (& working) version of this 
cb.ax.set_yticklabels(
    [str(np.cbrt(eval(i.get_text()))) for i in cb.ax.get_yticklabels()] 
    ) 

を考えます表示されたプロットのように)カラーバーに表示されます。

これは、逆関数(ここではcube-root)を使ってラベルを変更することで実現できると思います。 Matplotlibは一般的に丸め値を選択するという問題がありますが、一般的にこのような値は選択されません。

enter image description here

答えて

1

あなたはmatplotlib.tickerモジュールからFuncFormatterを使用してこれを行うことができます。例えば

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

x = np.random.random((50))*2 -1 
y = np.random.random((50)) 

plt.scatter(x,y,c=x**3,cmap='viridis',vmin=-1,vmax=1) 
cb = plt.colorbar() 

def label_cbrt(x,pos): 
    return "{:4.2f}".format(np.cbrt(x)) 

cb.formatter = ticker.FuncFormatter(label_cbrt) 
cb.update_ticks() 

plt.show() 

enter image description here

関連する問題