<jupyter_start><jupyter_text>Coding Practice 0210  1. Matplotlib visualization with objects:<jupyter_code>import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline<jupyter_output><empty_output><jupyter_text>1.1. Visualization with figure object:<jupyter_code>x = np.linspace(0,10,100)
y = np.sin(x)
z = np.cos(x)
fig0=plt.figure()
axes0 = fig0.add_axes([0,0,1,1])             # Left, bottom, width, height.
axes0.plot(x,y,color='red',linestyle='-')
axes0.set_xlabel('X')
axes0.set_ylabel('Y')
axes0.set_title('LINE PLOT')
plt.show()
fig0=plt.figure()
axes0 = fig0.add_axes([0,0,1,1])             # Left, bottom, width, height.
axes0.plot(x,y,color='purple',linestyle='-')
axes0.set_xlabel('X')
axes0.set_ylabel('Y')
axes0.set_title('LINE PLOT')
axes0.set_xlim([0,5])                        # Horizontal axis limits.
axes0.set_ylim([-2,2])                       # Vertical axis limits.
plt.show()<jupyter_output><empty_output><jupyter_text>Specify the width and height with 'figsize' argument:<jupyter_code>fig0=plt.figure(figsize=(10,1))              # Width and height specified with figsize.
axes0 = fig0.add_axes([0,0,1,1])             # Left, bottom, width, height.
axes0.plot(x,y,color='red',linestyle='-')
axes0.set_xlabel('X')
axes0.set_ylabel('Y')
axes0.set_title('LINE PLOT')
plt.show()<jupyter_output><empty_output><jupyter_text>1.2. Multiple plots within the same axes:<jupyter_code>fig0=plt.figure(figsize=(5,2), dpi=100)      # Width, height and DPI setting.
axes0 = fig0.add_axes([0,0,1,1])             # Left, bottom, width, height.
axes0.plot(x,y,color='orange',linestyle='--', label='Sin')           # 'label' for the legend.
axes0.plot(x,z,color='purple',linestyle='-.', label='Cos')        # 'label' for the legend.
axes0.legend(loc=0)                                             # Legend to be locted at the top-right corner.
axes0.set_xlabel('X')
axes0.set_ylabel('Y')
axes0.set_title('LINE PLOT')
plt.show()<jupyter_output><empty_output><jupyter_text>Output to an external file:<jupyter_code>fig0.savefig('my_fig_01.png')<jupyter_output><empty_output><jupyter_text>1.3. Multiple plots in separate axes:<jupyter_code>fig0=plt.figure(figsize=(8,3), dpi=80)       # Width, height and DPI setting.
axes1 = fig0.add_axes([0,0,0.3,1])            # Left, bottom, width, height.
axes2 = fig0.add_axes([0.5,0,0.3,1])          # Left, bottom, width, height.
axes1.plot(x,y,color='red',linestyle=':')  
axes2.plot(x,z,color='blue',linestyle='-.')
axes1.set_xlabel('X')
axes1.set_ylabel('Y')
axes1.set_title('SINE')
axes2.set_xlabel('X')
axes2.set_ylabel('Y')
axes2.set_title('COSINE')
plt.show()
fig0=plt.figure(figsize=(8,4), dpi=80)          # Width, height and DPI setting.
axes1 = fig0.add_axes([0,0.5,0.2,0.5])           # Left, bottom, width, height.
axes2 = fig0.add_axes([0.5,0,0.2,0.5])           # Left, bottom, width, height.
axes1.plot(x,z,linestyle='none',marker='^',markersize=10,markerfacecolor='purple',markeredgecolor='red',alpha=0.5)
axes2.plot(x,y,color='blue',linestyle='--')  
axes1.set_xlabel('X')
axes1.set_ylabel('Y')
axes1.set_title('COSINE')
axes2.set_xlabel('X')
axes2.set_ylabel('Y')
axes2.set_title('SINE')
plt.show()<jupyter_output><empty_output><jupyter_text>1.4. Multiple plots in an array of axes:<jupyter_code>fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(6,6))
# (0,0)
c = np.random.randn(10000)
axes[0,0].hist(c,bins=100,color='green',density=True,alpha=0.3)
axes[0,0].set_title('HISTOGRAM')
# (1,0)
axes[1,0].plot(x,z,linestyle='none',marker='^',markersize=10,markerfacecolor='purple',markeredgecolor='red',alpha=0.5)
axes[1,0].set_xlabel('X')
axes[1,0].set_ylabel('Y')
axes[1,0].set_title('COSINE')
# (0,1)
a = np.array(['Q1', 'Q2', 'Q3','Q4'])
b = np.array([ 234.0, 254.7, 144.6, 317.6])
axes[0,1].bar(a,b,color='blue',alpha=0.5)
axes[0,1].set_title('BAR PLOT')
# (1,1)
axes[1,1].plot(x,y,color='green',linestyle=':')  
axes[1,1].set_xlabel('X')
axes[1,1].set_ylabel('Y')
axes[1,1].set_title('SINE')

plt.tight_layout()                             # Avoid overlapping.
plt.show()<jupyter_output><empty_output><jupyter_text>Output to an external file:<jupyter_code>fig.savefig('my_fig_02.png')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.19451073985680192, 'fraction_numerical': 0.05250596658711217, 'mean_word_length': 5.154185022026431, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '516485', 'license': 'MIT', 'n_tokens_mistral': 1861, 'n_tokens_neox': 1724, 'n_words': 241, 'path': 'SIC_AI_Coding_Exercises/SIC_AI_Chapter_03_Coding_Exercises/ex_0210.ipynb', 'repo_name': 'BattlePlot/SIC-Artificial-Intelligence', 'size': '4190'}