python數(shù)據(jù)處理7

#==============================================================================

# 第七章 使用正確的圖表理解數(shù)據(jù)

# 7.1 簡介

#==============================================================================

#==============================================================================

# 7.2 理解對數(shù)圖

#==============================================================================

from matplotlib import pyplot as plt

import numpy as np

'''

根據(jù)經(jīng)驗,以下情況使用對數(shù)標度:

1.當要展示的數(shù)據(jù)的值跨越好幾個量級時

2.當要展示的數(shù)據(jù)有朝向大值(一些數(shù)據(jù)點比其他數(shù)據(jù)大很多)的傾斜度。

3.當要展示變化率(增長率),而不是值的變化

'''

x = np.linspace(1, 10)

y = [10 ** el for el in x]? #

z = [2 * el for el in x]? ?

fig = plt.figure(figsize=(10, 8))

ax1 = fig.add_subplot(2, 2, 1)

ax1.plot(x, y, color='blue')

ax1.set_yscale('log')? #對數(shù)標度

ax1.set_title(r'Logarithmic plot of $ {10}^{x} $ ')

ax1.set_ylabel(r'$ {y} = {10}^{x} $')

plt.grid(b=True, which='both', axis='both')

ax2 = fig.add_subplot(2, 2, 2)

ax2.plot(x, y, color='red')

ax2.set_yscale('linear')? ? #線性標度

ax2.set_title(r'Linear plot of $ {10}^{x} $ ')

ax2.set_ylabel(r'$ {y} = {10}^{x} $')

plt.grid(b=True, which='both', axis='both')

ax3 = fig.add_subplot(2, 2, 3)

ax3.plot(x, z, color='green')

ax3.set_yscale('log')? #對數(shù)標度

ax3.set_title(r'Logarithmic plot of $ {2}*{x} $ ')

ax3.set_ylabel(r'$ {y} = {2}*{x} $')

plt.grid(b=True, which='both', axis='both')

ax4 = fig.add_subplot(2, 2, 4)

ax4.plot(x, z, color='magenta')

ax4.set_yscale('linear')? ? #線性標度

ax4.set_title(r'Linear plot of $ {2}*{x} $ ')

ax4.set_ylabel(r'$ {y} = {2}*{x} $')

plt.grid(b=True, which='both', axis='both')

plt.show()

#==============================================================================

# 7.3 理解頻譜圖

#==============================================================================

安裝libsndfile1系統(tǒng)庫來讀/寫音頻文件

sudu apt-get install libasound1-dev

sudu apt-get install libasound2-dev

pip install scikits.audiolab

import os

from math import floor, log

from scikits.audiolab import Sndfile

import numpy as np

from matplotlib import pyplot as plt

soundfile = Sndfile("test.wav")

samplerate = soundfile.samplerate

start_sec = 0

stop_sec? = 5

start_frame = start_sec * soundfile.samplerate

stop_frame? = stop_sec * soundfile.samplerate

soundfile.seek(start_frame)

delta_frames = stop_frame - start_frame

sample = soundfile.read_frames(delta_frames)

map = 'CMRmap'

fig = plt.figure(figsize=(10, 6), )

ax = fig.add_subplot(111)

NFFT = 128

noverlap = 65

pxx,? freq, t, cax = ax.specgram(sample, Fs=soundfile.samplerate,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NFFT=NFFT, noverlap=noverlap,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? cmap=plt.get_cmap(map))

plt.colorbar(cax)

plt.xlabel("Times [sec]")

plt.ylabel("Frequency [Hz]")

plt.show()

-----------------------------------------------

import numpy

import matplotlib.pyplot as plt

def _get_mask(t, t1, t2, lvl_pos, lvl_neg):

? ? if t1 >= t2:

? ? ? ? raise ValueError("t1 must be less than t2")

? ? return numpy.where(numpy.logical_and(t > t1, t < t2), lvl_pos, lvl_neg)

def generate_signal(t):

? ? sin1 = numpy.sin(2 * numpy.pi * 100 * t)

? ? sin2 = 2 * numpy.sin(2 * numpy.pi * 200 * t)

? ? # add interval of high pitched signal

? ? masks = _get_mask(t, 2, 4, 1.0, 0.0) + \

? ? ? ? ? ? _get_mask(t, 14, 15, 1.0, 0.0)

