Matplotlib学习笔记(二)

原  Matplotlib学习笔记

参考:Python数据科学入门教程

Python3.6.1

jupyter notebook

 

 

 

从文件加载数据

In [10]:
import matplotlib.pyplot as plt
import numpy as np
In [2]:
sample_data = np.loadtxt('./data/plot_example1.txt',delimiter=',')
In [3]:
sample_data
Out[3]:
array([[  1.,   5.],
       [  2.,   3.],
       [  3.,   4.],
       [  4.,   7.],
       [  5.,   4.],
       [  6.,   3.],
       [  7.,   5.],
       [  8.,   7.],
       [  9.,   4.],
       [ 10.,   4.]])
In [8]:
x, y = sample_data[:,0], sample_data[:,1]
In [9]:
plt.plot(x,y,label = 'Loaded from the file')
Out[9]:
[<matplotlib.lines.Line2D at 0x18790cc1e48>]
In [10]:
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()
 
 

注:更多内容学习numpy

 

从网络加载数据

In [35]:
import requests
import tushare as ts
In [2]:
stock = ts.get_h_data('399106', index=True) #深圳综合指数
 
[Getting data:]####
In [4]:
stock.shape
Out[4]:
(244, 6)
In [6]:
stock.head()
Out[6]:
 
 openhighcloselowvolumeamount
date      
2017-12-29 1888.051 1899.344 1899.344 1887.408 13977231914 195470662301
2017-12-28 1876.817 1893.923 1887.340 1874.927 15981871794 216291073972
2017-12-27 1890.585 1893.866 1878.803 1878.020 15471870004 211776769538
2017-12-26 1882.113 1892.206 1892.018 1871.673 13995679666 189812051879
2017-12-25 1900.396 1904.234 1883.891 1878.683 14797106870 203950137352
In [19]:
plt.plot(stock.index,stock.close,'-',color = 'b',label = '399106')
Out[19]:
[<matplotlib.lines.Line2D at 0x25f02ae0320>]
In [20]:
plt.xlabel('Date')
plt.ylabel('Close Price')
plt.title('Stock Plot')
plt.legend()
plt.show()
 
 

注:股票数据来自Python tushare库。 参考:tushare

 

基本自定义

In [30]:
#fig = plt.figure()
In [31]:
#ax1 = plt.subplot2grid((1,1),(0,0))
 

注:1,1表明这是一个 1×1 网格。 然后0,0表明这个子图的『起点』将为0,0

In [34]:
fig = plt.figure()
ax1 = plt.subplot2grid((1,1),(0,0))
ax1.plot(stock.index,stock.close,label = '399106')
for label in ax1.xaxis.get_ticklabels():
    label.set_rotation(45)                         #转动x轴标签45°
ax1.grid(True)                                  #, color='g', linestyle='-', linewidth=5)

plt.xlabel('Date')
plt.ylabel('Price')
plt.title('Subplot')
plt.legend()
#plt.subplots_adjust(left=0.09, bottom=0.20, right=0.94, top=0.90, wspace=0.2, hspace=0)
plt.show()
 
 

颜色与填充

In [49]:
fig = plt.figure()
ax1 = plt.subplot2grid((1,1), (0,0))
ax1.fill_between(stock.index,1500,stock.close,color='b')      #在1500至closeprice之间填充。
ax1.plot(stock.index,stock.close,linewidth=1.,color = 'k',label = '399106')

ax1.grid(True,linestyle='--')                     # color='g', linestyle='-', linewidth=5)
for label in ax1.xaxis.get_ticklabels():
    label.set_rotation(45)                         #转动x轴标签45°
    
ax1.set_yticks(np.arange(1500,2200,100))          #设置y轴分度
ax1.xaxis.label.set_color('c')
ax1.yaxis.label.set_color('r')                   #设置轴标签颜色
plt.xlabel('Date')
plt.ylabel('Price')
plt.title('Subplot')
plt.legend()
plt.subplots_adjust(left=0.09, bottom=0.20, right=0.94, top=0.90, wspace=0.2, hspace=0)
plt.show()
 
 

