# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python [conda root]
#     language: python
#     name: conda-root-py
# ---

# %matplotlib inline
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns

# # Plotting & Visualization

# ## [1] matplotlib Primer

# set style like ggplot
plt.style.use('ggplot')

# ### [1.1] Figures & Subplots

# #### Cumbersome Method

fig = plt.figure(); ## Figure object

ax1 = fig.add_subplot(2, 2, 1)  ## creates empty 2x2 figure; subplot 1 object
ax2 = fig.add_subplot(2, 2, 2)  ## subplot 2 object
ax3 = fig.add_subplot(2, 2, 3)  ## subplot 3 object

fig

ax1.hist(np.random.randn(100), bins=20, color='k', alpha=0.3);
ax2.scatter(np.arange(30), np.arange(30) + 3 * np.random.randn(30));
ax3.plot(np.random.randn(50).cumsum(), 'k--');

fig

# #### Better Method

fig, axes = plt.subplots(2, 2, sharex=False, sharey=False)

axes[0,0].hist(np.random.randn(100), bins=20, color='b', alpha=0.3);
axes[0,1].scatter(np.arange(30), np.arange(30) + 3 * np.random.randn(30));
axes[1,0].plot(np.random.randn(50).cumsum(), 'k--');
axes[1,1].plot(range(30), range(30));

fig

# ### [1.2] Adjusting Spacing Around Subplots

fig, axes = plt.subplots(2, 2, sharex=True, sharey=True)
for i in range(2):
    for j in range(2):
        axes[i, j].hist(np.random.randn(500), bins=50, color='k', alpha=0.5)
plt.subplots_adjust(wspace=0, hspace=0)  ## removes width and height spacing, respectively

# ### [1.3] Colors, Markers, & Line Styles

plt.plot(range(10), range(0, 20, 2), 'b--');

plt.plot(range(10), range(0, 20, 2), linestyle='--', color='darkgreen');  ## to separate linestyle and color

np.random.seed(11)
plt.plot(np.random.randn(30).cumsum(), 'ko--'); ## include data point markers
plt.grid();

np.random.seed(11)
plt.plot(np.random.randn(30).cumsum(), linestyle='--', color='orange', marker='o');  ## explicit version of graph above

np.random.seed(11)
data = np.random.randn(30).cumsum()
plt.plot(data, 'k-', drawstyle='steps-post', label='steps-post');  ## use drawstyle to alter interpolation between pts

plt.plot(data, 'k--', drawstyle='steps-mid', label='steps-post');  ## use drawstyle to alter interpolation between pts

# ### [1.4] Ticks, Labels, & Legends

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot(np.random.randn(1000).cumsum());

ticks = ax.set_xticks([0, 250, 500, 750, 1000])  ## select label positions
labels = ax.set_xticklabels(['one', 'two', 'three', 'four', 'five'], rotation=45, fontsize='small')  ## select label names

fig

ax.set_title('Example Plot')  ## add title
ax.set_xlabel('Stages')  ## add x-axis label
fig

# ### [1.5] Adding Legends

np.random.seed(10)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot(np.random.randn(1000).cumsum(), 'r', label='one')
ax.plot(np.random.randn(1000).cumsum(), 'b--', label='two')
ax.plot(np.random.randn(1000).cumsum(), 'g.', label='three')
ax.legend(loc='best');

# ### [1.6] Annotations on a Subplot

# +
np.random.seed(42)

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)

data = np.random.randn(20).cumsum()

labels = ('here', 'no here', 'etc')
locations = (20, 50, 95)

ax.plot(data)
ax.annotate('look here', xy=(17, -1), 
           xytext=(17, 1),
           arrowprops=dict(facecolor='black'),
           horizontalalignment='center', verticalalignment='top')  ## add text + arrow

circ = plt.Circle((3, 2), 1, color='g', alpha=0.3)  ## add green circle
ax.add_patch(circ);
# -

# ### [1.7] matplotlib Configuration

# +
#plt.rc('figure', figsize=(8,8))  ## set global parameters
# -

plt.plot(range(10));

# ## [2] Plotting w/pandas

# ### [2.1] Line Plots

s = pd.Series(np.random.randn(10).cumsum(), index=np.arange(0, 100, 10))

s.plot();

df = pd.DataFrame(np.random.randn(10, 4).cumsum(0), 
                  columns=['A', 'B', 'C', 'D'], 
                  index=np.arange(0, 100, 10))
df

df.plot();

df.plot(subplots=True, figsize=(7, 7), sharex=True, sharey=True, title="DF Subplots");

# ### [2.2] Bar Plots

fig, axes = plt.subplots(2, 1)
data = pd.Series(abs(np.random.randn(16)), index=list('abcdefghijklmnop'))
data.plot(kind='bar', ax=axes[0], color='r', alpha=0.7);
data.plot(kind='barh', ax=axes[1], color='b', alpha=0.7);

df = pd.DataFrame(abs(np.random.randn(6, 4)), 
                 index=['one', 'two', 'three', 'four', 'five', 'six'],
                 columns=pd.Index(['A', 'B', 'C', 'D'], name='Genus'))
df

df.plot(kind='bar', alpha=0.7);

df.plot(kind='bar', stacked=True, alpha=0.5);

# ### [2.3] Histograms & Density Plots

df.C.plot(kind='hist');

df.plot(kind='kde');

comp1 = np.random.normal(0, 1, size=200)
comp2 = np.random.normal(10, 2, size=200)
values = pd.Series(np.concatenate([comp1, comp2]))
values.hist(bins=100, alpha=0.3, color='k', normed=True);

values.hist(bins=100, alpha=0.3, color='k', normed=False);

values.plot(kind='kde', style='k--');

sns.distplot(values, bins=100);

df = pd.DataFrame(np.random.randn(100, 4), 
                 columns=pd.Index(['A', 'B', 'C', 'D'], name='Genus'))
pd.plotting.scatter_matrix(df, diagonal='hist', color='k', alpha=0.3);

sns.pairplot(df);

StarcoderdataJupyterScriptsDedupFiltered