2012-08-26 8 views
1

히스토그램 그리기 및 상자 플롯 Matplotlib에 대한 질문이 있습니다.Matplotlib : 같은 차트에 막대 그래프와 상자 그림을 그릴 수 있습니까?

히스토그램과 상자 그림을 개별적으로 그릴 수 있음을 알고 있습니다. 제 질문은,이 웹 사이트에 표시된 차트와 같은 그래프에 그래프를 그릴 수 있습니까? Springer Images

대단히 감사합니다.

+0

예, 가능합니다. ['subplot'] (http://matplotlib.sourceforge.net/examples/pylab_examples/subplot_demo.html)을 참조하십시오. 각 서브 플롯에는 자체 축 (tick)이있을 필요가 없습니다. –

+0

또한, Springer 예제에 따라 축 중 하나를 공유하고 하위 그림 사이의 공백을 줄여야합니다. http://matplotlib.sourceforge.net/examples/pylab_examples/ganged_plots.html을 살펴보십시오. 미래를 위해 : http://matplotlib.sourceforge.net/gallery.html에는 수백 가지 예제가 있으며, 종종 그 중 하나가 원하는 것에 가깝습니다. – Evert

답변

1

matplotlib를 사용하여 여러 가지 방법으로이를 수행 할 수 있습니다. plt.subplots() 방법과 AxesGrid1gridspec 툴킷은 모두 매우 세련된 솔루션을 제공하지만 학습하는 데 시간이 걸릴 수 있습니다.

이것을하기위한 단순하고 강압적 인 방법은 수동으로 축 개체를 직접 그림에 추가하는 것입니다.

import numpy as np 
import matplotlib.pyplot as plt 

# fake data 
x = np.random.lognormal(mean=2.25, sigma=0.75, size=37) 

# setup the figure and axes 
fig = plt.figure(figsize=(6,4)) 
bpAx = fig.add_axes([0.2, 0.7, 0.7, 0.2]) # left, bottom, width, height: 
              # (adjust as necessary) 
histAx = fig.add_axes([0.2, 0.2, 0.7, 0.5]) # left specs should match and 
              # bottom + height on this line should 
              # equal bottom on bpAx line 
# plot stuff 
bp = bpAx.boxplot(x, notch=True, vert=False) 
h = histAx.hist(x, bins=7) 

# confirm that the axes line up 
xlims = np.array([bpAx.get_xlim(), histAx.get_xlim()]) 
for ax in [bpAx, histAx]: 
    ax.set_xlim([xlims.min(), xlims.max()]) 

bpAx.set_xticklabels([]) # clear out overlapping xlabels 
bpAx.set_yticks([]) # don't need that 1 tick mark 
plt.show() 
관련 문제