? ? sin2 = sin2 * masks

? ? noise = 0.02 * numpy.random.randn(len(t))

? ? final_signal = sin1 + sin2 + noise

? ? return final_signal

if __name__ == '__main__':

? ? step = 0.001

? ? sampling_freq=1000

? ? t = numpy.arange(0.0, 20.0, step)

? ? y = generate_signal(t)

? ? # we can visualize this now

? ? # in time

? ? ax1 = plt.subplot(211)

? ? plt.plot(t, y)

? ? # and in frequency

? ? plt.subplot(212)

? ? plt.specgram(y, NFFT=1024, noverlap=900,

? ? ? ? Fs=sampling_freq, cmap=plt.cm.gist_heat)

? ? plt.show()

#==============================================================================

# 7.4 創(chuàng)建火柴桿圖

#==============================================================================

import matplotlib.pyplot as plt

import numpy as np

x = np.linspace(0, 20, 50)

y = np.sin(x + 1) + np.cos(x ** 2)

bottom = -0.1 # 基線位置

# True,hold current axes for further plotting

# False,opposite. clear and use new figure/plot

hold = False? #把所有當前圖形放在當前坐標軸上

label = "delta" #設置火柴杠圖圖例

'''

markerline:保存了表示火柴桿本身的線條引用,僅渲染了標記,不包括連接標記的線條

stemlines:保存了表示stemlines原點的水平線條的引用。

baseline:表示莖線的集合

'''

markerline, stemlines, baseline = plt.stem(x, y, bottom=bottom,label=label, hold=hold)

plt.setp(markerline, color='red', marker='o')

plt.setp(stemlines, color='blue', linestyle=':')

plt.setp(baseline, color='grey', linewidth=2, linestyle='-')

# draw a legend

plt.legend()

plt.show()

·

#==============================================================================

# 7.5 繪制矢量場流線圖

#==============================================================================

import matplotlib.pyplot as plt

import numpy as np

Y, X = np.mgrid[0:5:100j, 0:5:100j]

U = X # np.sin(X)

V = Y # np.sin(Y)

from pprint import pprint

print "X"

pprint(X)

print "Y"

pprint(Y)

plt.streamplot(X, Y, U, V)

plt.show()

#==============================================================================

# 7.6 使用顏色表

#==============================================================================

數(shù)據(jù)沒有與紅色/綠色有很強的關聯(lián)時,要盡可能的避免使用這兩種顏色埂淮。

顏色表:

Sequential:表示同一顏色從低飽和度到高飽和度的兩個色調(diào)的單色顏色表欠拾。

Diverging:表示中間值,是顏色的中值,然后顏色范圍在高和低兩個方向上變化到兩個不同的色調(diào)菠秒。

例如中值為0,能清晰的顯示負值和正值之間的區(qū)別恶阴。

Qualitative:對于數(shù)據(jù)沒有固定的順序,并且想要讓不同種類的數(shù)據(jù)之間能輕易的區(qū)分開的情況,可選用該顏色表淌山。

Cyclic:當數(shù)據(jù)可以圍繞端點值顯示的時候,比較方便拯勉,例如一天的時間竟趾,風向憔购,相位角。

matplotlib預定義的顏色表:autumn岔帽、bone玫鸟、cool、copper山卦、flag鞋邑、gray、hot账蓉、hsv枚碗、jet、pink铸本、prism肮雨、sprint、summer箱玷、winter怨规、spectral

Yorick科學可視化包:gist_earth、gist_heat锡足、gist_ncar波丰、gist_rainbow、gist_stern

ColorBrewer顏色表

Diverging:中間亮度最高,向兩端遞減

Sequential:亮度單調(diào)地遞減

Qualitative:不同種類的顏色用來區(qū)分不同的數(shù)據(jù)類別

其他:

brg:表示一個發(fā)散型的藍舶得、紅掰烟、綠顏色表

bwr:發(fā)散型藍、白沐批、紅顏色表

seismic:發(fā)散藍纫骑、白、紅

coolwarm:對于3D陰影,色盲和顏色排序非常有用

rainbow:發(fā)散亮度的紫九孩、藍先馆、綠、黃躺彬、橙煤墙、紅光譜

terrain:地圖標記的顏色(藍、綠宪拥、黃仿野、棕、白)?

