2016-03-27 34 views
0

matplotlibnumpyを使用して図を作成するコード(コードが続きます)があります。私は私のプロットのための以下の要件を持っています:整数目盛りラベルmatplotlib、起点目盛りラベルなし

  1. 私は、軸が整数ティックを持つことを望みます。
  2. 軸の目盛は末尾にゼロを持たないので、たとえば1.0または2.0ではなく、12のいずれかを指定します。
  3. x=0y=0のティックは、隠されていなければなりません(原点ダニ)。

私はこのコードでこれを達成した:

import numpy 
import matplotlib.pyplot as plt 
import matplotlib as mp 
from matplotlib.ticker import FormatStrFormatter 

# approximately a circle 
x1 = [0.3, 1.0, 1.3, 1.3, 1.0, 0.3, -0.3, -1.0, -1.3, -1.3, -1.0, -0.3] 
y1 = [1.7, 1.2, 0.6, -0.2, -1.2, -1.5, -1.5, -1.2, -0.2, 0.6, 1.2, 1.7] 

number_of_values = 12 
x2 = numpy.random.uniform(low=-1.0, high=1.0, size=number_of_values) 
y2 = numpy.random.uniform(low=-1.0, high=1.0, size=number_of_values) 


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

ax.scatter(x1, y1, s=100, color='#FF0000', linewidth='1.5', marker='x', label='$y=1$') 
ax.scatter(x2, y2, s=100, facecolors='none', edgecolors='#66AAAA', linewidth='1.5', marker='o', label='$y=0$') 

ax.legend(loc='upper right') 
ax.grid(True) 

# LIMITS 
x_lower_limit = min(x1) - 0.2 * max(x1) 
x_upper_limit = 1.2 * max(x1) 
ax.set_xlim(-2.0, 2.0) 

y_lower_limit = min(y1) - 0.2 * max(y1) 
y_upper_limit = 1.2 * max(y1) 
ax.set_ylim(-2.0, 2.0) 

# ORIGIN AXIS 
ax.spines['left'].set_position('center') 
ax.spines['right'].set_color('none') 
ax.spines['bottom'].set_position('center') 
ax.spines['top'].set_color('none') 

ax.spines['left'].set_smart_bounds(True) 
ax.spines['bottom'].set_smart_bounds(True) 

ax.xaxis.set_ticks_position('bottom') 
ax.yaxis.set_ticks_position('left') 

ax.set_xlabel('$x_1$', fontsize=14)#, x=0.9, y=1, labelpad=0.6) 
ax.set_ylabel('$x_2$', fontsize=14, rotation=0)#, x=1, y=0.9, labelpad=0.6) 

ax.xaxis.set_label_coords(1.04, 0.5) 
ax.yaxis.set_label_coords(0.5, 1.02) 

ax.set_xticks([-2.0, -1.0, 1.0, 2.0]) 
ax.set_yticks([-2.0, -1.0, 1.0, 2.0]) 

plt.title('Non-linear Decision Boundary', y=1.06) 

plt.show() 

をしかし、あなたが見ることができるように、私はそこにダニをハードコーディングしています - 悪い習慣を。私はこのやり方を改善したいと思います。ここでの問題は、それが実際にしても動作しないこと、である

trimming trailing xtick zeros

:私は、これまでのところ、すなわち、以下のSOポストのソリューションを他の方法を試してみました。 ax.set_xticks([-2.0, -1.0, 1.0, 2.0])ax.set_yticks([-2.0, -1.0, 1.0, 2.0])のハードコードを削除すると、プロットは0.5のティックで表示され、末尾にゼロが表示されます。

modify tick labels text

これに伴う問題は、私は' 'のようなものに目盛りラベルを変更した場合、それはまだ0.0としてそれを解釈し、末尾のゼロと非整数値をレンダリングするだろうということ、です。

ティックラベルの値をハードコーディングすることを回避するにはどうすればよいですか?私はこのようないくつかの頻度を設定したいと思います:

ax.xaxis.set_ticks_frequency(1.0) 
ax.xaxis.set_ticks_formatter(something with integer values only) 

これはまた、すべての目盛りラベルに対して反復を避けるでしょう。必要に応じて、目盛りラベルを反復することは必要な悪ですが、私はそのような繰り返しがない方法があるべきだと思います。

答えて

0

あなたがハードコーディングされたダニ置き換えることができます:

import matplotlib.ticker as ticker 
loc = ticker.MultipleLocator(1) 
ax.xaxis.set_major_locator(loc) 
ax.yaxis.set_major_locator(loc) 
def fmt(x, pos): 
    return '' if x == 0 else '{:.0f}'.format(x) 
ax.xaxis.set_major_formatter(ticker.FuncFormatter(fmt)) 
ax.yaxis.set_major_formatter(ticker.FuncFormatter(fmt)) 

enter image description here

:ステップサイズ1(ゼロを省略する)カスタム FuncFormatterMultipleLocator

ax.set_xticks([-2.0, -1.0, 1.0, 2.0]) 
ax.set_yticks([-2.0, -1.0, 1.0, 2.0]) 

関連する問題