2014-07-25 4 views
2

이 질문은 이전 질문과 관련이 있는데 "matplotlib: Change grid interval and specify tick labels"이라고 물었습니다. 이제 x 및 y 축의 눈금을 변경하고 싶습니다. x 및 y 축의 범위를 설정 한 다음 주 눈금 및 보조 눈금의 간격을 지정하면 x 및 y 축을 동일하게 만듭니다.matplotlib : 주 눈금과 눈금 눈금을 모두 설정하면 같은 x 및 y 눈금이 표시됩니다.

이것은 내 코드입니다.

import matplotlib.pyplot as plt 
import numpy as np 

fig = plt.figure() 
ax = fig.add_subplot(111) 

for key, value in sorted(data.items()): 
    x = value[0][2] 
    y = value[0][3] 
    count = value[0][4] 

    ax.annotate(count, xy = (x, y), size = 3) 

plt.suptitle('Number of counts', fontsize = 12) 

ax.set_xlabel('x') 
ax.set_ylabel('y') 
ax.set_aspect('equal') 

# I want max x axis to be 500 
ax.set_xlim(0, 501) 
# I want max y axis to be 300 
ax.set_ylim(0, 301) 

# I want major ticks to be every 20 
major_ticks = np.arange(0, 501, 20) 

# I want minor ticks to be every 5 
minor_ticks = np.arange(0, 501, 5) 
# If I do minor_ticks = np.arange(0, 301, 5), I will not get minor ticks for the entire plot 

# Specify tick label size 
ax.tick_params(axis = 'both', which = 'major', labelsize = 4) 
ax.tick_params(axis = 'both', which = 'minor', labelsize = 0) 
# Suppress minor tick labels 

ax.set_xticks(major_ticks) 
ax.set_xticks(minor_ticks, minor = True) 
ax.set_yticks(major_ticks) 
ax.set_yticks(minor_ticks, minor = True) 

# Set both ticks to be outside 
ax.tick_params(which = 'both', direction = 'out') 

# Specify different settings for major and minor grids 
ax.grid(which = 'minor', alpha = 0.3) 
ax.grid(which = 'major', alpha = 0.7) 

filename = 'C:\Users\Owl\Desktop\Plot.png' 
plt.savefig(filename, dpi = 150) 
plt.close() 

이것은 내가 얻은 것입니다.

Output

어떻게 아직도 이런 크고 작은 눈금을 한 x와 y 축에 다른 범위를 얻을 수 있을까? 나는 아주 간단한 것을 놓치고 있을지도 모르지만 만약 누군가 그것을 지적 할 수 있다면, 나는 그것에 대해 매우 감사하고있다 !!

+0

귀하의 의견에 따르면 x 축 제한을 500으로 설정하려고했지만 실제로 301로 설정했습니다. – BrenBarn

+0

감사합니다. 방금 편집했습니다. 그것은 오타였습니다! – owl

+0

'suptitle'이 코드에서 실제로 작동합니까? –

답변

3

x와 y 틱을 같은 것으로 설정했기 때문입니다. 다른 크기를 원하면 다른 틱이 필요합니다. 두 진드기 세트를 동일한 major_ticks 위치로 설정할 수 없습니다. x 축의 눈금 위치 목록과 y 축의 눈금 위치 목록을 만들고 각 축의 눈금을 해당 목록으로 설정하십시오.

+0

감사합니다. 나는 그것들을 따로 설정하고 작동하도록했습니다! 참조 용으로 아래 코드를 게시 할 예정입니다. – owl

4

다음과 같이 코드를 업데이트 했으므로 이제 원하는 내용을 얻었습니다! 감사!

import matplotlib.pyplot as plt 
import numpy as np 

fig = plt.figure() 
ax = fig.add_subplot(111) 

for key, value in sorted(data.items()): 
    x = value[0][2] 
    y = value[0][3] 
    count = value[0][4] 

    ax.annotate(count, xy = (x, y), size = 3) 

plt.suptitle('Number of counts', fontsize = 12) 

ax.set_xlabel('x') 
ax.set_ylabel('y') 
ax.set_aspect('equal') 

# I want max x axis to be 500 
ax.set_xlim(0, 501) 
# I want max y axis to be 300 
ax.set_ylim(0, 301) 

# Set major ticks for x axis 
major_xticks = np.arange(0, 501, 20) 

# Set major ticks for y axis 
major_yticks = np.arange(0, 301, 20) 

# I want minor ticks for x axis 
minor_xticks = np.arange(0, 501, 5) 

# I want minor ticks for y axis 
minor_yticks = np.arange(0, 301, 5) 

# Specify tick label size 
ax.tick_params(axis = 'both', which = 'major', labelsize = 6) 
ax.tick_params(axis = 'both', which = 'minor', labelsize = 0) 
# Suppress minor tick labels 

ax.set_xticks(major_xticks) 
ax.set_xticks(minor_yticks, minor = True) 

ax.set_yticks(major_xticks) 
ax.set_yticks(minor_yticks, minor = True) 

# Set both ticks to be outside 
ax.tick_params(which = 'both', direction = 'out') 

# Specify different settings for major and minor grids 
ax.grid(which = 'minor', alpha = 0.3) 
ax.grid(which = 'major', alpha = 0.7) 

filename = 'C:\Users\Owl\Desktop\Plot.png' 
plt.savefig(filename, dpi = 150) 
plt.close()