顏色表名_r表示反轉(zhuǎn)江解。

import matplotlib as mpl

import matplotlib.pyplot as plt

import numpy as np?

red_yellow_green = ['#d73027', '#f46d43', '#fdae61',

? ? ? ? ? ? ? ? ? ? '#fee08b', '#ffffbf', '#d9ef8b',

? ? ? ? ? ? ? ? ? ? '#a6d96a', '#66bd63', '#1a9850']

fig,ax = plt.subplots(1)

for i in range(9):

? ? y = np.random.normal(size=1000).cumsum()

? ? x = np.arange(1000)

? ? ax.scatter(x, y, label=str(i), linewidth=0.1, edgecolors='grey', facecolor=red_yellow_green[i])

ax.legend()

plt.show()

#==============================================================================

# 7.7 使用散點圖和直方圖

#==============================================================================

散點圖

-----------------------------------------------------------

import matplotlib.pyplot as plt

import numpy as np

# import the data

from ch07_search_data import DATA

data = DATA, data1 = np.random.random(365)

assert len(data) == len(data1)

fig = plt.figure()

ax1 = fig.add_subplot(221)

ax1.scatter(data, data1, alpha=0.5)

ax1.set_title('No correlation')

ax1.grid(True)

ax2 = fig.add_subplot(222)

ax2.scatter(data1, data1, alpha=0.5)

ax2.set_title('Ideal positive correlation')

ax2.grid(True)

ax3 = fig.add_subplot(223)

ax3.scatter(data1, data1*-1, alpha=0.5)

ax3.set_title('Ideal negative correlation')

ax3.grid(True)

ax4 = fig.add_subplot(224)

ax4.scatter(data1, data1+data, alpha=0.5)

ax4.set_title('Non ideal positive correlation')

ax4.grid(True)

plt.tight_layout()

plt.show()

直方圖

-----------------------------------------------------------

import numpy as np

import matplotlib.pyplot as plt

from mpl_toolkits.axes_grid1 import make_axes_locatable

def scatterhist(x, y, figsize=(8,8)):

? ? """

? ? Create simple scatter & histograms of data x, y inside given plot

? ? @param figsize: Figure size to create figure

? ? @type figsize: Tuple of two floats representing size in inches

? ? @param x: X axis data set

? ? @type x: np.array

? ? @param y: Y axis data set

? ? @type y: np.array

? ? """

? ? _, scatter_axes = plt.subplots(figsize=figsize)

? ? # the scatter plot:

? ? scatter_axes.scatter(x, y, alpha=0.5)

? ? scatter_axes.set_aspect(1.)

? ? divider = make_axes_locatable(scatter_axes)

? ? axes_hist_x = divider.append_axes(position="top", sharex=scatter_axes, size=1, pad=0.1)

? ? axes_hist_y = divider.append_axes(position="right", sharey=scatter_axes, size=1, pad=0.1)

? ? binwidth = 0.25# compute bins accordingly

? ? # global max value in both data sets

? ? xymax = np.max([np.max(np.fabs(x)), np.max(np.fabs(y))])

? ? # number of bins

? ? bincap = int(xymax / binwidth) * binwidth

? ? bins = np.arange(-bincap, bincap, binwidth)

? ? nx, binsx, _ = axes_hist_x.hist(x, bins=bins, histtype='stepfilled',

? ? ? ? ? ? ? ? ? ? orientation='vertical')

? ? ny, binsy, _ = axes_hist_y.hist(y, bins=bins, histtype='stepfilled',

? ? ? ? ? ? ? ? ? ? orientation='horizontal')

? ? tickstep = 50

? ? ticksmax = np.max([np.max(nx), np.max(ny)])

? ? xyticks = np.arange(0, ticksmax + tickstep, tickstep)

? ? # hide x and y ticklabels on histograms

? ? for tl in axes_hist_x.get_xticklabels():

? ? ? ? tl.set_visible(False)

? ? axes_hist_x.set_yticks(xyticks)

? ? for tl in axes_hist_y.get_yticklabels():

? ? ? ? tl.set_visible(False)

? ? axes_hist_y.set_xticks(xyticks)

? ? plt.show()

if __name__ == '__main__':

? ? # import the data

? ? from ch07_search_data import DATA

