<jupyter_start><jupyter_text>《利用Python进行数据分析》1st Edition chapter 8<jupyter_code>import numpy as np
import pandas as pd
from pandas import Series, DataFrame
import matplotlib.pyplot as plt
import os<jupyter_output><empty_output><jupyter_text>Figure 和 Subplot matplotlib API docs: https://matplotlib.org/api/index.html<jupyter_code>fig = plt.figure()
ax1 = fig.add_subplot(2, 2, 1)    #图像是 2 * 2的， 并且当前选中4个subplot中的第一个，编号从1开始
ax2 = fig.add_subplot(2, 2, 2)
ax3 = fig.add_subplot(2, 2, 3)
plt.plot(np.random.randn(50).cumsum(), 'k--')
type(ax3)
_ = 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))
fig
ax3.plot(np.random.randn(50).cumsum(), 'k--')
fig
fig, axes = plt.subplots(2, 3)
axes
# 利用 subplots_adjust() 调整subplot之间的间距, 此外该函数还是个顶级函数
plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=None)   # wspace和hspace用于控制宽度和高度的百分比
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.rand(500), bins=50, color='k', alpha=0.5)
        
plt.subplots_adjust(wspace=0, hspace=0)
"""
    u'参数说明'： axes.hist()    -- create a histogram  (直方图)
                bins : int or sequence or str
                color : color or array_like of colors or None, optional
                orientation : {'horizontal', 'vertical'}, optiona
                histtype : {'bar', 'barstacked', 'step', 'stepfilled'}, optional
""" 
from numpy.random import randn
plt.figure()
#fmt 参数的说明详见 https://matplotlib.org/api/_as_gen/matplotlib.pyplot.plot.html#matplotlib.pyplot.plot 中的Notes section
plt.plot(randn(30).cumsum(), 'ko--')    
plt.plot(randn(30).cumsum(), 'bv-')     #fmt格式举例： b : blue, v : triangle down  - : solid line
#或者可以将格式更加明确地写出：
plt.plot(randn(30), color='k', linestyle='dashed', marker='o')
data = randn(30).cumsum()
plt.plot(data, 'k--', label='Default')
plt.plot(data, 'k-', drawstyle='steps-post', label='steps-post')<jupyter_output><empty_output><jupyter_text>刻度、标签 和 图例<jupyter_code>fig = plt.figure(); ax = fig.add_subplot(1, 1, 1)
ax.plot(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')
fig
from matplotlib.font_manager import _rebuild
_rebuild()
fig = plt.figure(); ax = fig.add_subplot(1, 1, 1)
ax.plot(randn(1000).cumsum())
ticks = ax.set_xticks([0, 250, 500, 750, 1000])
labels = ax.set_xticklabels([u'一', u'二', u'三', u'四', u'五'],
                            rotation=30, fontsize='small')
ax.set_title(u'我的matplotlib plot')
ax.set_xlabel(u'阶段')
fig
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')    # 创建图例
fig
ax.text(600, 30, 'Hello World!', family='monospace', fontsize=12)
fig
# 绘制图形
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)<jupyter_output><empty_output><jupyter_text>将图表保存到文件<jupyter_code>plt.savefig('figpath.svg')
fig.savefig('figpath.pdf')    #根据文件扩展名推断文件类型
from io import BytesIO
buff = BytesIO()
fig.savefig(buff)
plot_data = buff.getvalue()<jupyter_output><empty_output><jupyter_text>matplotlib 配置<jupyter_code>plt.rc('figure', figsize=(10, 10))
# rc 的第一个参数是希望 自定义的对象， 如 ‘figure’, 'axes'， ‘xtick’, 'ytick', 'grid', 'legend'等
font_options = {'family' : 'monospace', 
                'weight' : 'bold', 
                'weight' : 'bold', 
                'size' : '12'}

plt.rc('font', **font_options)<jupyter_output><empty_output><jupyter_text>pandas 中的绘图函数<jupyter_code>plt.close('all')
fig
s = Series(np.random.randn(10).cumsum(), index=np.arange(0, 100, 10))
s.plot()     # 该Series对象的索引会被传给matplotlib, 并用以绘制X轴
df = DataFrame(np.random.randn(10, 4).cumsum(0),
               columns=['A', 'B', 'C', 'D'],
               index=np.arange(0, 100, 10))
df.plot()    # DataFrame 的plot() 方法会在一个subplot中为各列绘制一条线，并自动创建图例
fig, axes = plt.subplots(2, 1)
data = Series(np.random.rand(16), index=list('abcdefghijklmnop'))
data.plot(kind='bar', ax=axes[0], color='k', alpha=0.7)
data.plot(kind='barh', ax=axes[1], color='k', alpha=0.7)
df = DataFrame(np.random.rand(6, 4), index=[u'一', u'二', u'三', u'四', u'五', u'六'],
              columns=pd.Index(['A', 'B', 'C', 'D'], name='Statistic'))
