2016-04-24 22 views
4

バープロットの各バーの幅は、列chromに特定の値が設定されている回数に基づいて設定します。これは私にエラーを与えるシーボーンバープロットの幅を設定する方法

ax = sns.barplot(x = plot_data['chrom'], y = plot_data['dummy'], width=widthbars) 

:として

barplotをプロット
list_counts = plot_data.groupby('chrom')['gene'].count() 

widthbars = list_counts.tolist() 

TypeError: bar() got multiple values for keyword argument 'width' 

は幅可変されている 私はオカレンスのリストであることを幅のバーを設定しています暗黙のうちにどこかに置く? 各バーの幅をどのように異ならせることができますか?

答えて

4

seabornでこれを行うには、組み込みの方法はありませんが、あなたはsns.barplotは軸がオブジェクトmatplotlibの上で作成したパッチを操作することができます。

seaborn example for barplot hereに基づいて、これを行う方法の最小例を以下に示します。

各バーには1単位幅のスペースが割り当てられているので、カウントを0-1の間隔に正規化することが重要です。

import matplotlib.pyplot as plt 
import seaborn as sns 

sns.set_style("whitegrid") 
tips = sns.load_dataset("tips") 
ax = sns.barplot(x="day", y="total_bill", data=tips) 

# Set these based on your column counts 
columncounts = [20,40,60,80] 

# Maximum bar width is 1. Normalise counts to be in the interval 0-1. Need to supply a maximum possible count here as maxwidth 
def normaliseCounts(widths,maxwidth): 
    widths = np.array(widths)/float(maxwidth) 
    return widths 

widthbars = normaliseCounts(columncounts,100) 

# Loop over the bars, and adjust the width (and position, to keep the bar centred) 
for bar,newwidth in zip(ax.patches,widthbars): 
    x = bar.get_x() 
    width = bar.get_width() 
    centre = x+width/2. 

    bar.set_x(centre-newwidth/2.) 
    bar.set_width(newwidth) 

plt.show() 

enter image description here

+1

ニース。質問には幅のカウントメジャーを使用することになっているので、各バーに1単位幅のスペースが割り当てられているので、幅測定にある種の正規化を追加すると良いでしょう。さもなければプロットは解釈不能。 – mwaskom

+1

提案に感謝します。私の編集はそれもカバーしていると思う – tom

関連する問題