# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # CH.9_Plotting_and_Visualization

# ## 1. Brief Matplotlib

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

data = np.arange(10)
data
plt.plot(data)
plt.show()

# ## 2. Subplot

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)

# ## 3. Color, Marker, Style

from numpy.random import randn
plt.plot(randn(30).cumsum(), 'ko--')
plt.show()

# +
data = np.random.randn(30).cumsum()
plt.plot(data, 'k--', label = 'Default')
plt.plot(data, 'k--', drawstyle = 'steps-post', label = 'steps-post')
plt.legend(loc = 'best')

plt.show()
# -

# ## 4. Label, Legend

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

# +
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(randn(1000).cumsum(), 'k', label = 'one')
ax.plot(randn(1000).cumsum(), 'k--', label = 'two')
ax.plot(randn(1000).cumsum(), 'k', label = 'three')
plt.legend(loc = 'best')

plt.show()
# -

# ## 5. Figure

# +
fig = plt.figure(figsize = (12, 6))
ax = fig.add_subplot(1,1,1)

rect = plt.Rectangle((0.2, 0.75), 0.4, 0.15, color = 'k', alpha = 0.3)
circ = plt.Circle((0.7, 0.2), 0.15, color = 'b', alpha = 0.3)
pgon = plt.Polygon([[0.15, 0.15], [0.35, 0.4], [0.2, 0.6]],
                  color = 'g', alpha = 0.5)

ax.add_patch(rect)
ax.add_patch(circ)
ax.add_patch(pgon)
# -

# - save  
# plt.savefig('figpath.svg')  
# plt.savefig('figpath.png', dpi = 400, bbox_inches = 'tight')  
# from io import BytesIO buffer = BytesIO()  
# plt.savefig(buffer)  
# plot_data = buffer.getvalue()  

# ## 6. Line, Bar, Histogram

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

fig, axes = plt.subplots(2,1)
data = pd.Series(np.random.rand(16), index = list('abcdefghijklmnop'))
data.plot.bar(ax = axes[0], color = 'k', alpha = 0.7)
data.plot.barh(ax = axes[1], color = 'k', alpha = 0.7)

comp1 = np.random.normal(0,1, size = 200)
comp2 = np.random.normal(10,2, size = 100)
values = pd.Series(np.concatenate([comp1, comp2]))
sns.distplot(values, bins = 100, color = 'k')

StarcoderdataJupyterScriptsDedupFiltered