2016-10-23 13 views
2

matplotlib/seabornを使用してヒストグラムをプロットすると、x軸がlog2スケールに変更されます。私は値のLOG2を取り、それが動作ヒストグラムを作る際にプロットされたデータは、matplotlib/seabornのヒストグラムでlog2軸が機能しない

hereをしているが、私はログに記録されない値をプロットし、set_xscaleを使用して、x軸を変更した場合、それは間違った結果を与えます。コードは次のとおりです。

import numpy as np 
import seaborn as sns 
import matplotlib.pylab as plt 

df = pandas.read_table("data.csv",sep="\t") 
plt.figure() 
sns.set_style("ticks") 
ax1 = plt.subplot(2, 1, 1) 
plt.hist(df["y"]) 
ax1.set_xscale("log", basex=2) 
ax2 = plt.subplot(2, 1, 2) 
plt.hist(np.log2(df["y"])) 

プロット:

enter image description here

が、これはバグですか、私は間違って軸を変更しましたか?

答えて

4

どちらもありません!あなたはビンの数増やす場合は何が起こるかを参照してください:

plt.hist(df["y"], bins = 300) 
ax1.set_xscale("log", basex=2) 
ax2 = plt.subplot(2, 1, 2) 
plt.hist(np.log2(df["y"]), bins=300) 

logbins

ヒストグラムのデータは同じですが、ビンのサイズ分布は、トップケースでは、まだ線形です。

2つのケースを理想的にするにはどうすればいいですか? plt.histにログスペース内のカスタムビンサイズを渡します

plt.figure() 
sns.set_style("ticks") 
ax1 = plt.subplot(2, 1, 1) 
logbins = np.logspace(np.log2(df["y"].min()), 
         np.log2(df["y"].max()), 
         300, base=2) 
plt.hist(df["y"], bins = logbins) 
ax1.set_xscale("log", basex=2) 
ax2 = plt.subplot(2, 1, 2) 
plt.hist(np.log2(df["y"]), bins=300) 

fixed

あり、まだ二つのグラフの間にいくつかのマイナーな違いがありますが、私は彼らがあなたの元の問題に関連していないと信じています。

+0

まだ奇妙な動作です。 ggplot2がこれを自動的に処理します – mvd

+0

私は同意します。これはあまり使いやすいユースケースではありません。 –

+0

@mvd matplotlib開発チームは、http://github.com/matplotlib/matplotlib/ –

関連する問題