<jupyter_start><jupyter_text>Plotting and Visualization<jupyter_code>import numpy as np
import pandas as pd
PREVIOUS_MAX_ROWS = pd.options.display.max_rows
pd.options.display.max_rows = 20
np.random.seed(12345)
import matplotlib.pyplot as plt
import matplotlib
# configuration files usually ends iwht *rc
plt.rc('figure', figsize=(10, 6))
np.set_printoptions(precision=4, suppress=True)
%matplotlib notebook<jupyter_output><empty_output><jupyter_text>A Brief matplotlib API Primer<jupyter_code>import matplotlib.pyplot as plt
import numpy as np
data = np.arange(10)
print(data)
plt.plot(data)<jupyter_output>[0 1 2 3 4 5 6 7 8 9]<jupyter_text>Figures and Subplots<jupyter_code># create a new figure, add subplots
# 2 X 2: up to four plots numbered as 1, 2, 3
fig = plt.figure()
ax1 = fig.add_subplot(2, 2, 1)
ax2 = fig.add_subplot(2, 2, 2)
ax3 = fig.add_subplot(2, 2, 3)
# draws on the last used figure and plot 
plt.plot(np.random.randn(50).cumsum(), 'k--')
fig = plt.figure()
ax1 = fig.add_subplot(2, 2, 1)
ax2 = fig.add_subplot(2, 2, 2)
ax3 = fig.add_subplot(2, 2, 3)
# directly plot on subplot
_ = 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))
plt.close('all')
# create a grid of subplots
fig, axes = plt.subplots(2, 3)
axes<jupyter_output><empty_output><jupyter_text>Adjusting the spacing around subplots * Use `subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=None)` method on `Figure` object<jupyter_code># shrink the percent of figure width and height to zero 
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)<jupyter_output><empty_output><jupyter_text>Colors, Markers, and Line Styles * Use `ax.plot(x, y, 'g--')` to specify color and linestyle * Or equivalently, `ax.plot(x, y, linestyle='--', color='g')`<jupyter_code>from numpy.random import randn
plt.figure()
plt.plot(randn(30).cumsum(), 'ko--')<jupyter_output><empty_output><jupyter_text>plot(randn(30).cumsum(), color='k', linestyle='dashed', marker='o')<jupyter_code>plt.close('all')
# change the interpolation style 
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')<jupyter_output><empty_output><jupyter_text>Ticks, Labels, and Legends  Setting the title, axis labels, ticks, and ticklabels<jupyter_code>fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot(np.random.randn(1000).cumsum())
# set tick locations and labels 
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])
labels = ax.set_xticklabels(['one', 'two', 'three', 'four', 'five'],
                            rotation=30, fontsize='small')
ax.set_title('My first matplotlib plot')
ax.set_xlabel('Stages')
# or set label and ticks with .set 
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])
labels = ax.set_xticklabels(['one', 'two', 'three', 'four', 'five'],
                            rotation=30, fontsize='small')

props = {
    'title': 'My first matplotlib plot',
    'xlabel': 'Stages'
}
ax.set(**props)<jupyter_output><empty_output><jupyter_text>Adding legends<jupyter_code>from numpy.random import randn
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')
ax.legend(loc='best')
ax.legend?<jupyter_output><empty_output><jupyter_text>Annotations and Drawing on a Subplot* Add annotations, text with `text`, `arrow` and `annotate` options.<jupyter_code>from numpy.random import randn
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')
ax.legend(loc='best')
ax.text(200, 35, 'Hello world!',
        family='monospace', fontsize=10)
# add annotation
from datetime import datetime

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

data = pd.read_csv('examples/spx.csv', index_col=0, parse_dates=True)
spx = data['SPX']

spx.plot(ax=ax, style='k-')

crisis_data = [
    (datetime(2007, 10, 11), 'Peak of bull market'),
    (datetime(2008, 3, 12), 'Bear Stearns Fails'),
    (datetime(2008, 9, 15), 'Lehman Bankruptcy')
]

for date, label in crisis_data:
    ax.annotate(label, xy=(date, spx.asof(date) + 75),
                xytext=(date, spx.asof(date) + 225),
                arrowprops=dict(facecolor='black', headwidth=4, width=2,
                                headlength=4),
                horizontalalignment='left', verticalalignment='top')

# Zoom in on 2007-2010
ax.set_xlim(['1/1/2007', '1/1/2011'])
ax.set_ylim([600, 1800])

