[Matplotlib] Data Representation

Jupyter Notebook

 

Goto: https://plot.ly/python/#3d-charts【豐富的做圖資源】javascript

 



 

Data Visualization

In [1]:
from pylab import plt
plt.style.use('seaborn')
import matplotlib as mpl
mpl.rcParams['font.family'] = 'serif'
 

Two-Dimensional Plotting

In [2]:
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
 

One-Dimensional Data Set

In [3]:
np.random.seed(1000)
y = np.random.standard_normal(20)
In [4]:
x = range(len(y))
plt.plot(x, y)
# tag: matplotlib_0
# title: Plot given x- and y-values
Out[4]:
[<matplotlib.lines.Line2D at 0x110382fd0>]
 
In [5]:
plt.plot(y)
# tag: matplotlib_1
# title: Plot given data as 1d-array
Out[5]:
[<matplotlib.lines.Line2D at 0x11047f5c0>]
 
In [6]:
plt.plot(y.cumsum())
# tag: matplotlib_2
# title: Plot given a 1d-array with method attached
Out[6]:
[<matplotlib.lines.Line2D at 0x1104e6ac8>]
 
In [7]:
plt.plot(y.cumsum())
plt.grid(True)  # adds a grid
plt.axis('tight')  # adjusts the axis ranges
# tag: matplotlib_3_a
# title: Plot with grid and tight axes
Out[7]:
(-0.95000000000000007,
 19.949999999999999,
 -2.3228186637490449,
 0.56550858086558653)
 
In [8]:
plt.plot(y.cumsum())
plt.grid(True)
plt.xlim(-1, 20)
plt.ylim(np.min(y.cumsum()) - 1,
         np.max(y.cumsum()) + 1)
# tag: matplotlib_3_b
# title: Plot with custom axes limits
Out[8]:
(-3.1915310617211072, 1.4342209788376488)
 
In [9]:
plt.figure(figsize=(7, 4))
  # the figsize parameter defines the
  # size of the figure in (width, height)
plt.plot(y.cumsum(), 'b', lw=1.5)
plt.plot(y.cumsum(), 'ro')
plt.grid(True)
plt.axis('tight')
plt.xlabel('index')
plt.ylabel('value')
plt.title('A Simple Plot')
# tag: matplotlib_4
# title: Plot with typical labels
Out[9]:
Text(0.5,1,'A Simple Plot')
 
 

Two-Dimensional Data Set

In [10]:
np.random.seed(2000)
y = np.random.standard_normal((20, 2)).cumsum(axis=0)
In [11]:
plt.figure(figsize=(7, 4))
plt.plot(y, lw=1.5)
  # plots two lines
plt.plot(y, 'ro')
  # plots two dotted lines
plt.grid(True)
plt.axis('tight')
plt.xlabel('index')
plt.ylabel('value')
plt.title('A Simple Plot')
# tag: matplotlib_5
# title: Plot with two data sets
Out[11]:
Text(0.5,1,'A Simple Plot')
 
In [12]:
plt.figure(figsize=(7, 4))
plt.plot(y[:, 0], lw=1.5, label='1st')
plt.plot(y[:, 1], lw=1.5, label='2nd')
plt.plot(y, 'ro')
plt.grid(True)
plt.legend(loc=0)
plt.axis('tight')
plt.xlabel('index')
plt.ylabel('value')
plt.title('A Simple Plot')
# tag: matplotlib_6
# title: Plot with labeled data sets
Out[12]:
Text(0.5,1,'A Simple Plot')
 
In [13]:
y[:, 0] = y[:, 0] * 100
plt.figure(figsize=(7, 4))
plt.plot(y[:, 0], lw=1.5, label='1st')
plt.plot(y[:, 1], lw=1.5, label='2nd')
plt.plot(y, 'ro')
plt.grid(True)
plt.legend(loc=0)
plt.axis('tight')
plt.xlabel('index')
plt.ylabel('value')
plt.title('A Simple Plot')
# tag: matplotlib_7
# title: Plot with two differently scaled data sets
Out[13]:
Text(0.5,1,'A Simple Plot')
 
