4

Matplotlib の 2d scatterplot 関数を使用していくつかのデータをプロットしようとしていますが、同時に x 軸と y 軸に投影されたヒストグラムを作成しています。私が見つけた例は、matplotlib イメージ ギャラリー ( pylab_examples サンプル コード: scatter_hist.py ) からのものです。

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

# the random data
x = np.random.randn(1000)
y = np.random.randn(1000)

nullfmt   = NullFormatter()         # no labels

# definitions for the axes
left, width = 0.1, 0.65
bottom, height = 0.1, 0.65
bottom_h = left_h = left+width+0.02

rect_scatter = [left, bottom, width, height]
rect_histx = [left, bottom_h, width, 0.2]
rect_histy = [left_h, bottom, 0.2, height]

# start with a rectangular Figure
plt.figure(1, figsize=(8,8))

axScatter = plt.axes(rect_scatter)
axHistx = plt.axes(rect_histx)
axHisty = plt.axes(rect_histy)

# no labels
axHistx.xaxis.set_major_formatter(nullfmt)
axHisty.yaxis.set_major_formatter(nullfmt)

# the scatter plot:
axScatter.scatter(x, y)

# now determine nice limits by hand:
binwidth = 0.25
xymax = np.max( [np.max(np.fabs(x)), np.max(np.fabs(y))] )
lim = ( int(xymax/binwidth) + 1) * binwidth

axScatter.set_xlim( (-lim, lim) )
axScatter.set_ylim( (-lim, lim) )

bins = np.arange(-lim, lim + binwidth, binwidth)

axHistx.hist(x, bins=bins)
axHisty.hist(y, bins=bins, orientation='horizontal')

axHistx.set_xlim( axScatter.get_xlim() )
axHisty.set_ylim( axScatter.get_ylim() )

plt.show()

唯一の問題は、例が機能しないことです。次のエラーが表示されます。

~$ python ~/Desktop/scatter_and_hist.py 
Traceback (most recent call last):
  File "/Users/username/Desktop/scatter_and_hist.py", line 45, in <module>
    axHisty.hist(y, bins=bins, orientation='horizontal')
  File "//anaconda/lib/python2.7/site-packages/matplotlib/axes.py", line 8180, in hist
    color=c, bottom=bottom)
TypeError: barh() got multiple values for keyword argument 'bottom'

コードを調べて、問題を特定しました。問題を引き起こしているのは 45 行目 (axHisty.hist(y, bins=bins, orientation='horizo​​ntal')) です。画像ライブラリに必要なプロットが表示されているのに、例が機能しないのは非常にイライラします。2番目の目のセットをいただければ幸いです。

4

1 に答える 1

5

v1.2.1 ( https://github.com/matplotlib/matplotlib/pull/1985 ) でバグに遭遇しました。matplotlib をアップグレードするか、バージョンにバグ修正を適用してモンキー パッチを適用するか、正しい順序の引数で自分自身を使用np.histogramして呼び出すことができます。barh

補足として、この質問に必要な唯一のコードは次のとおりです。

x = np.random.rand(100)
plt.hist(x, orientation='horizontal')
plt.show()

あなたが投稿した他のすべてはノイズです。

于 2013-08-26T21:14:55.633 に答える