This repository has been archived on 2021-05-17. You can view files and clone it, but cannot push or open issues or pull requests.
scientificComputing/regression/lecture/cubicfunc.py

51 lines
1.4 KiB
Python

import matplotlib.pyplot as plt
import numpy as np
from plotstyle import *
def create_data():
# wikipedia:
# Generally, males vary in total length from 250 to 390 cm and
# weigh between 90 and 306 kg
c = 6
x = np.arange(2.2, 3.9, 0.05)
y = c * x**3.0
rng = np.random.RandomState(32281)
noise = rng.randn(len(x))*50
y += noise
return x, y, c
def plot_data(ax, x, y):
ax.plot(x, y, **psA)
ax.set_xlabel('Size x', 'm')
ax.set_ylabel('Weight y', 'kg')
ax.set_xlim(2, 4)
ax.set_ylim(0, 400)
ax.set_xticks(np.arange(2.0, 4.1, 0.5))
ax.set_yticks(np.arange(0, 401, 100))
def plot_data_fac(ax, x, y, c):
ax.plot(x, y, zorder=10, **psA)
xx = np.linspace(2.1, 3.9, 100)
ax.plot(xx, c*xx**3.0, zorder=5, **lsB)
for cc in [0.25*c, 0.5*c, 2.0*c, 4.0*c]:
ax.plot(xx, cc*xx**3.0, zorder=5, **lsDm)
ax.set_xlabel('Size x', 'm')
ax.set_ylabel('Weight y', 'kg')
ax.set_xlim(2, 4)
ax.set_ylim(0, 400)
ax.set_xticks(np.arange(2.0, 4.1, 0.5))
ax.set_yticks(np.arange(0, 401, 100))
if __name__ == "__main__":
x, y, c = create_data()
print('n=%d' % len(x))
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=cm_size(figure_width, 0.9*figure_height))
fig.subplots_adjust(wspace=0.5, **adjust_fs(fig, left=6.0, right=1.2))
plot_data(ax1, x, y)
plot_data_fac(ax2, x, y, c)
fig.savefig("cubicfunc.pdf")
plt.close()