diff --git a/programming/lecture/logical_operators.py b/programming/lecture/logical_operators.py new file mode 100644 index 0000000..df4c0c6 --- /dev/null +++ b/programming/lecture/logical_operators.py @@ -0,0 +1,69 @@ +import numpy as np +import matplotlib.pyplot as plt +from IPython import embed +# plt.xkcd() + +x = np.random.randn(1000000) + +fig = plt.figure() +ax1 = plt.subplot2grid((2,2), (0, 0)) +ax1.hist(x, bins=np.arange(-4,4.1, 0.1), density=False, fc='dodgerblue', lw=None, ec=None ) +ax1.set_xlim(-3, 3) +#ax1.set_xlabel("x", fontsize=9) +ax1.set_ylabel("#", fontsize=9) +ax1.spines["top"].set_visible(False) +ax1.spines["right"].set_visible(False) +ax1.set_xticks(range(-4, 5, 1)) +ax1.text(-3, ax1.get_ylim()[1], "x = randn(n, 1)", fontsize=8, color="dodgerblue") +ax1.set_yticklabels([]) +ax1.set_xticklabels([]) +ax1.text(-5.5, 1.025*ax1.get_ylim()[1], "A", fontsize=12) + +ax2 = plt.subplot2grid((2,2), (0, 1)) +ax2.hist(x[x < 0.0], bins=np.arange(-4,4.1, 0.1), density=False, fc='red', lw=None, ec=None) +ax2.hist(x[x > 0.0], bins=np.arange(-4,4.1, 0.1), density=False, fc='orange', lw=None, ec=None) +ax2.set_xlim(-3, 3) +#ax2.set_xlabel("x", fontsize=9) +ax2.set_ylabel("#", fontsize=9) +ax2.spines["top"].set_visible(False) +ax2.spines["right"].set_visible(False) +ax2.text(-2, ax2.get_ylim()[1], "x < 0.0", fontsize=8, color="red", ha="center") +ax2.text(2, ax2.get_ylim()[1], "x > 0.0", fontsize=8, color="orange", ha="center") +ax2.set_xticks(range(-4, 5, 1)) +ax2.set_yticklabels([]) +ax2.set_xticklabels([]) +ax2.text(-5.5, 1.025*ax2.get_ylim()[1], "B", fontsize=12) + +ax3 = plt.subplot2grid((2,2), (1, 0)) +ax3.hist(x[(x > -0.5) & (x < 0.5)], bins=np.arange(-4,4.1, 0.1), density=False, fc='red', lw=None, ec=None) +ax3.hist(x[(x < -0.5) | (x > 0.5)], bins=np.arange(-4,4.1, 0.1), density=False, fc='dodgerblue', lw=None, ec=None) +ax3.set_xlim(-3, 3) +ax3.set_xlabel("x", fontsize=9) +ax3.set_ylabel("#", fontsize=9) +ax3.spines["top"].set_visible(False) +ax3.spines["right"].set_visible(False) +ax3.text(0, ax3.get_ylim()[1], "x > -0.5 & x < 0.5", fontsize=8, color="red", ha="center") +ax3.set_xticks(range(-4, 5, 1)) +ax3.set_yticklabels([]) +ax3.text(-5.5, 1.025*ax3.get_ylim()[1], "C", fontsize=12) + +ax4 = plt.subplot2grid((2,2), (1, 1)) +ax4.hist(x[(x >= -1.2) & (x <= 1.2)], bins=np.arange(-4,4.1, 0.1), density=False, fc='dodgerblue', + lw=None, ec=None) +ax4.hist(x[(x < -1.2) | (x > 1.2)], bins=np.arange(-4,4.1, 0.1), density=False, fc='red', + lw=None, ec=None) +ax4.set_xlim(-3, 3) +ax4.set_xlabel("x", fontsize=9) +ax4.set_ylabel("#", fontsize=9) +ax4.spines["top"].set_visible(False) +ax4.spines["right"].set_visible(False) +ax4.text(0, ax4.get_ylim()[1], "x > -1.25 | x > 1.25", fontsize=8, color="red", ha="center") +ax4.set_xticks(range(-4, 5, 1)) +ax4.set_yticklabels([]) +ax4.text(-5.5, 1.025*ax4.get_ylim()[1], "D", fontsize=12) + +fig.set_size_inches(3.5, 3.5) +fig.subplots_adjust(left=0.1, right=0.975, top=0.95, bottom=0.12, hspace=0.3) +fig.savefig("logical_operations.png") +plt.close() +