2013-06-04 2 views
2

나는이 코드를 사용하고 histogram2d 뻗어 :하기 matplotlib 수직

fig = plt.figure(num=2, figsize=(8, 8), dpi=80, 
       facecolor='w', edgecolor='k') 
x, y = [xy for xy in zip(*self.pulse_time_distance)] 
H, xedges, yedges = np.histogram2d(x, y, bins=(25, 25)) 
extent = [-50, +50, 0, 10] 
plt.imshow(H, extent=extent, interpolation='nearest') 
plt.colorbar() 

는 2 차원 히스토그램을 생산하기를, 그러나, 플롯은 수직으로 늘어 나는 단순히 제대로 크기를 설정하는 방법을 알아낼 수 없습니다 :

Matplotlib stretches 2d histogram vertically

답변

4

것들 "뻗어"된다 . 기본적으로 플롯의 종횡비가 1 (데이터 좌표) 인 이미지를 표시한다고 가정합니다.

이 동작을 비활성화하고 픽셀을 늘려 플롯을 채우려면 aspect="auto"을 지정하십시오.

import matplotlib.pyplot as plt 
import numpy as np 

# Generate some data 
x, y = np.random.random((2, 500)) 
x *= 10 

# Make a 2D histogram 
H, xedges, yedges = np.histogram2d(x, y, bins=(25, 25)) 

# Plot the results 
fig, ax = plt.subplots(figsize=(8, 8), dpi=80, facecolor='w', edgecolor='k') 

extent = [-50, +50, 0, 10] 
im = ax.imshow(H, extent=extent, interpolation='nearest') 
fig.colorbar(im) 

plt.show() 

enter image description here

그리고 우리는 단지 imshow 전화에 aspect="auto"를 추가하여 문제를 해결할 수 있습니다 : 예를 들어

, 당신의 문제를 재현하는 데 (당신의 코드 기준)

import matplotlib.pyplot as plt 
import numpy as np 

# Generate some data 
x, y = np.random.random((2, 500)) 
x *= 10 

# Make a 2D histogram 
H, xedges, yedges = np.histogram2d(x, y, bins=(25, 25)) 

# Plot the results 
fig, ax = plt.subplots(figsize=(8, 8), dpi=80, facecolor='w', edgecolor='k') 

extent = [-50, +50, 0, 10] 
im = ax.imshow(H, extent=extent, interpolation='nearest', aspect='auto') 
fig.colorbar(im) 

plt.show() 

enter image description here

2
확인

하지하지만 난 당신이 당신의 historgram2d 수정하려고한다고 생각 :

H, xedges, yedges = np.histogram2d(x, y, bins=(25, 25), range=[[-50, 50], [0, 10]]) 

것은 편집 : 나는 정확히 비율을 수정하는 방법을 발견하지 못했지만, 측면 = '자동'으로 바로 추측하기 matplotlib : 당신이 imshow를 사용하고 있기 때문에

plt.imshow(hist.T, extent=extent, interpolation='nearest', aspect='auto') 
+0

작동하지 않음 : - \ – tamasgal

+0

너무 늦었습니다 :( – bendaizer