2014-11-21 1 views
0

Matplotlib (Python 2.7)에서 y 축을 따라 주 간격과 부 간격의 수를 설정하려고합니다. 간격 수를 설정하는 방법에 문제가 있습니다. 그들은 항상 주요 눈금 선에 맞춰 정렬되지는 않습니다. 간격을 설정하는 예를 here특정 메이저/마이너 틱 주파수에 대해 matplotlib에서 y 축 틱을 자동으로 설정합니다.

import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt 
from matplotlib.ticker import MultipleLocator 

# --------------USER INPUT SECTION---------------------- 
# Generating x and y values: 
ind_ex = np.arange(16) 
df2a = pd.DataFrame(np.random.randn(len(ind_ex), 5), columns = list('ABCDE'), index=ind_ex) 
x_var = np.arange(len(ind_ex)) 
x_var_plot = 'X variable' 
df2a.insert(0, x_var_plot, x_var) 
x_values = df2a[x_var_plot] #this is the x-variable (a list passed into a dataframe column) 
y_values = df2a[df2a.columns.values.tolist()[1:]] #columns 2 onwards are the y-variables (the list part, or the values, of a dataframe column) 
label_entries = df2a.columns.tolist()[1:] #label names are column names for column 2 onwards 

# Setting line thickness, length and width: 
thck_lines = 1 
len_maj_ticks = 10 #length of major tickmarks 
len_min_ticks = 5 #length of minor tickmarks 
wdth_maj_tick = 1 #width of major tickmarks 
wdth_min_tick = 1 #width of minor tickmarks 

# Setting y-axis major and minor intervals: 
y_axis_intervals = 10 #number of major intervals along y-axis 
y_minor_intervals = 5 #number of minor intervals along y-axis 
# ------------------------------------------------------ 

# Matplotlib (Plotting) section follows: 
fig = plt.figure(1) 
ax = fig.add_subplot(111) 
ax.plot(x_values, y_values, alpha=1, label = label_entries, linewidth = thck_lines) 

# Set the y-axis limits, for tickmarks, and the major tick intervals: 
starty, endy = ax.get_ylim() 
ax.yaxis.set_ticks(np.arange(starty, endy+1, (endy-starty)/y_axis_intervals)) 

# Set the y-axis minor tick intervals: 
minorLocatory = MultipleLocator((endy-starty)/y_axis_intervals/y_minor_intervals) #y-axis minor tick intervals 
ax.yaxis.set_minor_locator(minorLocatory) #for the minor ticks, use no labels; default NullFormatter (comment out for log-scaled y-axis) 

ax.grid(True) 
ax.tick_params(which='minor', length=len_min_ticks, width = wdth_min_tick) #length and width of both x and y-axis minor tickmarks 
ax.tick_params(direction='out') 
ax.tick_params(which='major', width=wdth_maj_tick) #width of major tickmarks 
ax.tick_params(length=len_maj_ticks) #length of all tickmarks 

plt.show() 

내가 만든 사용 : 여기

내가 틱 간격을 설정하고 플롯을 생성하기 위해 사용하고있는 코드입니다. 문제는, 제 경우에는, y 축의 주요 눈금 선이 항상 주요 눈금 간격으로 정렬되지 않는다는 것입니다 - 그림을보십시오. 이 코드를 4 ~ 5 번 실행하면이 문제가 발생합니다.

어떻게 주요 y 축 눈금 선이 눈금으로 올바르게 정렬되도록 y 축 간격을 올바르게 설정할 수 있습니까?

enter image description here

답변

0

나는 내 자신의 질문에 대답하는 나의 시도로이를 게시해야합니다 : 후

from matplotlib.ticker import MultipleLocator, AutoMinorLocator 

이 라인을 추가 (라인 41) :

난에 라인 (4) 변경을 40 행 :

ax.yaxis.set_minor_locator(AutoMinorLocator(y_minor_intervals)) 

40 행을 제거하면 40 행을 유지하는 것과 같은 결과를 얻으므로 40 행과 41 행 (두 번째 행은 여기)이 필요하거나 41 행이 맞는지 확실하지 않습니다. 개인적인. 그래서, 나는 정말로 나의 2 변화가 이것을하는 가장 좋은 방법인지 또는 그들이 어딘가에서 2 실수를하기 때문에 그들이 일하고 있는지 모른다.

누군가가 이것을 확인해 주면 정말 도움이 될 것입니다.

관련 문제