? ? d = DATA

? ? # Now let's generate random data for the same period

? ? d1 = np.random.random(365)

? ? assert len(d) == len(d1)

? ? # try with the random data

#? ? d = np.random.randn(1000)

#? ? d1 = np.random.randn(1000)

? ? scatterhist(d, d1)

#==============================================================================

# 7.8 繪制兩個變量之間的互相圖形

#==============================================================================

繪兩個數(shù)據(jù)集之間的相互關系

import matplotlib.pyplot as plt

import numpy as np

# import the data

from ch07_search_data import DATA

data = DATA

total = sum(data)

av = total / len(data)

z = [i - av for i in data]

av1 = sum(data1) / len(data1)

z1 = [i - av1 for i in data1]

data1 = np.random.random(365)

assert len(data) == len(data1)

fig = plt.figure()

ax1 = fig.add_subplot(311)

ax1.plot(data)

ax1.set_xlabel('Google Trends data for "flowers"')

ax2 = fig.add_subplot(312)

ax2.plot(data1)

ax2.set_xlabel('Random data')

ax3 = fig.add_subplot(313)

ax3.set_xlabel('Cross correlation of random data')

ax3.xcorr(z, z1, usevlines=True, maxlags=None, normed=True, lw=2)

ax3.grid(True)

plt.ylim(-1,1)

plt.tight_layout()

plt.show()

#==============================================================================

# 7.9 自相關的重要性

#==============================================================================

import matplotlib.pyplot as plt

import numpy as np

from ch07_search_data import DATA as data

total = sum(data)

av = total / len(data)

z = [i - av for i in data]

fig = plt.figure()

plt.title('Comparing autocorrelations')

ax1 = fig.add_subplot(221)

ax1.plot(data)

ax1.set_xlabel('Google Trends data for "flowers"')

ax2 = fig.add_subplot(222)

ax2.acorr(z, usevlines=True, maxlags=None, normed=True, lw=2)

ax2.grid(True)

ax2.set_xlabel('Autocorrelation')

data1 = np.random.random(365)

assert len(data) == len(data1)

total = sum(data1)

av = total / len(data1)

z = [i - av for i in data1]

ax3 = fig.add_subplot(223)

ax3.plot(data1)

ax3.set_xlabel('Random data')

ax4 = fig.add_subplot(224)

ax4.set_xlabel('Autocorrelation of random data')

ax4.acorr( z, usevlines=True, maxlags=None, normed=True, lw=2)

ax4.grid(True)

plt.show()

#==============================================================================

# 第八章 更多matplotlib

# 8.2 繪制風杠barbs

#==============================================================================

import matplotlib.pyplot as plt

import numpy as np

'''

barbcolor:風杠中除旗標顏色

flagcolor:風杠中旗標顏色

facecolor:默認,前兩者將覆蓋這個

spacing:旗標/風杠屬性間的間距

height:箭桿到旗標或者風杠頂部的距離

width:旗標的寬度

emptybarb:定義最小值的圓圈的半徑

'''

V = [0, -5, -10, -15, -30, -40, -50, -60, -100]

U = np.zeros(len(V))

y = np.ones(len(V))

x = [0, 5, 10, 15, 30, 40, 50, 60, 100]

plt.barbs(x, y, U, V, length=9)

plt.xticks(x)

plt.ylim(0.98, 1.05)

plt.show()

----------------------------------------------

import matplotlib.pyplot as plt

import numpy as np

x = np.linspace(-20, 20, 8)

y = np.linspace(? 0, 20, 8)

# make 2D coordinates

X, Y = np.meshgrid(x, y)

U, V = X + 25, Y - 35

# plot the barbs

plt.subplot(1,2,1)

plt.barbs(X, Y, U, V, flagcolor='green', alpha=0.75)

plt.grid(True, color='gray')

# compare that with quiver / arrows

plt.subplot(1,2,2)

plt.quiver(X, Y, U, V, facecolor='red', alpha=0.75)

# misc settings

plt.grid(True, color='grey')

plt.show()

#==============================================================================

# 8.3 繪制箱線圖

#==============================================================================

import matplotlib.pyplot as plt