ax.set_title('Important dates in the 2008-2009 financial crisis')
ax.set_title('Important dates in the 2008–2009 financial crisis')
ax.annotate?
matplotlib.patches?
fig = plt.figure()
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)
plt.Rectangle?
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)<jupyter_output><empty_output><jupyter_text>Saving Plots to File plt.savefig('figpath.svg') plt.savefig('figpath.png', dpi=400, bbox_inches='tight') from io import BytesIObuffer = BytesIO()plt.savefig(buffer)plot_data = buffer.getvalue()  matplotlib Configuration The first argument is the component you wish to customize, followed by a sequence of key word arguments.`plt.rc('figure', figsize=(10, 10))` font_options = {'family' : 'monospace',                'weight' : 'bold',                'size'   : 'small'}plt.rc('font', **font_options)  Plotting with pandas and seaborn  Line Plots* Use `plot` attribute of pandas<jupyter_code>plt.close('all')
s = pd.Series(np.random.randn(10).cumsum(), index=np.arange(0, 100, 10))
# s.xticks = list(range(1, 101, 10))
s.plot(use_index = False, xticks = list(range(1, 11, 1)))
# plot DataFrame's column as a different line on the same subplot 
df = pd.DataFrame(np.random.randn(10, 4).cumsum(0),
                  columns=['A', 'B', 'C', 'D'],
                  index=np.arange(0, 100, 10))
df.plot()<jupyter_output><empty_output><jupyter_text>Bar Plots* `plot.bar()`* `plot.barh()`* Use `seaborn`'s `barplot`<jupyter_code>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)
np.random.seed(12348)
# DataFrame bar plot group values of each row together 
df = pd.DataFrame(np.random.rand(6, 4),
                  index=['one', 'two', 'three', 'four', 'five', 'six'],
                  columns=pd.Index(['A', 'B', 'C', 'D'], name='Genus'))
df
df.plot.bar()
plt.figure()
# stack bar plots together with stacked = True 
df.plot.barh(stacked=True, alpha=0.5)
plt.close('all')
# use corsstab to find the percentage of data point 
tips = pd.read_csv('examples/tips.csv')
party_counts = pd.crosstab(tips['day'], tips['size'])
party_counts
# Not many 1- and 6-person parties
party_counts = party_counts.loc[:, 2:5]
party_pcts = party_counts.div(party_counts.sum(1), axis=0)
party_pcts
# Normalize to sum to 1
party_pcts.plot.bar()
plt.close('all')
import seaborn as sns
tips['tip_pct'] = tips['tip'] / (tips['total_bill'] - tips['tip'])
print(tips.head())
# average with 95% confidence interval 
sns.barplot(x='tip_pct', y='day', data=tips, orient='h')
plt.close('all')
# use hue to split by an additional categorical variable 
sns.barplot(x='tip_pct', y='day', hue='time', data=tips, orient='h')
plt.close('all')
sns.set(style="whitegrid")<jupyter_output><empty_output><jupyter_text>Histograms and Density Plots<jupyter_code>plt.figure()
tips['tip_pct'].plot.hist(bins=50)
plt.figure()
tips['tip_pct'].plot.density()
plt.figure()
# distplot from sns 
comp1 = np.random.normal(0, 1, size=200)
comp2 = np.random.normal(10, 2, size=200)
values = pd.Series(np.concatenate([comp1, comp2]))
sns.distplot(values, bins=100, color='k')<jupyter_output><empty_output><jupyter_text>Scatter or Point Plots<jupyter_code>macro = pd.read_csv('examples/macrodata.csv')
data = macro[['cpi', 'm1', 'tbilrate', 'unemp']]
trans_data = np.log(data).diff().dropna()
trans_data[-5:]
plt.figure()
sns.regplot('m1', 'unemp', data=trans_data)
plt.title('Changes in log %s versus log %s' % ('m1', 'unemp'))
sns.pairplot(trans_data, diag_kind='kde', plot_kws={'alpha': 0.2})<jupyter_output><empty_output><jupyter_text>Facet Grids and Categorical Data<jupyter_code>sns.catplot(x='day', y='tip_pct', hue='time', col='smoker',
               kind='bar', data=tips[tips.tip_pct < 1])
sns.catplot(x='day', y='tip_pct', row='time',
               col='smoker',
               kind='bar', data=tips[tips.tip_pct < 1])
sns.catplot(x='tip_pct', y='day', kind='box',
               data=tips[tips.tip_pct < 0.5])
sns.catplot(x='tip_pct', kind='box',
               data=tips[tips.tip_pct < 0.5])<jupyter_output><empty_output><jupyter_text>Other Python Visualization Tools<jupyter_code>pd.options.display.max_rows = PREVIOUS_MAX_ROWS<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18031297324583542, 'fraction_numerical': 0.044220090863200404, 'mean_word_length': 4.989117291414752, 'pattern_counts': {'":': 0, '<': 64, '<?xml version=': 0, '>': 60, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '15', 'dir': 'jupyter-structured-clean-dedup', 'id': '438129', 'license': 'MIT', 'n_tokens_mistral': 4226, 'n_tokens_neox': 3846, 'n_words': 817, 'path': 'Chapter_09_Plotting_and_Visualization.ipynb', 'repo_name': 'QianqianShan/Python_for_Data_Analysis', 'size': '9905'}