df
df.plot(kind='bar')
from matplotlib.font_manager import _rebuild
_rebuild()
df.plot(kind='barh')
df.plot(kind='barh', stacked=False)
df.plot(kind='barh', stacked=True)<jupyter_output><empty_output><jupyter_text>直方图和密度图<jupyter_code>df.plot(kind='kde')
comp1 = np.random.normal(0, 1, size=200)
comp2 = np.random.normal(10, 2, size=200)
values = Series(np.concatenate([comp1, comp2]))
values.hist(bins=100, alpha=0.3, color='k', density=True)
values.plot(kind='kde', style='k--')<jupyter_output><empty_output><jupyter_text>绘制地图：图形化显示海地地震危机数据<jupyter_code>haiti_data = pd.read_csv('G:/dataAnalysis/data/Haiti.csv')
haiti_data
haiti_data.info()
haiti_data[['INCIDENT DATE', 'LOCATION', 'LONGITUDE', 'LATITUDE']][:10]
haiti_data['CATEGORY'][:7]     #消息紧急程序类型
haiti_data.describe()
# 去掉异常数据
data = haiti_data
data = data[(data.LATITUDE > 18) & (data.LATITUDE < 20) & (data.LONGITUDE > -75) & (data.LONGITUDE < 70) & data.CATEGORY.notnull()]
"""
获取所有分类的列表
"""
def to_cat_list(catstr):
    stripped = (x.strip() for x in catstr.split(','))
    return [x for x in stripped if x]

def get_all_categories(cat_series):
    cat_sets = (set(to_cat_list(x)) for x in cat_series)
    return sorted(set.union(*cat_sets))

"""
获取英语名称
"""
def get_english(cat):
    code, names = cat.split('.')
    if '|' in names:
        names = names.split(' | ')[1]
    return code, names.strip()
get_english('2. Urgences logistiques | Vital Lines')
all_catgs = get_all_categories(data.CATEGORY)
all_catgs[:10]
enname_mapping = dict(get_english(x) for x in all_catgs)
enname_mapping
enname_mapping['6a']
def get_code(seq):
    return [x.split('.')[0] for x in seq if x]

all_codes = get_code(all_catgs)
code_index = pd.Index(np.unique(all_codes))
dummy_frame = DataFrame(np.zeros((len(data), len(code_index))),
                        index=data.index, columns=code_index)
dummy_frame.iloc[:, :6].info()
for row, cat in zip(data.index, data.CATEGORY):
    codes = get_code(to_cat_list(cat))
    print codes
    dummy_frame.loc[row, codes] = 1

data = data.join(dummy_frame.add_prefix('category_'))
data.iloc[:, 10:15].info()
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt

def basic_haiti_map(ax=None, lllat=17.25, urlat=20.25,
                    lllon=-75, urlon=-71):
    # create polar stereographic Basemap instance.
    m = Basemap(ax=ax, projection='stere',
                lon_0=(urlon + lllon) / 2,
                lat_0=(urlat + lllat) / 2,
                llcrnrlat=lllat, urcrnrlat=urlat,
                llcrnrlon=lllon, urcrnrlon=urlon,
                resolution='f')
    # draw coastlines, state and country boundaries, edge of map.
    m.drawcoastlines()
    m.drawstates()
    m.drawcountries()
    return m
fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(12, 10))
fig.subplots_adjust(hspace=0.05, wspace=0.05)

to_plot = ['2a', '1', '3c', '7a']

lllat=17.25; urlat=20.25; lllon=-75; urlon=-71

for code, ax in zip(to_plot, axes.flat):
    m = basic_haiti_map(ax, lllat=lllat, urlat=urlat,
                        lllon=lllon, urlon=urlon)

    cat_data = data[data['category_%s' % code] == 1]

    # compute map proj coordinates.
    x, y = m(cat_data.LONGITUDE.values, cat_data.LATITUDE.values)

    shapefile_path = 'G:/dataAnalysis/data/ch8/PortAuPrince_Roads/PortAuPrince_Roads'
    m.readshapefile(shapefile_path, 'roads')
    m.plot(x, y, 'k.', alpha=0.5)
    ax.set_title('%s: %s' % (code, enname_mapping[code]))
fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(12, 10))
fig.subplots_adjust(hspace=0.05, wspace=0.05)

to_plot = ['2a', '1', '3c', '7a']

lllat=17.25; urlat=20.25; lllon=-75; urlon=-71

def make_plot():

    for i, code in enumerate(to_plot):
        cat_data = data[data['category_%s' % code] == 1]
        lons, lats = cat_data.LONGITUDE, cat_data.LATITUDE

        ax = axes.flat[i]
        m = basic_haiti_map(ax, lllat=lllat, urlat=urlat,
                            lllon=lllon, urlon=urlon)

        # compute map proj coordinates.
        x, y = m(lons.values, lats.values)

        m.plot(x, y, 'k.', alpha=0.5)
        ax.set_title('%s: %s' % (code, enname_mapping[code]))
make_plot()
shapefile_path = 'G:/dataAnalysis/data/ch8/PortAuPrince_Roads/PortAuPrince_Roads'
m.readshapefile(shapefile_path, 'roads')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17760106030483763, 'fraction_numerical': 0.03655842721449083, 'mean_word_length': 4.19804822043628, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '94453', 'license': 'MIT', 'n_tokens_mistral': 4073, 'n_tokens_neox': 3852, 'n_words': 677, 'path': 'pydata-learning/ch8.ipynb', 'repo_name': 'JanGin/pydata-book-learning', 'size': '9054'}