PROCESSES = {

? ? "A": [12, 15, 23, 24, 30, 31, 33, 36, 50, 73],

? ? "B": [6, 22, 26, 33, 35, 47, 54, 55, 62, 63],

? ? "C": [2, 3, 6, 8, 13, 14, 19, 23, 60, 69],

? ? "D": [1, 22, 36, 37, 45, 47, 48, 51, 52, 69],

? ? }

DATA = PROCESSES.values()

LABELS = PROCESSES.keys()

plt.boxplot(DATA, widths=0.3)

# set ticklabel to process name

plt.gca().xaxis.set_ticklabels(LABELS)

# some makeup (removing chartjunk)

for spine in plt.gca().spines.values():

? ? spine.set_visible(False)

plt.gca().xaxis.set_ticks_position('none')

plt.gca().yaxis.set_ticks_position('left')

plt.gca().grid(axis='y', color='gray')

# set axes labels

plt.ylabel("Errors observed over defined period.")

plt.xlabel("Process observed over defined period.")

plt.show()

#==============================================================================

# 8.4 繪制甘特圖

#==============================================================================

from datetime import datetime

import sys

import numpy as np

import matplotlib.pyplot as plt

import matplotlib.font_manager as font_manager

import matplotlib.dates as mdates

import logging

class Gantt(object):

? ? # from http://colorbrewer2.org/

? ? RdYlGr = ['#d73027', '#f46d43', '#fdae61',

? ? ? ? ? ? ? '#fee08b', '#ffffbf', '#d9ef8b',

? ? ? ? ? ? ? '#a6d96a', '#66bd63', '#1a9850']

? ? POS_START = 1.0

? ? POS_STEP = 0.5

? ? def __init__(self, tasks):

? ? ? ? self._fig = plt.figure()

? ? ? ? self._ax = self._fig.add_axes([0.1, 0.1, .75, .5])

? ? ? ? self.tasks = tasks[::-1]

? ? def _format_date(self, date_string):

? ? ? ? try:

? ? ? ? ? ? date = datetime.strptime(date_string, '%Y-%m-%d %H:%M:%S')

? ? ? ? except ValueError as err:

? ? ? ? ? ? logging.error("String '{0}' can not be converted to datetime object: {1}"

? ? ? ? ? ? ? ? ? .format(date_string, err))

? ? ? ? ? ? sys.exit(-1)

? ? ? ? mpl_date = mdates.date2num(date)

? ? ? ? return mpl_date

? ? def _plot_bars(self):

? ? ? ? i = 0

? ? ? ? for task in self.tasks:

? ? ? ? ? ? start = self._format_date(task['start'])

? ? ? ? ? ? end = self._format_date(task['end'])

? ? ? ? ? ? bottom = (i * Gantt.POS_STEP) + Gantt.POS_START

? ? ? ? ? ? width = end - start

? ? ? ? ? ? self._ax.barh(bottom, width, left=start, height=0.3,

? ? ? ? ? ? ? ? ? ? ? ? ? align='center', label=task['label'],

? ? ? ? ? ? ? ? ? ? ? ? ? color = Gantt.RdYlGr[i])

? ? ? ? ? ? i += 1

? ? def _configure_yaxis(self):'''y axis'''

? ? ? ? task_labels = [t['label'] for t in self.tasks]

? ? ? ? pos = self._positions(len(task_labels))

? ? ? ? ylocs = self._ax.set_yticks(pos)

? ? ? ? ylabels = self._ax.set_yticklabels(task_labels)

? ? ? ? plt.setp(ylabels, size='medium')

? ? def _configure_xaxis(self):''''x axis'''?

? ? ? ? self._ax.xaxis_date()

? ? ? ? # format date to ticks on every 7 days

? ? ? ? rule = mdates.rrulewrapper(mdates.DAILY, interval=7)

? ? ? ? loc = mdates.RRuleLocator(rule)

? ? ? ? formatter = mdates.DateFormatter("%d %b")

? ? ? ? self._ax.xaxis.set_major_locator(loc)

? ? ? ? self._ax.xaxis.set_major_formatter(formatter)

? ? ? ? xlabels = self._ax.get_xticklabels()

? ? ? ? plt.setp(xlabels, rotation=30, fontsize=9)

? ? def _configure_figure(self):

? ? ? ? self._configure_xaxis()

? ? ? ? self._configure_yaxis()

? ? ? ? self._ax.grid(True, color='gray')