In [14]:
fig, ax1 = plt.subplots()
plt.plot(y[:, 0], 'b', lw=1.5, label='1st')
plt.plot(y[:, 0], 'ro')
plt.grid(True)
plt.legend(loc=8)
plt.axis('tight')
plt.xlabel('index')
plt.ylabel('value 1st')
plt.title('A Simple Plot')
ax2 = ax1.twinx()
plt.plot(y[:, 1], 'g', lw=1.5, label='2nd')
plt.plot(y[:, 1], 'ro')
plt.legend(loc=0)
plt.ylabel('value 2nd')
# tag: matplotlib_8
# title: Plot with two data sets and two y-axes
Out[14]:
Text(0,0.5,'value 2nd')
 
In [15]:
plt.figure(figsize=(7, 5))
plt.subplot(211)
plt.plot(y[:, 0], lw=1.5, label='1st')
plt.plot(y[:, 0], 'ro')
plt.grid(True)
plt.legend(loc=0)
plt.axis('tight')
plt.ylabel('value')
plt.title('A Simple Plot')
plt.subplot(212)
plt.plot(y[:, 1], 'g', lw=1.5, label='2nd')
plt.plot(y[:, 1], 'ro')
plt.grid(True)
plt.legend(loc=0)
plt.axis('tight')
plt.xlabel('index')
plt.ylabel('value')
# tag: matplotlib_9
# title: Plot with two sub-plots
Out[15]:
Text(0,0.5,'value')
 
In [16]:
plt.figure(figsize=(9, 4))
plt.subplot(121)
plt.plot(y[:, 0], lw=1.5, label='1st')
plt.plot(y[:, 0], 'ro')
plt.grid(True)
plt.legend(loc=0)
plt.axis('tight')
plt.xlabel('index')
plt.ylabel('value')
plt.title('1st Data Set')
plt.subplot(122) plt.bar(np.arange(len(y)), y[:, 1], width=0.5, color='g', label='2nd') plt.grid(True) plt.legend(loc=0) plt.axis('tight') plt.xlabel('index') plt.title('2nd Data Set') # tag: matplotlib_10 # title: Plot combining line/point sub-plot with bar sub-plot # size: 80
Out[16]:
Text(0.5,1,'2nd Data Set')
 
 

Other Plot Styles

In [17]:
y = np.random.standard_normal((1000, 2))
In [18]:
plt.figure(figsize=(7, 5))
plt.plot(y[:, 0], y[:, 1], 'ro')
plt.grid(True)
plt.xlabel('1st')
plt.ylabel('2nd')
plt.title('Scatter Plot')
# tag: matplotlib_11_a
# title: Scatter plot via +plot+ function
Out[18]:
Text(0.5,1,'Scatter Plot')
 
In [19]:
plt.figure(figsize=(7, 5))
plt.scatter(y[:, 0], y[:, 1], marker='o')
plt.grid(True)
plt.xlabel('1st')
plt.ylabel('2nd')
plt.title('Scatter Plot')
# tag: matplotlib_11_b
# title: Scatter plot via +scatter+ function
Out[19]:
Text(0.5,1,'Scatter Plot')
 
In [20]:
c = np.random.randint(0, 10, len(y))
In [21]:
plt.figure(figsize=(7, 5))
plt.scatter(y[:, 0], y[:, 1], c=c, marker='o')
plt.colorbar()
plt.grid(True)
plt.xlabel('1st')
plt.ylabel('2nd')
plt.title('Scatter Plot')
# tag: matplotlib_11_c
# title: Scatter plot with third dimension
Out[21]:
Text(0.5,1,'Scatter Plot')
 
In [22]:
plt.figure(figsize=(7, 4))
plt.hist(y, label=['1st', '2nd'], bins=25)
plt.grid(True)
plt.legend(loc=0)
plt.xlabel('value')
plt.ylabel('frequency')
plt.title('Histogram')
# tag: matplotlib_12_a
# title: Histogram for two data sets
Out[22]:
Text(0.5,1,'Histogram')
 
In [23]:
plt.figure(figsize=(7, 4))
plt.hist(y, label=['1st', '2nd'], color=['b', 'g'],
            stacked=True, bins=20)
plt.grid(True)
plt.legend(loc=0)
plt.xlabel('value')
plt.ylabel('frequency')
plt.title('Histogram')
# tag: matplotlib_12_b
# title: Stacked histogram for two data sets
Out[23]:
Text(0.5,1,'Histogram')
 
In [24]:
fig, ax = plt.subplots(figsize=(7, 4))
plt.boxplot(y)
plt.grid(True)
plt.setp(ax, xticklabels=['1st', '2nd'])
plt.xlabel('data set')
plt.ylabel('value')
plt.title('Boxplot')
# tag: matplotlib_13
# title: Boxplot for two data sets
# size: 70
Out[24]:
Text(0.5,1,'Boxplot')
 
