<jupyter_start><jupyter_text>Scatter Plot (Realation Plots) Uses Design Practices Resources WebExamples showing up in this notebook come or based on this resource1. [[1.1.] The-Data-Visualization-Workshop/Chapter03/Examples/examples.ipynb](https://github.com/PacktWorkshops/The-Data-Visualization-Workshop/blob/master/Chapter03/Examples/examples.ipynb)2. [[1.2.] The-Data-Visualization-Workshop/Chapter03/Activity3.06/Activity3.06.ipynb](https://github.com/PacktWorkshops/The-Data-Visualization-Workshop/blob/master/Chapter03/Activity3.06/Activity3.06.ipynb)<jupyter_code>import numpy as np
import pandas as pd
import os
import time

import matplotlib.pyplot as plt
%matplotlib inline

from sklearn import datasets
# Const ...

# TODO ... 
# ROOT_DIR = 'C:\Salem\WorkSpace\Python\matplotlib'
ROOT_DIR = '/home/salem/WorkSpaces/python/matplotlib'
PATH_TO_DATA = os.path.join(ROOT_DIR, 'assets', 'data', 'the-data-visualization-workshop')
# methods ...

def read_csv(file_name : str):
    return pd.read_csv(os.path.join(PATH_TO_DATA, file_name))<jupyter_output><empty_output><jupyter_text>Data<jupyter_code>x = np.random.normal(0, 1, size=100)
y = x + np.random.normal(0, 1, size=100)

iris = datasets.load_iris()
X_ = iris.data[:, :2]  # we only take the first two features.
y_ = iris.target<jupyter_output><empty_output><jupyter_text>Plot Ex.1.  v.1.<jupyter_code>start = time.time()
# ...
plt.figure(figsize=(6,6), dpi=100)
plt.scatter(x, y)

# ...
plt.grid()
plt.xlabel('xaxis label')
plt.ylabel('yaxis label')

plt.title('Scatter plot')

plt.show();
stop = time.time()

print('===> Time : {} sec. <==='.format(stop - start))<jupyter_output><empty_output><jupyter_text>v.2.<jupyter_code>start = time.time()
# ...b
plt.figure(figsize=(6,6), dpi=100)
plt.plot(x, y, '*', c='r')

# ...
plt.grid()
plt.xlabel('xaxis label')
plt.ylabel('yaxis label')
plt.title('Scatter plot')

plt.show();
stop = time.time()
print('===> Time : {} sec. <==='.format(stop - start))<jupyter_output><empty_output><jupyter_text>Plot Ex.2.  v.1.<jupyter_code>start = time.time()

x_min, x_max = X_[:, 0].min() - .5, X_[:, 0].max() + .5
y_min, y_max = X_[:, 1].min() - .5, X_[:, 1].max() + .5

# ...
plt.figure(figsize=(6,6), dpi=100)
plt.scatter(X_[:, 0], X_[:, 1], c=y_, cmap=plt.cm.Set1, edgecolor='k', alpha=0.5)

# ...
plt.xlabel('Sepal length')
plt.ylabel('Sepal width')
plt.xlim(x_min, x_max)
plt.ylim(y_min, y_max)

# ...
plt.title('Scatter plot')
# plt.legend() # ??? <<<========
plt.grid()
plt.show();

stop = time.time()
print('===> Time : {} sec. <==='.format(stop - start))<jupyter_output><empty_output><jupyter_text>v.2.<jupyter_code>start = time.time()

x_min, x_max = X_[:, 0].min() - .5, X_[:, 0].max() + .5
y_min, y_max = X_[:, 1].min() - .5, X_[:, 1].max() + .5

# ...
plt.figure(figsize=(6,6), dpi=100)
plt.plot(X_[:50, 0], X_[:50, 1], '.', label='class 0', c='r')
plt.plot(X_[50:100, 0], X_[50:100, 1], '*', label='class 1', c='b')
plt.plot(X_[100:, 0], X_[100:, 1], '+', label='class 2', c='g')

# ...
plt.xlabel('Sepal length')
plt.ylabel('Sepal width')
plt.xlim(x_min, x_max)
plt.ylim(y_min, y_max)

# ...
plt.title('Scatter plot')
plt.legend() # ??? <<<========
plt.grid()
plt.show();

stop = time.time()
print('===> Time : {} sec. <==='.format(stop - start))<jupyter_output><empty_output><jupyter_text>Variants: Scatter Plots with Marginal Histograms<jupyter_code>data = read_csv('anage_data.csv')
data.head(3)
# Preprocessing
longevity = 'Maximum longevity (yrs)'
mass = 'Body mass (g)'
data = data[np.isfinite(data[longevity]) & np.isfinite(data[mass])]
# Sort according to class
aves = data[data['Class'] == 'Aves']
aves = data[data[mass] < 20000]
aves.head(2)
# ...
fig = plt.figure(figsize=(8, 8), dpi=150, constrained_layout=True)
gs = fig.add_gridspec(4, 4)

# ...
histx_ax = fig.add_subplot(gs[0, :-1])
histy_ax = fig.add_subplot(gs[1:, -1])
scatter_ax = fig.add_subplot(gs[1:, :-1])

# ...
scatter_ax.scatter(aves[mass], aves[longevity])

histx_ax.hist(aves[mass], bins=20, density=True)
histx_ax.set_xticks([])

histy_ax.hist(aves[longevity], bins=20, density=True, orientation='horizontal')
histy_ax.set_yticks([])

# ...
plt.xlabel('Body mass in grams')
plt.ylabel('Maximum longevity in years')
fig.suptitle('Scatter plot with marginal histograms')

plt.show();
# ...
fig = plt.figure(figsize=(8, 8), dpi=150, constrained_layout=True)
gs = fig.add_gridspec(4, 4)

# ...
histx_ax = fig.add_subplot(gs[0, :-1])
histy_ax = fig.add_subplot(gs[1:, -1])
scatter_ax = fig.add_subplot(gs[1:, :-1])

# ...
scatter_ax.scatter(np.log(aves[mass]), np.log(aves[longevity]))

histx_ax.hist(np.log(aves[mass]), bins=20, density=True)
histx_ax.set_xticks([])

histy_ax.hist(np.log(aves[longevity]), bins=20, density=True, orientation='horizontal')
histy_ax.set_yticks([])

# ...
plt.xlabel('Body mass in grams')
plt.ylabel('Maximum longevity in years')
fig.suptitle('Scatter plot with marginal histograms  <(log scale)>')

plt.show();<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.2152791775851643, 'fraction_numerical': 0.031042128603104215, 'mean_word_length': 5.2889733840304185, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 34, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '576762', 'license': 'MIT', 'n_tokens_mistral': 2208, 'n_tokens_neox': 2055, 'n_words': 354, 'path': 'The-Data-Visualization-Workshop/scatter-charts/ex1.ipynb', 'repo_name': 'SalAlba/matplotlib', 'size': '4961'}