[Matplotlib] Data Representation

Jupyter Notebook

 

Goto: https://plot.ly/python/#3d-charts【丰富的作图资源】

 



 

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

一组点,画一条斜线。

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()

 

显示属性

格式化字符:

字符

描述

'-' 实线样式
'--' 短横线样式
'-.' 点划线样式
':' 虚线样式
'.' 点标记
',' 像素标记
'o' 圆标记
'v' 倒三角标记
'^' 正三角标记
'&lt;' 左三角标记
'&gt;' 右三角标记
'1' 下箭头标记
'2' 上箭头标记
'3' 左箭头标记
'4' 右箭头标记
's' 正方形标记
'p' 五边形标记
'*' 星形标记
'h' 六边形标记 1
'H' 六边形标记 2
'+' 加号标记
'x' X 标记
'D' 菱形标记
'd' 窄菱形标记
'&#124;' 竖直线标记
'_' 水平线标记

颜色的缩写:

字符颜色
'b' 蓝色
'g' 绿色
'r' 红色
'c' 青色
'm' 品红色
'y' 黄色
'k' 黑色
'w' 白色

 

 

一组点、曲线

点的密度大点就好了。

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() 函数允许你在同一图中绘制不同的东西。

fig,ax = subplots(nrows,ncols,sharex,sharey,squeeze,subplot_kw,gridspec_kw,**fig_kw)  创建画布和子图。

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的两组数据画在了同一个坐标轴上。

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接口

常用于计算。

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.

posted @ 2017-08-22 06:14  郝壹贰叁  阅读(395)  评论(0编辑  收藏  举报