50 lines
1.4 KiB
Python
50 lines
1.4 KiB
Python
import numpy as np
|
|
import matplotlib.pyplot as plt
|
|
|
|
plt.xkcd()
|
|
fig = plt.figure( figsize=(6,4) )
|
|
|
|
# the line:
|
|
slope = 2.0
|
|
xx = np.arange(0.0, 4.1, 0.1)
|
|
yy = slope*xx
|
|
# the data:
|
|
n = 80
|
|
rng = np.random.RandomState(218)
|
|
sigma = 1.5
|
|
x = 4.0*rng.rand(n)
|
|
y = slope*x+rng.randn(n)*sigma
|
|
# fit:
|
|
slopef = np.sum(x*y)/np.sum(x*x)
|
|
yf = slopef*xx
|
|
|
|
# plot it:
|
|
ax = fig.add_subplot( 1, 1, 1 )
|
|
ax.spines['left'].set_position('zero')
|
|
ax.spines['bottom'].set_position('zero')
|
|
ax.spines['right'].set_visible(False)
|
|
ax.spines['top'].set_visible(False)
|
|
ax.get_xaxis().set_tick_params(direction='inout', length=10, width=2)
|
|
ax.get_yaxis().set_tick_params(direction='inout', length=10, width=2)
|
|
ax.yaxis.set_ticks_position('left')
|
|
ax.xaxis.set_ticks_position('bottom')
|
|
ax.set_xticks(np.arange(0.0, 4.1))
|
|
ax.set_xlim(0.0, 4.2)
|
|
#ax.set_ylim(-1, 5)
|
|
#ax.set_xticks( np.arange(0, 5))
|
|
#ax.set_yticks( np.arange(0, 0.9, 0.2))
|
|
ax.set_xlabel('x')
|
|
ax.set_ylabel('y')
|
|
#ax.annotate('', xy=(mu, 0.02), xycoords='data',
|
|
# xytext=(mu, 0.75), textcoords='data',
|
|
# arrowprops=dict(arrowstyle="->", relpos=(0.5,0.5),
|
|
# connectionstyle=cs), zorder=1 )
|
|
ax.scatter(x, y, label='data', s=50, zorder=10)
|
|
ax.plot(xx, yy, 'r', lw=6.0, color='#ff0000', label='original', zorder=5)
|
|
ax.plot(xx, yf, '--', lw=2.0, color='#ffcc00', label='fit', zorder=7)
|
|
ax.legend(loc='upper left', frameon=False)
|
|
|
|
plt.tight_layout();
|
|
plt.savefig('mlepropline.pdf')
|
|
#plt.show();
|