? ? ? ? self._set_legend()

? ? ? ? self._fig.autofmt_xdate()

? ? def _set_legend(self):

? ? ? ? font = font_manager.FontProperties(size='small')

? ? ? ? self._ax.legend(loc='upper right', prop=font)

? ? def _positions(self, count):

? ? ? ? end = count * Gantt.POS_STEP + Gantt.POS_START

? ? ? ? pos = np.arange(Gantt.POS_START, end, Gantt.POS_STEP)

? ? ? ? return pos

? ? def show(self):

? ? ? ? self._plot_bars()

? ? ? ? self._configure_figure()

? ? ? ? plt.show()

if __name__ == '__main__':

? ? TEST_DATA = (

? ? ? ? ? ? ? ? { 'label': 'Research',? ? ? 'start':'2013-10-01 12:00:00', 'end': '2013-10-02 18:00:00'},? # @IgnorePep8

? ? ? ? ? ? ? ? { 'label': 'Compilation',? ? 'start':'2013-10-02 09:00:00', 'end': '2013-10-02 12:00:00'},? # @IgnorePep8

? ? ? ? ? ? ? ? { 'label': 'Meeting #1',? ? 'start':'2013-10-03 12:00:00', 'end': '2013-10-03 18:00:00'},? # @IgnorePep8

? ? ? ? ? ? ? ? { 'label': 'Design',? ? ? ? 'start':'2013-10-04 09:00:00', 'end': '2013-10-10 13:00:00'},? # @IgnorePep8

? ? ? ? ? ? ? ? { 'label': 'Meeting #2',? ? 'start':'2013-10-11 09:00:00', 'end': '2013-10-11 13:00:00'},? # @IgnorePep8

? ? ? ? ? ? ? ? { 'label': 'Implementation', 'start':'2013-10-12 09:00:00', 'end': '2013-10-22 13:00:00'},? # @IgnorePep8

? ? ? ? ? ? ? ? { 'label': 'Demo',? ? ? ? ? 'start':'2013-10-23 09:00:00', 'end': '2013-10-23 13:00:00'},? # @IgnorePep8

? ? ? ? ? ? ? ? )

? ? gantt = Gantt(TEST_DATA)

? ? gantt.show()

#==============================================================================

# 8.5 繪制誤差條

#==============================================================================

import matplotlib.pyplot as plt

import numpy as np

import scipy.stats as sc

TEST_DATA = np.array([[1,2,3,2,1,2,3,4,2,3,2,1,2,3,4,4,3,2,3,2,3,2,1],

? ? ? ? ? ? ? ? ? ? ? [5,6,5,4,5,6,7,7,6,7,7,2,8,7,6,5,5,6,7,7,7,6,5],

? ? ? ? ? ? ? ? ? ? ? [9,8,7,8,8,7,4,6,6,5,4,3,2,2,2,3,3,4,5,5,5,6,1],

? ? ? ? ? ? ? ? ? ? ? [3,2,3,2,2,2,2,3,3,3,3,4,4,4,4,5,6,6,7,8,9,8,5],

? ? ? ? ? ? ? ? ? ? ? ])

y = np.mean(TEST_DATA, axis=1, dtype=np.float64)

#計算出95%的置信區(qū)間

ci95 = np.abs(y - 1.96 * sc.sem(TEST_DATA, axis=1))

tries = np.arange(0, len(y), 1.0)# each set is one try

plt.grid(True, alpha=0.5)

plt.gca().set_xlabel('Observation #')

plt.gca().set_ylabel('Mean (+- 95% CI)')

plt.title("Observations with corresponding 95% CI as error bar.")

plt.bar(tries, y, align='center', alpha=0.2)

plt.errorbar(tries, y, yerr=ci95, fmt=None)

plt.show()

#==============================================================================

# 8.6 使用文本和字體屬性

#==============================================================================

import matplotlib.pyplot as plt

from matplotlib.font_manager import FontProperties

#字體類型

families = ['serif', 'sans-serif', 'cursive', 'fantasy', 'monospace']

#字體大小

sizes? = ['xx-small', 'x-small', 'small', 'medium', 'large',

? ? ? ? 'x-large', 'xx-large']

#字體風格

styles? = ['normal', 'italic', 'oblique']

#字體粗細

