matplotlib绘图

matplotlib绘图

绘制双轴图

import numpy as np
import matplotlib.pyplot as plt

y = np.random.standard_normal([20, 2]).cumsum(axis=0)
plt.figure(figsize=(7, 4))
fig1, ax1 = plt.subplots()
plt.plot(y[:, 0]*100, lw=1.5, c=‘b’, label=‘1st’)
plt.legend(loc=0)
plt.plot(y[:, 0]*100,‘ro’)
plt.grid(True)
plt.axis(‘tight’)
plt.ylim(y[:, 0].min()*100, y[:, 0].max()*100)
plt.xlabel(‘index’)
plt.ylabel(‘value1’)
plt.title(‘2axis_plot’)

ax2 = ax1.twinx()
plt.plot(y[:, 1], lw=1.5, c='y', label='2nd')
plt.legend(loc=0)
plt.plot(y[:, 1],'ro')
plt.grid(True)
plt.axis('tight')
plt.ylim(y[:, 1].min(), y[:, 1].max())
plt.ylabel('value2')
plt.show()

绘制子图

import numpy as np
import matplotlib.pyplot as plt

y = np.random.standard_normal([20, 2]).cumsum(axis=0)
plt.figure(figsize=(7, 4))
plt.subplot(211)  # 1行两列布局的第一个图
plt.plot(y[:, 0]*100, lw=1.5, c='b', label='1st')  # 折线图
plt.legend(loc=0)
plt.plot(y[:, 0]*100,'ro')
plt.axis('tight')

plt.grid(True)
plt.ylim(y[:, 0].min()*100, y[:, 0].max()*100)
plt.xlabel('index')
plt.ylabel('value1')
plt.title('2sub_plot')

plt.subplot(212)  # 1行两列的子图中的第二个子图
plt.bar(np.arange(len(y)), y[:, 1], width=0.5, color='g', label='2nd')  # 柱状图
plt.legend(loc=0)
plt.axis('tight')
plt.grid(True)
plt.ylim(y[:, 1].min(), y[:, 1].max())
plt.ylabel('value2')
plt.show()

绘制热力散点图

import matplotlib.pyplot as plt
import numpy as np


y = np.random.standard_normal((1000, 2))
c = np.random.randint(0, 5, len(y))
plt.scatter(y[:, 0], y[:, 1], c=c, marker='o')
plt.colorbar()
 plt.xlabel('x')
plt.ylabel('y')
plt.title('hot_scater')
plt.show()

直方图

import matplotlib.pyplot as plt
import numpy as np

y = np.random.standard_normal((1000, 2))

直方图

plt.hist(y,label=['1st', '2nd'], color=['r', 'g'], bins=25, stacked=False)
plt.grid(True)
plt.legend(loc=0)
plt.title('histogram')
plt.show()

曲边梯形积分图

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon


def func(x):
    return 0.5*np.exp(x) + 1

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

fig, ax = plt.subplots(figsize=(7, 4))
plt.plot(x, y, 'b', linewidth=2)
plt.ylim(ymin=0)
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 0.5exp(x)+1 \mathrm{d}x$', fontsize=20, horizontalalignment='center')
plt.figtext(0.9, 0.075, '$x$')
plt.figtext(0.075, 0.9, '$f(x)$')
ax.set_xticks((a,b))
plt.show()

K线图

 import datetime
import numpy as np
import pandas.io.data as web
import matplotlib.pyplot as plt
import matplotlib.finance as mpf
   quotes = web.DataReader(
       'GOOG', data_source='google', start='05/01/2015', end='06/01/2015')
    
    fig, ax = plt.subplots(figsize=(8, 5))
    fig.subplots_adjust(bottom=0.2)
    mpf.candlestick_ohlc(ax, quotes, width=0.6, colorup='r', colordown='g')
    plt.grid(True)
    ax.xaxis_date()
    ax.autoscale_view()
    plt.setp(plt.gca().get_xticklabels(), rotation=30)  # x坐标轴刻度旋转30度
    plt.show()

竹节图

   import datetime
import numpy as np
import pandas.io.data as web
import matplotlib.pyplot as plt
import matplotlib.finance as mpf
   quotes = web.DataReader(
       'GOOG', data_source='google', start='05/01/2015', end='06/01/2015')
    fig, ax = plt.subplots(figsize=(8, 5))
    fig.subplots_adjust(bottom=0.2)
    mpf.plot_day_summary_ohlc(ax, quotes, colorup='r', colordown='g')
    plt.grid(True)
    ax.xaxis_date()
    ax.autoscale_view()
    plt._setp(plt.gca().get_xticklabels(), rotation=30)
    plt.show()

K线成交量图

 import datetime
import numpy as np
import pandas.io.data as web
import matplotlib.pyplot as plt
import matplotlib.finance as mpf
quotes = web.DataReader(
       'GOOG', data_source='google', start='05/01/2015', end='06/01/2015')
fig, (ax1, ax2) = plt.subplots(2, sharex=True, figsize=(8, 6))
mpf.candlestick_ohlc(ax1, quotes, width=0.6, colorup='r', colordown='g')  # k线图
ax1.set_title('inc')
ax1.set_ylabel('index level')  # 指数水平
ax1.grid(True)
ax1.xaxis_date()

plt.bar(quotes[:, 0]-0.25, quotes[:, 5], width=0.5)
ax2.set_ylabel('volumn')
ax2.grid(True)
ax2.autoscal_view()
plt._setp(plt.gca().get_xticklabels(), rotation=30)
plt.show()

模拟期权隐含波动率曲面

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

strike = np.linspace(50, 150, 24)
ttm = np.linspace(0.5, 2.5, 24)
strike, ttm = np.meshgrid(strike, ttm)
iv = (strike-100)**2/(100*strike)/ttm

fig = plt.figure(figsize=(9, 6))
ax = fig.gca(projection='3d')
surf = ax.plot_surface(
    strike, ttm, iv, rstride=2, cmap=plt.cm.coolwarm, linewidth=0.5, antialiased=True)
ax.set_xlabel('strike')
ax.set_ylabel('time_to_aturity')
ax.set_zlabel('implied_volatility')
fig.colorbar(surf, shrink=0.5, aspect=10)
plt.show()

模拟期权隐含波动率3D散点图

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

strike = np.linspace(50, 150, 24)
ttm = np.linspace(0.5, 2.5, 24)
strike, ttm = np.meshgrid(strike, ttm)
iv = (strike-100)**2/(100*strike)/ttm

fig = plt.figure(figsize=(9, 6))
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_aturity')
ax.set_zlabel('implied_volatility')
plt.show()

你可能感兴趣的:(python进阶)