In [25]:
from matplotlib.patches import Polygon
def func(x):
    return 0.5 * np.exp(x) + 1

a, b = 0.5, 1.5  # integral limits
x = np.linspace(0, 2)
y = func(x)

fig, ax = plt.subplots(figsize=(7, 5))
plt.plot(x, y, 'b', linewidth=2)
plt.ylim(ymin=0)

# Illustrate the integral value, i.e. the area under the function
# between lower and upper limit
Ix = np.linspace(a, b)
Iy = func(Ix)
verts = [(a, 0)] + list(zip(Ix, Iy)) + [(b, 0)]
poly = Polygon(verts, facecolor='0.7', edgecolor='0.5')
ax.add_patch(poly)

plt.text(0.5 * (a + b), 1, r"$\int_a^b f(x)\mathrm{d}x$",
         horizontalalignment='center', fontsize=20)

plt.figtext(0.9, 0.075, '$x$')
plt.figtext(0.075, 0.9, '$f(x)$')

ax.set_xticks((a, b))
ax.set_xticklabels(('$a$', '$b$'))
ax.set_yticks([func(a), func(b)])
ax.set_yticklabels(('$f(a)$', '$f(b)$'))
plt.grid(True)
# tag: matplotlib_math
# title: Exponential function, integral area and Latex labels
# size: 60
 
 

Financial Plots

In [26]:
import pandas as pd
import cufflinks as cf
In [27]:
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)
 
In [28]:
# data from FXCM Forex Capital Markets Ltd.
raw = pd.read_csv('source/fxcm_eur_usd_eod_data.csv',
                 index_col=0, parse_dates=True)
raw.columns
Out[28]:
Index(['Time', 'OpenBid', 'HighBid', 'LowBid', 'CloseBid', 'OpenAsk',
       'HighAsk', 'LowAsk', 'CloseAsk', 'TotalTicks'],
      dtype='object')
In [29]:
quotes = raw[['OpenAsk', 'HighAsk', 'LowAsk', 'CloseAsk']]
In [30]:
qf = cf.QuantFig(quotes.iloc[-100:], title='EUR/USD', legend='top',
                 name='EUR/USD', datalegend=False)
In [31]:
iplot(qf.iplot(asFigure=True))
 
 
In [32]:
qf.add_bollinger_bands(periods=15, boll_std=2)
In [33]:
iplot(qf.iplot(asFigure=True))
 
 
 

3d Plotting

In [34]:
strike = np.linspace(50, 150, 24)
ttm = np.linspace(0.5, 2.5, 24)
strike, ttm = np.meshgrid(strike, ttm)
In [35]:
strike[:2]
Out[35]:
array([[  50.        ,   54.34782609,   58.69565217,   63.04347826,
          67.39130435,   71.73913043,   76.08695652,   80.43478261,
          84.7826087 ,   89.13043478,   93.47826087,   97.82608696,
         102.17391304,  106.52173913,  110.86956522,  115.2173913 ,
         119.56521739,  123.91304348,  128.26086957,  132.60869565,
         136.95652174,  141.30434783,  145.65217391,  150.        ],
       [  50.        ,   54.34782609,   58.69565217,   63.04347826,
          67.39130435,   71.73913043,   76.08695652,   80.43478261,
          84.7826087 ,   89.13043478,   93.47826087,   97.82608696,
         102.17391304,  106.52173913,  110.86956522,  115.2173913 ,
         119.56521739,  123.91304348,  128.26086957,  132.60869565,
         136.95652174,  141.30434783,  145.65217391,  150.        ]])
In [36]:
iv = (strike - 100) ** 2 / (100 * strike) / ttm
  # generate fake implied volatilities
In [37]:
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure(figsize=(9, 6))
ax = fig.gca(projection='3d')

surf = ax.plot_surface(strike, ttm, iv, rstride=2, cstride=2,
                       cmap=plt.cm.coolwarm, linewidth=0.5,
                       antialiased=True)

ax.set_xlabel('strike')
ax.set_ylabel('time-to-maturity')
ax.set_zlabel('implied volatility')

fig.colorbar(surf, shrink=0.5, aspect=5)
# tag: matplotlib_17
# title: 3d surface plot for (fake) implied volatilities
# size: 70
Out[37]:
<matplotlib.colorbar.Colorbar at 0x1140aab70>
 