weights = ['light', 'normal', 'medium', 'semibold', 'bold', 'heavy', 'black']

#字體的變體形式

variants = ['normal', 'small-caps']

fig = plt.figure(figsize=(9,17))

ax = fig.add_subplot(111)

ax.set_xlim(0,9)

ax.set_ylim(0,17)

# VAR: FAMILY, SIZE

y = 0

size = sizes[0]

style = styles[0]

weight = weights[0]

variant = variants[0]

for family in families:

? ? x = 0

? ? y = y + .5

? ? for size in sizes:

? ? ? ? y = y + .4

? ? ? ? sample = family + " " + size

? ? ? ? ax.text(x, y, sample,

? ? ? ? ? ? ? ? family=family,

? ? ? ? ? ? ? ? size=size,

? ? ? ? ? ? ? ? style=style,

? ? ? ? ? ? ? ? weight=weight,

? ? ? ? ? ? ? ? variant=variant)


# VAR: STYLE, WEIGHT

y = 0

family = families[0]

size = sizes[4]

variant = variants[0]

for weight in weights:

? ? x = 5

? ? y = y + .5

? ? for style in styles:

? ? ? ? y = y + .4

? ? ? ? print x, y

? ? ? ? sample = weight + " " + style

? ? ? ? ax.text(x, y, sample,

? ? ? ? ? ? ? ? family=family,

? ? ? ? ? ? ? ? size=size,

? ? ? ? ? ? ? ? style=style,

? ? ? ? ? ? ? ? weight=weight,

? ? ? ? ? ? ? ? variant=variant)

ax.set_axis_off()

plt.show()

#==============================================================================

# 8.7 使用LaTeX渲染文本

#==============================================================================

import numpy as np

import matplotlib.pyplot as plt

# Example data

t = np.arange(0.0, 1.0 + 0.01, 0.01)

s = np.cos(4 * np.pi * t) * np.sin(np.pi*t/4) + 2

plt.rc('text', usetex=True)

plt.rc('font',**{'family':'sans-serif','sans-serif':['Helvetica'], 'size':16})

plt.plot(t, s, alpha=0.25)

# first, the equation for 's'

plt.annotate(r'$\cos(4 \times \pi \times {t}) \times \sin(\pi \times \frac {t} 4) + 2$', xy=(.9,2.2), xytext=(.5, 2.6), color='red', arrowprops={'arrowstyle':'->'})

# some math alphabet

plt.text(.01, 2.7, r'$\alpha, \beta, \gamma, \Gamma, \pi, \Pi, \phi, \varphi, \Phi$')

# some equation

plt.text(.01, 2.5, r'some equations $\frac{n!}{k!(n-k)!} = {n \choose k}$')

# more equations

plt.text(.01, 2.3, r'EQ1 $\lim_{x \to \infty} \exp(-x) = 0$')

# some ranges...

plt.text(.01, 2.1, r'Ranges: $( a ), [ b ], \{ c \}, | d |, \| e \|, \langle f \rangle, \lfloor g \rfloor, \lceil h \rceil$')

# you can multiply apples and oranges

plt.text(.01, 1.9, r'Text: $50 apples \times 100 oranges = lots of juice$')

plt.text(.01, 1.7, r'More text formatting: $50 \textrm{ apples} \times 100 \textbf{ apples} = \textit{lots of juice}$')

plt.text(.01, 1.5, r'Some indexing: $\beta = (\beta_1,\beta_2,\dotsc,\beta_n)$')

# we can also write on labels

plt.xlabel(r'\textbf{time} (s)')

plt.ylabel(r'\textit{y values} (W)')

# and write titles using LaTeX

plt.title(r"\TeX\ is Number "

? ? ? ? ? r"$\displaystyle\sum_{n=1}^\infty\frac{-e^{i\pi}}{2^n}$!",

? ? ? ? ? fontsize=16, color='gray')

# Make room for the ridiculously large title.

plt.subplots_adjust(top=0.8)

plt.savefig('tex_demo')

plt.show()

#==============================================================================

# 8.8 理解pyplot和OO API的不同

#==============================================================================

import matplotlib.pyplot as plt

from matplotlib.path import Path

import matplotlib.patches as patches

# add figure and axes

fig = plt.figure()

ax = fig.add_subplot(111)

