pythonのMatlibplotプロットライブラリ

14126 ワード

1、概要
MatplotlibのGitHubリンク:https://github.com/matplotlib/matplotlib
Matplotlibのインストールリンク:http://blog.csdn.net/walkandthink/article/details/45200597
Matplotlibはpythonの下で実現されるクラスmatlabの純粋なpythonの三方ライブラリであり、pythonでmatlabの機能を実現することを目的とし、pythonの下で最も優れたグラフィックライブラリであり、機能が完備しており、そのスタイルはmatlabとよく似ていると同時に、pythonの簡単明瞭なスタイルを継承し、2次元および3次元のデータの設計と出力を容易にすることができる.従来のデカルト座標、極座標、球座標、3 D座標などを提供します.
その使用にはnumpyライブラリ(pythonの下の配列処理の三方ライブラリ、便利な処理マトリクス、配列)をインストールし、データ図を作るのに原理が簡単で、関数をX、Y、Zの座標点の配列、例えば関数Y=X 2に変えて、私たちが図を描くのもまず特徴点(x、y)のセットを探して、それから線に接続します.matplotlibが図を出す過程と私たちが図を描く過程はあまり差がありません.先生はXの値を取る配列を作って、区間[0,1]の画像を描くには、まず[0,1]内の配列を取って(例えば、x=arangge(0,1,0.01)xが0.01をステップ長として100点を取ることを表しています)、それからxのyの値に対応するデータのセットを取って、座標(x,y)で描いた図が曲線です.
matplotlibは画像美化の面で比較的に完備しており、線の色とスタイルをカスタマイズすることができ、1枚の図面に複数の小図を描くことができ、1枚の図面に複数の線を描くことができ、データを簡単に可視化し、比較分析することができる.
各関数の意味:
python中的Matlibplot绘图库_第1张图片
Titleはタイトルです.Axisは座標軸、Labelは座標軸の寸法です.Tickは目盛線、Tick Labelは目盛注記です.各オブジェクトの間には、次のオブジェクトの依存関係があります.
这里写图片描述
2、numpy処理データ
numpy公式リンク:http://www.numpy.org/
matplotlibは、データを可視化する際に基本的に2つの方法を使用します.
(1)arangge関数はpythonのrange関数と類似しており、開始値、終了値、ステップ長を指定することで1次元配列を作成します.配列には終了値が含まれていないことに注意してください.
#      [0,1]   0.1         。          ,    1。
import numpy as np
print np.arange(0,1,0.1)

result:
[ 0.   0.1  0.2  0.3  0.4  0.5  0.6  0.7  0.8  0.9]

(2)linspace関数は、開始値、終了値、要素の数を指定して1次元配列を作成し、endpointキーワードで終了値を含めるかどうかを指定し、デフォルトでは終了値を含む.
#np.linspace(0, 1, 12,endpoint=False),       
import numpy as np
print np.linspace(0, 1, 12)

3、折れ線グラフ
#     
import matplotlib.pyplot as plt
year = [2011,2012,2013,2014]
pop = [1.2,3.4,4.5,6.5]
#       
plt.plot(year,pop)
plt.show();

python中的Matlibplot绘图库_第2张图片
4、散点図
#     
import matplotlib.pyplot as plt
year = [2011,2012,2013,2014]
pop = [1.2,3.4,4.5,6.5]
#       
plt.scatter(year,pop)
plt.show();

python中的Matlibplot绘图库_第3张图片
5、ヒストグラム
#     
import matplotlib.pyplot as plt
values = [0,1,2,3,4,1,2,3,4,4,5,2,4,1]
#       ,bins        
plt.hist(values,bins=10)
plt.show()

python中的Matlibplot绘图库_第4张图片
6、修飾図
title(’図形名’)(いずれも単一引用符に入れる)xlabel(’x軸説明’)ylabel(’y軸説明’)text(x,y,’図形説明’)legend(’図例1’,’図例2’,...)
#coding=utf-8
import matplotlib.pyplot as plt

year = [1950,1970,1990,2010]
pop = [2.3,3.4,5.8,6.5]

#   ,    
plt.fill_between(year,pop,0,color='green')

#    
plt.xlabel('Year')
plt.ylabel('Population')

#    
plt.title('World Population')

#  y  
plt.yticks([0,2,4,6,8,10],['0B','2B','4B','6B','8B','10B'])

python中的Matlibplot绘图库_第5张图片
7、複数のサブマップを描く
import matplotlib.pyplot as plt
import numpy as np

def f(t):
    return np.exp(-t) * np.cos(2 * np.pi * t)

t1 = np.arange(0, 5, 0.1)
t2 = np.arange(0, 5, 0.02)

plt.figure(12)
plt.subplot(221)
plt.plot(t1, f(t1), 'bo', t2, f(t2), 'r--')

plt.subplot(222)
plt.plot(t2, np.cos(2 * np.pi * t2), 'r--')

plt.subplot(212)
plt.plot([1, 2, 3, 4], [1, 4, 9, 16])

plt.show()

python中的Matlibplot绘图库_第6张图片
8、3 Dイメージを描く
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
X = [1, 1, 2, 2]
Y = [3, 4, 4, 3]
Z = [1, 2, 1, 1]
ax.plot_trisurf(X, Y, Z)
plt.show()

python中的Matlibplot绘图库_第7张图片
9、plt.axes()
まずFigureとAxesオブジェクトとは何かを見てみましょう.matplotlibでは、画像全体がFigureオブジェクトになります.Figureオブジェクトには、1つまたは複数のAxesオブジェクトを含めることができます.各Axesオブジェクトは、独自の座標系を持つ描画領域です.その論理関係は以下の通りである:python中的Matlibplot绘图库_第8张图片
import matplotlib.pyplot as plt
import numpy as np

# create some data to use for the plot
dt = 0.001
t = np.arange(0.0, 10.0, dt)
r = np.exp(-t[:1000] / 0.05)  # impulse response
x = np.random.randn(len(t))
s = np.convolve(x, r)[:len(x)] * dt  # colored noise

# the main axes is subplot(111) by default
plt.plot(t, s)
plt.axis([0, 1, 1.1 * np.amin(s), 2 * np.amax(s)])
plt.xlabel('time (s)')
plt.ylabel('current (nA)')
plt.title('Gaussian colored noise')

# this is an inset axes over the main axes
a = plt.axes([.65, .6, .2, .2], axisbg='y')
n, bins, patches = plt.hist(s, 400, normed=1)
plt.title('Probability')
plt.xticks([])
plt.yticks([])

# this is another inset axes over the main axes
a = plt.axes([0.2, 0.6, .2, .2], axisbg='y')
plt.plot(t[:len(r)], r)
plt.title('Impulse response')
plt.xlim(0, 0.2)
plt.xticks([])
plt.yticks([])

plt.show()

python中的Matlibplot绘图库_第9张图片
この文書では、Matlibplotデータの可視化に関する一般的なテクニックについて説明します.以下を参照してください.
(1)http://python.jobbole.com/85106/ (2)http://www.cnblogs.com/vamei/archive/2013/01/30/2879700.html#commentform