有条件填充

In [61]:
fig = plt.figure()
ax1 = plt.subplot2grid((1,1), (0,0))
date = stock.index
closep = stock.close
mean_closep = stock.close.mean()                 #收盘价均值

ax1.fill_between(date,closep,mean_closep,where=(closep>=mean_closep),facecolor='g',alpha=.6)      #有条件填充
ax1.fill_between(date,closep,mean_closep,where=(closep<mean_closep),facecolor='r',alpha=.6)

ax1.plot(stock.index,stock.close,linewidth=1.,color = 'b',label = 'Close Price')
ax1.plot([],[],linewidth=5, label='Low', color='r',alpha=0.5)                     #添加空白线
ax1.plot([],[],linewidth=5, label='High', color='g',alpha=0.5)
    
ax1.grid(True,linestyle='--')                     # color='g', linestyle='-', linewidth=5)
for label in ax1.xaxis.get_ticklabels():
    label.set_rotation(45)                         #转动x轴标签45°
    
#ax1.set_yticks(np.arange(1500,2200,100))          #设置y轴分度
ax1.xaxis.label.set_color('c')
ax1.yaxis.label.set_color('r')                   #设置轴标签颜色
plt.xlabel('Date')
plt.ylabel('Price')
plt.title('Fillplot')
plt.legend()
#plt.subplots_adjust(left=0.09, bottom=0.20, right=0.94, top=0.90, wspace=0.2, hspace=0)
plt.show()
 
 

说明: close price为深圳综合指数2016/12/31至2017/12/31复权数据中的收盘价; 高于均值为绿色,低于均值为红色。

 

边框和水平线

In [67]:
fig = plt.figure()
ax1 = plt.subplot2grid((1,1), (0,0))
date = stock.index
closep = stock.close
mean_closep = stock.close.mean()                 #收盘价均值

ax1.fill_between(date,closep,mean_closep,where=(closep>=mean_closep),facecolor='g',alpha=.6)      #有条件填充
ax1.fill_between(date,closep,mean_closep,where=(closep<mean_closep),facecolor='r',alpha=.6)

ax1.plot(stock.index,stock.close,linewidth=1.,color = 'b',label = 'Close Price')
ax1.plot([],[],linewidth=5, label='Low', color='r',alpha=0.5)                     #添加空白线
ax1.plot([],[],linewidth=5, label='High', color='g',alpha=0.5)
ax1.axhline(mean_closep,color = 'k',linewidth=1.)             #添加水平线

ax1.grid(True,linestyle='--')                     # color='g', linestyle='-', linewidth=5)
for label in ax1.xaxis.get_ticklabels():
    label.set_rotation(45)                         #转动x轴标签45°
    
#ax1.set_yticks(np.arange(1500,2200,100))          #设置y轴分度
#ax1.xaxis.label.set_color('c')
#ax1.yaxis.label.set_color('r')                   #设置轴标签颜色

for i in ax1.spines:
    ax1.spines[i].set_color('c')                 #设置边框颜色
    ax1.spines[i].set_linewidth(1.5)             #宽度
    #ax1.spines[i].set_visible(False)             #不显示边框
    
ax1.tick_params(axis='x',colors='#f06215')       #设置x轴标签颜色
plt.xlabel('Date')
plt.ylabel('Price')
plt.title('Fillplot')
plt.legend()
#plt.subplots_adjust(left=0.09, bottom=0.20, right=0.94, top=0.90, wspace=0.2, hspace=0)
plt.show()
 
 

说明:
 1.这个图很丑!
 2.16进制颜色代码可以用sublime自己选。
 3.看到一个小程序,16进制颜色和RGB的转换

 
对了,踩到一个坑,有python程序运行的时候不要安装包。

 

posted @ 2017-12-31 15:06  orange1002  阅读(291)  评论(0编辑  收藏  举报