coords = [

? ? (1., 0.),? # start position

? ? (0., 1.),

? ? (0., 2.),? # left side

? ? (1., 3.),

? ? (2., 3.),

? ? (3., 2.),? # top right corner

? ? (3., 1.),? # right side

? ? (2., 0.),

? ? (0., 0.),? # ignored

? ? ]

line_cmds = [Path.MOVETO,

? ? ? ? Path.LINETO,

? ? ? ? Path.LINETO,

? ? ? ? Path.LINETO,

? ? ? ? Path.LINETO,

? ? ? ? Path.LINETO,

? ? ? ? Path.LINETO,

? ? ? ? Path.LINETO,

? ? ? ? Path.CLOSEPOLY,

? ? ? ? ]

# construct path

path = Path(coords, line_cmds)

# construct path patch

patch = patches.PathPatch(path, lw=1,

? ? ? ? ? ? ? ? ? ? ? ? ? facecolor='#A1D99B', edgecolor='#31A354')

# add it to *ax* axes

ax.add_patch(patch)

ax.text(1.1, 1.4, 'Python', fontsize=24)

ax.set_xlim(-1, 4)

ax.set_ylim(-1, 4)

plt.show()

?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末徙歼,一起剝皮案震驚了整個濱河市犁河,隨后出現(xiàn)的幾起案子鳖枕,更是在濱河造成了極大的恐慌,老刑警劉巖桨螺,帶你破解...
    沈念sama閱讀 211,265評論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件宾符,死亡現(xiàn)場離奇詭異,居然都是意外死亡灭翔,警方通過查閱死者的電腦和手機魏烫,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,078評論 2 385
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來肝箱,“玉大人哄褒,你說我怎么就攤上這事』驼牛” “怎么了呐赡?”我有些...
    開封第一講書人閱讀 156,852評論 0 347
  • 文/不壞的土叔 我叫張陵,是天一觀的道長骏融。 經(jīng)常有香客問我链嘀,道長,這世上最難降的妖魔是什么档玻? 我笑而不...
    開封第一講書人閱讀 56,408評論 1 283
  • 正文 為了忘掉前任怀泊,我火速辦了婚禮,結(jié)果婚禮上误趴,老公的妹妹穿的比我還像新娘霹琼。我一直安慰自己,他們只是感情好冤留,可當我...
    茶點故事閱讀 65,445評論 5 384
  • 文/花漫 我一把揭開白布碧囊。 她就那樣靜靜地躺著,像睡著了一般纤怒。 火紅的嫁衣襯著肌膚如雪糯而。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,772評論 1 290
  • 那天泊窘,我揣著相機與錄音熄驼,去河邊找鬼。 笑死烘豹,一個胖子當著我的面吹牛瓜贾,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播携悯,決...
    沈念sama閱讀 38,921評論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼祭芦,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了憔鬼?” 一聲冷哼從身側(cè)響起龟劲,我...
    開封第一講書人閱讀 37,688評論 0 266
  • 序言:老撾萬榮一對情侶失蹤胃夏,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后昌跌,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體仰禀,經(jīng)...
    沈念sama閱讀 44,130評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,467評論 2 325
  • 正文 我和宋清朗相戀三年蚕愤,在試婚紗的時候發(fā)現(xiàn)自己被綠了答恶。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,617評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡萍诱,死狀恐怖悬嗓,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情裕坊,我是刑警寧澤烫扼,帶...
    沈念sama閱讀 34,276評論 4 329
  • 正文 年R本政府宣布,位于F島的核電站碍庵,受9級特大地震影響映企,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜静浴,卻給世界環(huán)境...
    茶點故事閱讀 39,882評論 3 312
  • 文/蒙蒙 一堰氓、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧苹享,春花似錦双絮、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,740評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至宫纬,卻和暖如春焚挠,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背漓骚。 一陣腳步聲響...
    開封第一講書人閱讀 31,967評論 1 265
  • 我被黑心中介騙來泰國打工蝌衔, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人蝌蹂。 一個月前我還...
    沈念sama閱讀 46,315評論 2 360
  • 正文 我出身青樓摩桶,卻偏偏與公主長得像芳悲,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子熄攘,可洞房花燭夜當晚...
    茶點故事閱讀 43,486評論 2 348

推薦閱讀更多精彩內(nèi)容