2013-08-26 2 views
2

동시에 Matplotlib의 2 차원 산점도 함수를 사용하여 일부 데이터를 플롯하려는 동시에 x 및 y 축에 투영 된 막대 그래프를 생성하려고합니다. 필자가 발견 한 예는 matplotlib 이미지 갤러리 (pylab_examples example code: scatter_hist.py)에서 가져온 것입니다.Matplotlib 분산 및 히스토그램

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 = 'horizontal'))입니다. 이미지 라이브러리에서 원하는 플롯을 보니 매우 실망 스럽지만 작동하지 않는 예제가 있습니다. 눈의 두 번째 세트는 크게 감사하겠습니다!

+1

오류를 재현하고 _full_ 스택 추적을 지나쳐야하는 _minimum_만큼 코드의 양을 줄이십시오. – tacaswell

+1

그리고 사용중인 matplotlib의 버전은 무엇입니까? 나는 당신이 벌레를 때렸다 고 생각합니다. – tacaswell

+0

다음 번에 적은 코드를 포함하려고합니다. 대부분의 사람들은 내가 필요한 범위와 데이터를 보길 원하기 때문에 필요합니다. matplotlib 버전 1.2.1을 사용하고 있습니다. 전체 스택 추적이 포함되었습니다. 또한, 나는 단지 다른 컴퓨터에서 기본 matplotlibrc 파일과 비 아나콘다 패키지 배포본을 사용하여이 작업을 시도 했으므로 이들 중 아무 것도 원인이 아닌 것으로 보입니다. – astromax

답변

5

v1.2.1 (https://github.com/matplotlib/matplotlib/pull/1985)의 버그가 발생했습니다. matplotlib를 업그레이드하고 monkey가 버그 수정으로 버전을 패치하거나 np.histogram을 사용하고 올바른 인수 순서로 barh을 호출 할 수 있습니다. 보조 노트로

는이 질문에 대한 필요한 유일한 코드는 다음과 같습니다 당신이 게시

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

모든 다른 소음입니다.

+0

사실, 오리엔테이션 = '가로'가 범인이라는 것을 알고 있기 때문입니다. 그 원인을 알았다면 관련 코드 스 니펫을 게시했을 것입니다. 그러나 그것이 나인, 사용자에 의해 야기되었다는 가능성은 실제 버그보다 훨씬 높았습니다. 따라서 제 질문에 대한 포괄적 인 시도가 필요합니다. 포인터를 가져 주셔서 감사합니다. 나는 너의 제안을 줄 것이다. – astromax

+1

@astromax 죄송합니다. 최근에 스트레스가 많은 부부가 있었으며 정말 괴팍합니다. – tacaswell

+1

걱정하지 마십시오. 개인적으로 받아들이지 않았습니다. 더 적은 정보보다는 더 많은 정보를 오류로 만들려고 노력하지만, 불필요한 코드 라인을 샅샅이 뒤질 필요가있는 것만 큼 작업하기에 부족한만큼 나쁠 수도 있습니다. – astromax