博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Matplotlib图例
阅读量:5347 次
发布时间:2019-06-15

本文共 6158 字,大约阅读时间需要 20 分钟。

 

折线图示例

#!/usr/bin/python2.7import numpy as npfrom matplotlib import pyplot as pltfrom dbtools import raw_datafrom utils import moving_sumdef moving_sum(array, window):    if type(array) is not np.ndarray:        raise TypeError('Expected one dimensional numpy array.')    remainder = array.size % window    if 0 != remainder:        array = array[remainder:]    array = array.reshape((array.size/window,window))    sum_arr = np.sum(array,axis=1)    return sum_arrdef run():    window = 3    y_lst = raw_data('2018-08-03 00:00:00', length=3600*24)    raw_arr = np.array(y_lst)    sum_arr = moving_sum(raw_arr,window)    res = np.true_divide(sum_arr[1:],sum_arr[:-1])        threshold = 0.5    upper = np.array([1+threshold]*res.size)    lower = np.array([1-threshold]*res.size)        plt.plot(upper,lw=1,color='red',label='Upper')    plt.plot(res,lw=1,color='blue',label='Trend')    plt.plot(lower,lw=1,color='red',label='Lower')    r_idx = np.argwhere(np.abs(res-1) > 0.5)    plt.plot(r_idx, res[r_idx], 'ro')    plt.legend()    plt.show()    return (r_idx,res[r_idx])

画布和子图

import numpy as npimport matplotlib.pyplot as pltfig = plt.figure(figsize=[10,8])ax1 = fig.add_subplot(2,1,1)x1 = np.linspace(0.1,10,99,endpoint = False)y1 = np.log(x1)ax1.plot(x1,y1)ax1.set_title('Logarithmic function')x2 = np.linspace(0, 5, num = 100)y2 = np.e**x2ax2 = fig.add_subplot(2,1,2)ax2.plot(x2,y2)ax2.set_title('Exponential function')plt.subplots_adjust(hspace =0.2)fig.show()

 

柱状图

import numpy as npimport matplotlib.pyplot as pltdata = [32,48,19,85]labels = ['Jan','Feb','Mar','Apr']plt.bar(np.arange(len(data)),data,color='lightgreen')plt.xticks(np.arange(len(data)),labels)plt.show()

 

饼状图

import numpy as npimport matplotlib.pyplot as pltdata = [35,47,13,5]labels = ['Bili','iQiYi','Tencent','YouKu']plt.pie(data,labels=labels,autopct="%.2f%%",explode=[0.1,0,0,0],shadow=True)plt.show()

 

 

K线图

1 pip3.6 install https://github.com/matplotlib/mpl_finance/archive/master.zip2 3 from mpl_finance import candlestick_ochl
matplotlib.finance has been deprecated
1 # https://github.com/Gypsying/iPython/blob/master/601318.csv 2  3 In [2]: import pandas as pd                                                                                                                                                                                                                4  5 In [3]: df = pd.read_csv('601318.csv', index_col='date', parse_dates=['date'])                                                                                                                                                             6  7 In [4]: df.head()                                                                                                                                                                                                                          8 Out[4]:  9             Unnamed: 0    open   close    high     low      volume    code10 date                                                                      11 2007-03-01           0  21.878  20.473  22.302  20.040  1977633.51  60131812 2007-03-02           1  20.565  20.307  20.758  20.075   425048.32  60131813 2007-03-05           2  20.119  19.419  20.202  19.047   419196.74  60131814 2007-03-06           3  19.253  19.800  20.128  19.143   297727.88  60131815 2007-03-07           4  19.817  20.338  20.522  19.651   287463.78  60131816 17 In [5]: from matplotlib.dates import date2num                                                                                                                                                                                             18 19 In [6]: df['time'] = date2num(df.index.to_pydatetime())                                                                                                                                                                                   20 21 In [7]: df.head()                                                                                                                                                                                                                         22 Out[7]: 23             Unnamed: 0    open   close    high     low      volume    code      time24 date                                                                                25 2007-03-01           0  21.878  20.473  22.302  20.040  1977633.51  601318  732736.026 2007-03-02           1  20.565  20.307  20.758  20.075   425048.32  601318  732737.027 2007-03-05           2  20.119  19.419  20.202  19.047   419196.74  601318  732740.028 2007-03-06           3  19.253  19.800  20.128  19.143   297727.88  601318  732741.029 2007-03-07           4  19.817  20.338  20.522  19.651   287463.78  601318  732742.030 31 In [8]: df.shape                                                                                                                                                                                                                          32 Out[8]: (2563, 8)33 34 In [9]: df.size                                                                                                                                                                                                                           35 Out[9]: 2050436 37 In [10]: 2563*8                                                                                                                                                                                                                           38 Out[10]: 2050439 40 In [11]:
数据源
import numpy as npimport pandas as pdfrom matplotlib.dates import date2numfrom mpl_finance import candlestick_ochl# 构建 candlestick_ochl 的sequence of (time, open, close, high, low, ...)df = pd.read_csv('601318.csv', index_col='date', parse_dates=['date'])# time must be in float days format - see date2numdf['time'] = date2num(df.index.to_pydatetime())# 原始数据比较多,截取一部分做展示df = df.iloc[:300,:]arr = df[['time','open','close','high','low']].valuesfig = plt.figure(figsize=[14,7])ax = fig.add_subplot(1,1,1)candlestick_ochl(ax,arr)plt.grid(True, which='major', c='gray', ls='-', lw=1, alpha=0.2)fig.show()

 

 

 

 

 

  

 

转载于:https://www.cnblogs.com/standby/p/9483351.html

你可能感兴趣的文章
How to Find Research Problems
查看>>
Linux用户管理
查看>>
数据库第1,2,3范式学习
查看>>
《Linux内核设计与实现》第四章学习笔记
查看>>
使用iperf测试网络性能
查看>>
图片的显示隐藏(两张图片,默认的时候显示第一张,点击的时候显示另一张)...
查看>>
Docker 安装MySQL5.7(三)
查看>>
python 模块 来了 (调包侠 修炼手册一)
查看>>
关于CSS的使用方式
查看>>
本地MongoDB服务开启与连接本地以及远程服务器MongoDB服务
查看>>
跨域解决方案之CORS
查看>>
分析语句执行步骤并对排出耗时比较多的语句
查看>>
原生JS轮播-各种效果的极简实现
查看>>
软件工程总结作业---提问回顾与个人总结
查看>>
计数器方法使用?
查看>>
带你全面了解高级 Java 面试中需要掌握的 JVM 知识点
查看>>
sonar结合jenkins
查看>>
解决VS+QT无法生成moc文件的问题
查看>>
AngularJs练习Demo14自定义服务
查看>>
关于空想X
查看>>