In [38]:
fig = plt.figure(figsize=(8, 5))
ax = fig.add_subplot(111, projection='3d')
ax.view_init(30, 60)

ax.scatter(strike, ttm, iv, zdir='z', s=25,
           c='b', marker='^')

ax.set_xlabel('strike')
ax.set_ylabel('time-to-maturity')
ax.set_zlabel('implied volatility')

# tag: matplotlib_18
# title: 3d scatter plot for (fake) implied volatilities
# size: 70
Out[38]:
Text(0.5,0,'implied volatility')
 
 

 

 

NumPy Matplotlib


一組點、斜線

pyplot

一組點,畫一條斜線。css

import numpy as np 
from matplotlib importpyplot as plt 
 
x = np.arange(1,11) 
y =  2  * x +  5 
plt.title(
"Matplotlib demo") plt.xlabel("x axis caption") plt.ylabel("y axis caption")
plt.plot(x,y,"ob")
plt.show()

 

顯示屬性

格式化字符:html

字符

描述html5

'-' 實線樣式
'--' 短橫線樣式
'-.' 點劃線樣式
':' 虛線樣式
'.' 點標記
',' 像素標記
'o' 圓標記
'v' 倒三角標記
'^' 正三角標記
'&lt;' 左三角標記
'&gt;' 右三角標記
'1' 下箭頭標記
'2' 上箭頭標記
'3' 左箭頭標記
'4' 右箭頭標記
's' 正方形標記
'p' 五邊形標記
'*' 星形標記
'h' 六邊形標記 1
'H' 六邊形標記 2
'+' 加號標記
'x' X 標記
'D' 菱形標記
'd' 窄菱形標記
'&#124;' 豎直線標記
'_' 水平線標記

顏色的縮寫:java

字符 顏色
'b' 藍色
'g' 綠色
'r' 紅色
'c' 青色
'm' 品紅色
'y' 黃色
'k' 黑色
'w' 白色

 

 

一組點、曲線

點的密度大點就行了。python

import numpy as np 
import matplotlib.pyplot as plt 
# 計算正弦曲線上點的 x 和 y 座標 x = np.arange(0, 3 * np.pi, 0.1) y = np.sin(x)
plt.title(
"sine wave form") # 使用 matplotlib 來繪製點 plt.plot(x, y) plt.show()

 

 

建立畫布上的子圖

subplot() 函數容許你在同一圖中繪製不一樣的東西。jquery

fig,ax = subplots(nrows,ncols,sharex,sharey,squeeze,subplot_kw,gridspec_kw,**fig_kw)  建立畫布和子圖。linux

import numpy as np 
import matplotlib.pyplot as plt 
# 計算正弦和餘弦曲線上的點的 x 和 y 座標 x = np.arange(0, 3 * np.pi, 0.1) y_sin = np.sin(x) y_cos = np.cos(x)
# 創建 subplot 網格,高爲 2,寬爲 1
# 激活第一個 subplot plt.subplot(2, 1, 1) # 繪製第一個圖像 plt.plot(x, y_sin) plt.title('Sine')
# 將第二個 subplot 激活,並繪製第二個圖像 plt.subplot(2, 1, 2) plt.plot(x, y_cos) plt.title('Cosine')
# 展現圖像 plt.show()

 

 

條形圖

顯示柱形圖

這裏x, x2的兩組數據畫在了同一個座標軸上。android

from matplotlib import pyplot as plt 
x
= [5,8,10] y = [12,16,6] x2 = [6,9,11] y2 = [6,15,7]
plt.bar(x, y, align
= 'center') plt.bar(x2, y2, color = 'g', align = 'center')
plt.title(
'Bar graph') plt.ylabel('Y axis') plt.xlabel('X axis') plt.show()

 

統計柱狀圖

NumPy接口

經常使用於計算。css3

import numpy as np 
 
a = np.array([22,87,5,43,56,73,55,54,11,20,51,5,79,31,27])
hist,bins = np.histogram(a,bins = [0,20,40,60,80,100])
print (hist) print (bins)

 

pyplot接口

可用於顯示。

from matplotlib import pyplot as plt 
import numpy as np  
 
a = np.array([22,87,5,43,56,73,55,54,11,20,51,5,79,31,27]) 
plt.hist(a, bins
= [0,20,40,60,80,100])
plt.title(
"histogram") plt.show()

 

 

End.

相關文章
相關標籤/搜索