동시에 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'))입니다. 이미지 라이브러리에서 원하는 플롯을 보니 매우 실망 스럽지만 작동하지 않는 예제가 있습니다. 눈의 두 번째 세트는 크게 감사하겠습니다!
오류를 재현하고 _full_ 스택 추적을 지나쳐야하는 _minimum_만큼 코드의 양을 줄이십시오. – tacaswell
그리고 사용중인 matplotlib의 버전은 무엇입니까? 나는 당신이 벌레를 때렸다 고 생각합니다. – tacaswell
다음 번에 적은 코드를 포함하려고합니다. 대부분의 사람들은 내가 필요한 범위와 데이터를 보길 원하기 때문에 필요합니다. matplotlib 버전 1.2.1을 사용하고 있습니다. 전체 스택 추적이 포함되었습니다. 또한, 나는 단지 다른 컴퓨터에서 기본 matplotlibrc 파일과 비 아나콘다 패키지 배포본을 사용하여이 작업을 시도 했으므로 이들 중 아무 것도 원인이 아닌 것으로 보입니다. – astromax